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 | |
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')
4 files changed, 35 insertions, 11 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend index 6898550d..c022beac 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend | |||
@@ -88,7 +88,7 @@ class ViatraReasoner extends LogicReasoner { | |||
88 | wf2ObjectiveConverter.createCompletenessObjective(method.unfinishedWF) | 88 | wf2ObjectiveConverter.createCompletenessObjective(method.unfinishedWF) |
89 | )) | 89 | )) |
90 | 90 | ||
91 | val extermalObjectives = Lists.newArrayListWithExpectedSize(viatraConfig.costObjectives.size) | 91 | val extremalObjectives = Lists.newArrayListWithExpectedSize(viatraConfig.costObjectives.size) |
92 | for (entry : viatraConfig.costObjectives.indexed) { | 92 | for (entry : viatraConfig.costObjectives.indexed) { |
93 | val objectiveName = '''costObjective«entry.key»''' | 93 | val objectiveName = '''costObjective«entry.key»''' |
94 | val objectiveConfig = entry.value | 94 | val objectiveConfig = entry.value |
@@ -111,13 +111,18 @@ class ViatraReasoner extends LogicReasoner { | |||
111 | objectiveConfig.threshold, 3) | 111 | objectiveConfig.threshold, 3) |
112 | dse.addObjective(costObjective) | 112 | dse.addObjective(costObjective) |
113 | if (objectiveConfig.findExtremum) { | 113 | if (objectiveConfig.findExtremum) { |
114 | extermalObjectives += costObjective | 114 | extremalObjectives += costObjective |
115 | } | 115 | } |
116 | } | 116 | } |
117 | 117 | ||
118 | val solutionStore = new SolutionStore(configuration.solutionScope.numberOfRequiredSolutions) | 118 | val numberOfRequiredSolutions = configuration.solutionScope.numberOfRequiredSolutions |
119 | val solutionStore = if (extremalObjectives.empty) { | ||
120 | new SolutionStore(numberOfRequiredSolutions) | ||
121 | } else { | ||
122 | new SolutionStore() | ||
123 | } | ||
119 | solutionStore.registerSolutionFoundHandler(new LoggerSolutionFoundHandler(viatraConfig)) | 124 | solutionStore.registerSolutionFoundHandler(new LoggerSolutionFoundHandler(viatraConfig)) |
120 | val solutionSaver = new ViatraReasonerSolutionSaver(newArrayList(extermalObjectives)) | 125 | val solutionSaver = new ViatraReasonerSolutionSaver(newArrayList(extremalObjectives), numberOfRequiredSolutions) |
121 | val solutionCopier = solutionSaver.solutionCopier | 126 | val solutionCopier = solutionSaver.solutionCopier |
122 | solutionStore.withSolutionSaver(solutionSaver) | 127 | solutionStore.withSolutionSaver(solutionSaver) |
123 | dse.solutionStore = solutionStore | 128 | dse.solutionStore = solutionStore |
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 | } |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend index 362ef4a3..e2585c83 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend | |||
@@ -69,7 +69,6 @@ class ThreeValuedCostObjective extends AbstractThreeValuedObjective { | |||
69 | if (matcher.weight <= 0) { | 69 | if (matcher.weight <= 0) { |
70 | cost += matcher.weight * matcher.mustMatcher.countMatches | 70 | cost += matcher.weight * matcher.mustMatcher.countMatches |
71 | } else if (matcher.mayMatcher.countMatches > 0) { | 71 | } else if (matcher.mayMatcher.countMatches > 0) { |
72 | // TODO Count may matches. | ||
73 | return Double.POSITIVE_INFINITY | 72 | return Double.POSITIVE_INFINITY |
74 | } | 73 | } |
75 | } | 74 | } |