diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ViatraReasonerSolutionSaver.xtend | 30 |
1 files changed, 25 insertions, 5 deletions
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 | } |