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 | 28 |
1 files changed, 23 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 17df6c55..6bffeb59 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 | |||
@@ -13,10 +13,11 @@ import org.eclipse.viatra.dse.solutionstore.SolutionStore.ISolutionSaver | |||
13 | import org.eclipse.xtend.lib.annotations.Accessors | 13 | import org.eclipse.xtend.lib.annotations.Accessors |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * Based on {@link org.eclipse.viatra.dse.solutionstore.SolutionStore.BestSolutionSaver}. | 16 | * Based on {@link SolutionStore.BestSolutionSaver}. |
17 | */ | 17 | */ |
18 | class ViatraReasonerSolutionSaver implements ISolutionSaver { | 18 | class ViatraReasonerSolutionSaver implements ISolutionSaver { |
19 | @Accessors val solutionCopier = new SolutionCopier | 19 | @Accessors val solutionCopier = new SolutionCopier |
20 | @Accessors val DiversityChecker diversityChecker | ||
20 | val boolean hasExtremalObjectives | 21 | val boolean hasExtremalObjectives |
21 | val int numberOfRequiredSolutions | 22 | val int numberOfRequiredSolutions |
22 | val ObjectiveComparatorHelper comparatorHelper | 23 | val ObjectiveComparatorHelper comparatorHelper |
@@ -24,7 +25,8 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
24 | 25 | ||
25 | @Accessors(PUBLIC_SETTER) var Map<Object, Solution> solutionsCollection | 26 | @Accessors(PUBLIC_SETTER) var Map<Object, Solution> solutionsCollection |
26 | 27 | ||
27 | new(IObjective[][] leveledExtremalObjectives, int numberOfRequiredSolutions) { | 28 | new(IObjective[][] leveledExtremalObjectives, int numberOfRequiredSolutions, DiversityChecker diversityChecker) { |
29 | this.diversityChecker = diversityChecker | ||
28 | comparatorHelper = new ObjectiveComparatorHelper(leveledExtremalObjectives) | 30 | comparatorHelper = new ObjectiveComparatorHelper(leveledExtremalObjectives) |
29 | hasExtremalObjectives = leveledExtremalObjectives.exists[!empty] | 31 | hasExtremalObjectives = leveledExtremalObjectives.exists[!empty] |
30 | this.numberOfRequiredSolutions = numberOfRequiredSolutions | 32 | this.numberOfRequiredSolutions = numberOfRequiredSolutions |
@@ -34,12 +36,15 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
34 | if (hasExtremalObjectives) { | 36 | if (hasExtremalObjectives) { |
35 | saveBestSolutionOnly(context, id, solutionTrajectory) | 37 | saveBestSolutionOnly(context, id, solutionTrajectory) |
36 | } else { | 38 | } else { |
37 | basicSaveSolution(context, id, solutionTrajectory) | 39 | saveAnyDiverseSolution(context, id, solutionTrajectory) |
38 | } | 40 | } |
39 | } | 41 | } |
40 | 42 | ||
41 | private def saveBestSolutionOnly(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { | 43 | private def saveBestSolutionOnly(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { |
42 | val fitness = context.lastFitness | 44 | val fitness = context.lastFitness |
45 | if (!fitness.satisifiesHardObjectives) { | ||
46 | return false | ||
47 | } | ||
43 | val dominatedTrajectories = newArrayList | 48 | val dominatedTrajectories = newArrayList |
44 | for (entry : trajectories.entrySet) { | 49 | for (entry : trajectories.entrySet) { |
45 | val isLastFitnessBetter = comparatorHelper.compare(fitness, entry.value) | 50 | val isLastFitnessBetter = comparatorHelper.compare(fitness, entry.value) |
@@ -54,9 +59,12 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
54 | if (dominatedTrajectories.size == 0 && !needsMoreSolutionsWithSameFitness) { | 59 | if (dominatedTrajectories.size == 0 && !needsMoreSolutionsWithSameFitness) { |
55 | return false | 60 | return false |
56 | } | 61 | } |
62 | if (!diversityChecker.newSolution(context, id, dominatedTrajectories.map[solution.stateCode])) { | ||
63 | return false | ||
64 | } | ||
57 | // We must save the new trajectory before removing dominated trajectories | 65 | // We must save the new trajectory before removing dominated trajectories |
58 | // to avoid removing the current solution when it is reachable only via dominated trajectories. | 66 | // to avoid removing the current solution when it is reachable only via dominated trajectories. |
59 | val solutionSaved = basicSaveSolution(context, id, solutionTrajectory) | 67 | val solutionSaved = basicSaveSolution(context, id, solutionTrajectory, fitness) |
60 | for (dominatedTrajectory : dominatedTrajectories) { | 68 | for (dominatedTrajectory : dominatedTrajectories) { |
61 | trajectories -= dominatedTrajectory | 69 | trajectories -= dominatedTrajectory |
62 | val dominatedSolution = dominatedTrajectory.solution | 70 | val dominatedSolution = dominatedTrajectory.solution |
@@ -73,8 +81,18 @@ class ViatraReasonerSolutionSaver implements ISolutionSaver { | |||
73 | solutionSaved | 81 | solutionSaved |
74 | } | 82 | } |
75 | 83 | ||
76 | private def basicSaveSolution(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { | 84 | private def saveAnyDiverseSolution(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory) { |
77 | val fitness = context.lastFitness | 85 | val fitness = context.lastFitness |
86 | if (!fitness.satisifiesHardObjectives) { | ||
87 | return false | ||
88 | } | ||
89 | if (!diversityChecker.newSolution(context, id, emptyList)) { | ||
90 | return false | ||
91 | } | ||
92 | basicSaveSolution(context, id, solutionTrajectory, fitness) | ||
93 | } | ||
94 | |||
95 | private def basicSaveSolution(ThreadContext context, Object id, SolutionTrajectory solutionTrajectory, Fitness fitness) { | ||
78 | var boolean solutionSaved = false | 96 | var boolean solutionSaved = false |
79 | var dseSolution = solutionsCollection.get(id) | 97 | var dseSolution = solutionsCollection.get(id) |
80 | if (dseSolution === null) { | 98 | if (dseSolution === null) { |