diff options
author | 2019-04-08 23:19:31 +0200 | |
---|---|---|
committer | 2019-04-08 23:19:31 +0200 | |
commit | d239ff5d82587220c7e157d18936351256bccffe (patch) | |
tree | 14f46882a6e661cd982bf841427db8f224162169 /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse | |
parent | Objective POC implementation (diff) | |
download | VIATRA-Generator-d239ff5d82587220c7e157d18936351256bccffe.tar.gz VIATRA-Generator-d239ff5d82587220c7e157d18936351256bccffe.tar.zst VIATRA-Generator-d239ff5d82587220c7e157d18936351256bccffe.zip |
Fix solution store for optimization
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse')
2 files changed, 26 insertions, 6 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SurelyViolatedObjectiveGlobalConstraint.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SurelyViolatedObjectiveGlobalConstraint.xtend index 7fd494a0..f54a31ca 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SurelyViolatedObjectiveGlobalConstraint.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SurelyViolatedObjectiveGlobalConstraint.xtend | |||
@@ -24,6 +24,6 @@ class SurelyViolatedObjectiveGlobalConstraint implements IGlobalConstraint { | |||
24 | 24 | ||
25 | override checkGlobalConstraint(ThreadContext context) { | 25 | override checkGlobalConstraint(ThreadContext context) { |
26 | val bestFitness = DseUtils.caclulateBestPossibleFitness(context) | 26 | val bestFitness = DseUtils.caclulateBestPossibleFitness(context) |
27 | bestFitness.satisifiesHardObjectives && !solutionSaver.isFitnessDominated(bestFitness) | 27 | bestFitness.satisifiesHardObjectives && solutionSaver.fitnessMayBeSaved(bestFitness) |
28 | } | 28 | } |
29 | } | 29 | } |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend index 5877778e..17df6c55 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend | |||
@@ -18,14 +18,16 @@ import org.eclipse.xtend.lib.annotations.Accessors | |||
18 | class ViatraReasonerSolutionSaver implements ISolutionSaver { | 18 | class ViatraReasonerSolutionSaver implements ISolutionSaver { |
19 | @Accessors val solutionCopier = new SolutionCopier | 19 | @Accessors val solutionCopier = new SolutionCopier |
20 | val boolean hasExtremalObjectives | 20 | val boolean hasExtremalObjectives |
21 | val int numberOfRequiredSolutions | ||
21 | val ObjectiveComparatorHelper comparatorHelper | 22 | val ObjectiveComparatorHelper comparatorHelper |
22 | val Map<SolutionTrajectory, Fitness> trajectories = new HashMap | 23 | val Map<SolutionTrajectory, Fitness> trajectories = new HashMap |
23 | 24 | ||
24 | @Accessors(PUBLIC_SETTER) var Map<Object, Solution> solutionsCollection | 25 | @Accessors(PUBLIC_SETTER) var Map<Object, Solution> solutionsCollection |
25 | 26 | ||
26 | new(IObjective[][] leveledExtremalObjectives) { | 27 | new(IObjective[][] leveledExtremalObjectives, int numberOfRequiredSolutions) { |
27 | comparatorHelper = new ObjectiveComparatorHelper(leveledExtremalObjectives) | 28 | comparatorHelper = new ObjectiveComparatorHelper(leveledExtremalObjectives) |
28 | hasExtremalObjectives = leveledExtremalObjectives.exists[!empty] | 29 | hasExtremalObjectives = leveledExtremalObjectives.exists[!empty] |
30 | this.numberOfRequiredSolutions = numberOfRequiredSolutions | ||
29 | } | 31 | } |
30 | 32 | ||
31 | override saveSolution(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { | 33 | override saveSolution(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { |
@@ -49,6 +51,9 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
49 | dominatedTrajectories += entry.key | 51 | dominatedTrajectories += entry.key |
50 | } | 52 | } |
51 | } | 53 | } |
54 | if (dominatedTrajectories.size == 0 && !needsMoreSolutionsWithSameFitness) { | ||
55 | return false | ||
56 | } | ||
52 | // We must save the new trajectory before removing dominated trajectories | 57 | // We must save the new trajectory before removing dominated trajectories |
53 | // to avoid removing the current solution when it is reachable only via dominated trajectories. | 58 | // to avoid removing the current solution when it is reachable only via dominated trajectories. |
54 | val solutionSaved = basicSaveSolution(context, id, solutionTrajectory) | 59 | val solutionSaved = basicSaveSolution(context, id, solutionTrajectory) |
@@ -86,14 +91,29 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
86 | } | 91 | } |
87 | solutionSaved | 92 | solutionSaved |
88 | } | 93 | } |
89 | 94 | ||
90 | def isFitnessDominated(Fitness fitness) { | 95 | def fitnessMayBeSaved(Fitness fitness) { |
96 | if (!hasExtremalObjectives) { | ||
97 | return true | ||
98 | } | ||
99 | var boolean mayDominate | ||
91 | for (existingFitness : trajectories.values) { | 100 | for (existingFitness : trajectories.values) { |
92 | val isNewFitnessBetter = comparatorHelper.compare(fitness, existingFitness) | 101 | val isNewFitnessBetter = comparatorHelper.compare(fitness, existingFitness) |
93 | if (isNewFitnessBetter < 0) { | 102 | if (isNewFitnessBetter < 0) { |
94 | return true | 103 | return false |
95 | } | 104 | } |
105 | if (isNewFitnessBetter > 0) { | ||
106 | mayDominate = true | ||
107 | } | ||
108 | } | ||
109 | mayDominate || needsMoreSolutionsWithSameFitness | ||
110 | } | ||
111 | |||
112 | private def boolean needsMoreSolutionsWithSameFitness() { | ||
113 | if (solutionsCollection === null) { | ||
114 | // The solutions collection will only be initialized upon saving the first solution. | ||
115 | return true | ||
96 | } | 116 | } |
97 | false | 117 | solutionsCollection.size < numberOfRequiredSolutions |
98 | } | 118 | } |
99 | } | 119 | } |