diff options
author | 20001LastOrder <boqi.chen@mail.mcgill.ca> | 2020-11-04 01:16:22 -0500 |
---|---|---|
committer | 20001LastOrder <boqi.chen@mail.mcgill.ca> | 2020-11-04 01:16:22 -0500 |
commit | 93243cb3faf1ccd733081fcf380559ac03c9ad35 (patch) | |
tree | 421f9f174eb77c387b5acaa05f01e64a62cab3a7 /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java | |
parent | add realistic solver (diff) | |
parent | Optimizing generator with linear objective functions (diff) | |
download | VIATRA-Generator-93243cb3faf1ccd733081fcf380559ac03c9ad35.tar.gz VIATRA-Generator-93243cb3faf1ccd733081fcf380559ac03c9ad35.tar.zst VIATRA-Generator-93243cb3faf1ccd733081fcf380559ac03c9ad35.zip |
merge with current master, comment numerical solver related logging
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java | 191 |
1 files changed, 96 insertions, 95 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java index e0f838a6..4b7cead1 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java | |||
@@ -9,33 +9,25 @@ | |||
9 | *******************************************************************************/ | 9 | *******************************************************************************/ |
10 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse; | 10 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse; |
11 | 11 | ||
12 | import java.io.BufferedReader; | ||
13 | import java.io.IOException; | ||
14 | import java.io.InputStreamReader; | ||
15 | import java.util.ArrayList; | ||
16 | import java.util.Arrays; | 12 | import java.util.Arrays; |
17 | import java.util.Collection; | ||
18 | import java.util.Collections; | 13 | import java.util.Collections; |
19 | import java.util.Comparator; | 14 | import java.util.Comparator; |
20 | import java.util.Iterator; | 15 | import java.util.Iterator; |
21 | import java.util.LinkedList; | 16 | import java.util.LinkedList; |
22 | import java.util.List; | 17 | import java.util.List; |
23 | import java.util.Map; | ||
24 | import java.util.PriorityQueue; | 18 | import java.util.PriorityQueue; |
25 | import java.util.Random; | 19 | import java.util.Random; |
26 | 20 | ||
27 | import org.apache.log4j.Logger; | 21 | import org.apache.log4j.Logger; |
28 | import org.eclipse.emf.ecore.EObject; | 22 | import org.eclipse.emf.ecore.EObject; |
29 | import org.eclipse.emf.ecore.util.EcoreUtil; | 23 | import org.eclipse.emf.ecore.util.EcoreUtil; |
24 | import org.eclipse.viatra.dse.api.SolutionTrajectory; | ||
30 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; | 25 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; |
31 | import org.eclipse.viatra.dse.base.ThreadContext; | 26 | import org.eclipse.viatra.dse.base.ThreadContext; |
32 | import org.eclipse.viatra.dse.objectives.Fitness; | 27 | import org.eclipse.viatra.dse.objectives.Fitness; |
33 | import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper; | 28 | import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper; |
29 | import org.eclipse.viatra.dse.solutionstore.ISolutionFoundHandler; | ||
34 | import org.eclipse.viatra.dse.solutionstore.SolutionStore; | 30 | import org.eclipse.viatra.dse.solutionstore.SolutionStore; |
35 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | ||
36 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification; | ||
37 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
38 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | ||
39 | 31 | ||
40 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel; | 32 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel; |
41 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner; | 33 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner; |
@@ -43,13 +35,11 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem; | |||
43 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.InconsistencyResult; | 35 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.InconsistencyResult; |
44 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult; | 36 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult; |
45 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult; | 37 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult; |
46 | import hu.bme.mit.inf.dslreasoner.viatra2logic.NumericProblemSolver; | ||
47 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod; | ||
48 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.PartialInterpretation2Logic; | 38 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.PartialInterpretation2Logic; |
49 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; | 39 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; |
50 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.NeighbourhoodBasedPartialInterpretationStateCoder; | ||
51 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation; | 40 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation; |
52 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser; | 41 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser; |
42 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ModelGenerationMethod; | ||
53 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration; | 43 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration; |
54 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; | 44 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; |
55 | 45 | ||
@@ -82,58 +72,72 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
82 | // Running | 72 | // Running |
83 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; | 73 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; |
84 | private SolutionStore solutionStore; | 74 | private SolutionStore solutionStore; |
85 | private SolutionStoreWithCopy solutionStoreWithCopy; | ||
86 | private SolutionStoreWithDiversityDescriptor solutionStoreWithDiversityDescriptor; | ||
87 | private volatile boolean isInterrupted = false; | 75 | private volatile boolean isInterrupted = false; |
88 | private ModelResult modelResultByInternalSolver = null; | 76 | private ModelResult modelResultByInternalSolver = null; |
89 | private Random random = new Random(); | 77 | private Random random = new Random(); |
90 | //private Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers; | 78 | // private Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers; |
91 | public ActivationSelector activationSelector = new EvenActivationSelector(random); | 79 | public ActivationSelector activationSelector = new EvenActivationSelector(random); |
92 | public NumericSolver numericSolver = null; | 80 | public ViatraReasonerSolutionSaver solutionSaver; |
81 | public NumericSolver numericSolver; | ||
93 | // Statistics | 82 | // Statistics |
94 | private int numberOfStatecoderFail = 0; | 83 | private int numberOfStatecoderFail = 0; |
95 | private int numberOfPrintedModel = 0; | 84 | private int numberOfPrintedModel = 0; |
96 | private int numberOfSolverCalls = 0; | 85 | private int numberOfSolverCalls = 0; |
86 | public long globalConstraintEvaluationTime = 0; | ||
87 | public long fitnessCalculationTime = 0; | ||
97 | 88 | ||
98 | public long explorationStarted = 0; | 89 | public long explorationStarted = 0; |
99 | 90 | ||
100 | public BestFirstStrategyForModelGeneration( | 91 | public BestFirstStrategyForModelGeneration( |
101 | ReasonerWorkspace workspace, | 92 | ReasonerWorkspace workspace, |
102 | ViatraReasonerConfiguration configuration, | 93 | ViatraReasonerConfiguration configuration, |
103 | ModelGenerationMethod method) | 94 | ModelGenerationMethod method, |
104 | { | 95 | ViatraReasonerSolutionSaver solutionSaver, |
96 | NumericSolver numericSolver) { | ||
105 | this.workspace = workspace; | 97 | this.workspace = workspace; |
106 | this.configuration = configuration; | 98 | this.configuration = configuration; |
107 | this.method = method; | 99 | this.method = method; |
100 | this.solutionSaver = solutionSaver; | ||
101 | this.numericSolver = numericSolver; | ||
102 | // logger.setLevel(Level.DEBUG); | ||
108 | } | 103 | } |
109 | 104 | ||
110 | public SolutionStoreWithCopy getSolutionStoreWithCopy() { | ||
111 | return solutionStoreWithCopy; | ||
112 | } | ||
113 | public SolutionStoreWithDiversityDescriptor getSolutionStoreWithDiversityDescriptor() { | ||
114 | return solutionStoreWithDiversityDescriptor; | ||
115 | } | ||
116 | public int getNumberOfStatecoderFail() { | 105 | public int getNumberOfStatecoderFail() { |
117 | return numberOfStatecoderFail; | 106 | return numberOfStatecoderFail; |
118 | } | 107 | } |
119 | //LinkedList<ViatraQueryMatcher<? extends IPatternMatch>> matchers; | 108 | public long getForwardTime() { |
109 | return context.getDesignSpaceManager().getForwardTime(); | ||
110 | } | ||
111 | public long getBacktrackingTime() { | ||
112 | return context.getDesignSpaceManager().getBacktrackingTime(); | ||
113 | } | ||
114 | |||
120 | @Override | 115 | @Override |
121 | public void initStrategy(ThreadContext context) { | 116 | public void initStrategy(ThreadContext context) { |
122 | this.context = context; | 117 | this.context = context; |
123 | this.solutionStore = context.getGlobalContext().getSolutionStore(); | 118 | this.solutionStore = context.getGlobalContext().getSolutionStore(); |
119 | solutionStore.registerSolutionFoundHandler(new ISolutionFoundHandler() { | ||
120 | |||
121 | @Override | ||
122 | public void solutionTriedToSave(ThreadContext context, SolutionTrajectory trajectory) { | ||
123 | // Ignore. | ||
124 | } | ||
125 | |||
126 | @Override | ||
127 | public void solutionFound(ThreadContext context, SolutionTrajectory trajectory) { | ||
128 | configuration.progressMonitor.workedModelFound(configuration.solutionScope.numberOfRequiredSolutions); | ||
129 | saveTimes(); | ||
130 | logger.debug("Found a solution."); | ||
131 | } | ||
132 | }); | ||
133 | numericSolver.init(context); | ||
124 | 134 | ||
125 | // ViatraQueryEngine engine = context.getQueryEngine(); | 135 | // ViatraQueryEngine engine = context.getQueryEngine(); |
126 | // // TODO: visualisation | ||
127 | // matchers = new LinkedList<ViatraQueryMatcher<? extends IPatternMatch>>(); | 136 | // matchers = new LinkedList<ViatraQueryMatcher<? extends IPatternMatch>>(); |
128 | // for(IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> p : this.method.getAllPatterns()) { | 137 | // for(IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> p : this.method.getAllPatterns()) { |
129 | // //System.out.println(p.getSimpleName()); | 138 | // ViatraQueryMatcher<? extends IPatternMatch> matcher = p.getMatcher(engine); |
130 | // ViatraQueryMatcher<? extends IPatternMatch> matcher = p.getMatcher(engine); | ||
131 | // matchers.add(matcher); | ||
132 | // } | 139 | // } |
133 | 140 | // | |
134 | this.solutionStoreWithCopy = new SolutionStoreWithCopy(); | ||
135 | this.solutionStoreWithDiversityDescriptor = new SolutionStoreWithDiversityDescriptor(configuration.diversityRequirement); | ||
136 | |||
137 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 141 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
138 | this.comparator = new Comparator<TrajectoryWithFitness>() { | 142 | this.comparator = new Comparator<TrajectoryWithFitness>() { |
139 | @Override | 143 | @Override |
@@ -141,23 +145,14 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
141 | return objectiveComparatorHelper.compare(o2.fitness, o1.fitness); | 145 | return objectiveComparatorHelper.compare(o2.fitness, o1.fitness); |
142 | } | 146 | } |
143 | }; | 147 | }; |
144 | |||
145 | this.numericSolver = new NumericSolver(context, method, this.configuration.runIntermediateNumericalConsistencyChecks, false); | ||
146 | 148 | ||
147 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); | 149 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); |
148 | } | 150 | } |
149 | 151 | ||
150 | @Override | 152 | @Override |
151 | public void explore() { | 153 | public void explore() { |
152 | // System.out.println("press enter"); | ||
153 | // try { | ||
154 | // new BufferedReader(new InputStreamReader(System.in)).readLine(); | ||
155 | // } catch (IOException e) { | ||
156 | // // TODO Auto-generated catch block | ||
157 | // e.printStackTrace(); | ||
158 | // } | ||
159 | this.explorationStarted=System.nanoTime(); | 154 | this.explorationStarted=System.nanoTime(); |
160 | if (!context.checkGlobalConstraints()) { | 155 | if (!checkGlobalConstraints()) { |
161 | logger.info("Global contraint is not satisifed in the first state. Terminate."); | 156 | logger.info("Global contraint is not satisifed in the first state. Terminate."); |
162 | return; | 157 | return; |
163 | } else if(!numericSolver.maySatisfiable()) { | 158 | } else if(!numericSolver.maySatisfiable()) { |
@@ -169,14 +164,13 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
169 | return; | 164 | return; |
170 | } | 165 | } |
171 | 166 | ||
172 | final Fitness firstFittness = context.calculateFitness(); | 167 | final Fitness firstFitness = calculateFitness(); |
173 | checkForSolution(firstFittness); | 168 | checkForSolution(firstFitness); |
174 | 169 | ||
175 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 170 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
176 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); | 171 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); |
177 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); | 172 | TrajectoryWithFitness currentTrajectoryWithFitness = new TrajectoryWithFitness(firstTrajectory, firstFitness); |
178 | trajectoiresToExplore.add(currentTrajectoryWithFittness); | 173 | trajectoiresToExplore.add(currentTrajectoryWithFitness); |
179 | |||
180 | //if(configuration) | 174 | //if(configuration) |
181 | visualiseCurrentState(); | 175 | visualiseCurrentState(); |
182 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { | 176 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { |
@@ -190,22 +184,22 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
190 | 184 | ||
191 | mainLoop: while (!isInterrupted && !configuration.progressMonitor.isCancelled()) { | 185 | mainLoop: while (!isInterrupted && !configuration.progressMonitor.isCancelled()) { |
192 | 186 | ||
193 | if (currentTrajectoryWithFittness == null) { | 187 | if (currentTrajectoryWithFitness == null) { |
194 | if (trajectoiresToExplore.isEmpty()) { | 188 | if (trajectoiresToExplore.isEmpty()) { |
195 | logger.debug("State space is fully traversed."); | 189 | logger.debug("State space is fully traversed."); |
196 | return; | 190 | return; |
197 | } else { | 191 | } else { |
198 | currentTrajectoryWithFittness = selectState(); | 192 | currentTrajectoryWithFitness = selectState(); |
199 | if (logger.isDebugEnabled()) { | 193 | if (logger.isDebugEnabled()) { |
200 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); | 194 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); |
201 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithFittness); | 195 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithFitness); |
202 | } | 196 | } |
203 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFittness.trajectory); | 197 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFitness.trajectory); |
204 | } | 198 | } |
205 | } | 199 | } |
206 | 200 | ||
207 | // visualiseCurrentState(); | 201 | // visualiseCurrentState(); |
208 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 202 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithfitness); |
209 | // if(consistencyCheckResult == true) { | 203 | // if(consistencyCheckResult == true) { |
210 | // continue mainLoop; | 204 | // continue mainLoop; |
211 | // } | 205 | // } |
@@ -215,29 +209,25 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
215 | 209 | ||
216 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && iterator.hasNext()) { | 210 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && iterator.hasNext()) { |
217 | final Object nextActivation = iterator.next(); | 211 | final Object nextActivation = iterator.next(); |
218 | // if (!iterator.hasNext()) { | ||
219 | // logger.debug("Last untraversed activation of the state."); | ||
220 | // trajectoiresToExplore.remove(currentTrajectoryWithFittness); | ||
221 | // } | ||
222 | logger.debug("Executing new activation: " + nextActivation); | 212 | logger.debug("Executing new activation: " + nextActivation); |
223 | context.executeAcitvationId(nextActivation); | 213 | context.executeAcitvationId(nextActivation); |
214 | method.getStatistics().incrementDecisionCount(); | ||
224 | 215 | ||
225 | visualiseCurrentState(); | 216 | visualiseCurrentState(); |
226 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { | 217 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { |
227 | // int c = matcher.countMatches(); | 218 | // int c = matcher.countMatches(); |
228 | // if(c>=100) { | 219 | // if(c>=1) { |
229 | // System.out.println(c+ " " +matcher.getPatternName()); | 220 | // System.out.println(c+ " " +matcher.getPatternName()); |
230 | // } | 221 | // } |
231 | // | ||
232 | // } | 222 | // } |
233 | 223 | ||
234 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 224 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFitness); |
235 | if(consistencyCheckResult == true) { continue mainLoop; } | 225 | if(consistencyCheckResult == true) { continue mainLoop; } |
236 | 226 | ||
237 | if (context.isCurrentStateAlreadyTraversed()) { | 227 | if (context.isCurrentStateAlreadyTraversed()) { |
238 | logger.info("The new state is already visited."); | 228 | logger.info("The new state is already visited."); |
239 | context.backtrack(); | 229 | context.backtrack(); |
240 | } else if (!context.checkGlobalConstraints()) { | 230 | } else if (!checkGlobalConstraints()) { |
241 | logger.debug("Global contraint is not satisifed."); | 231 | logger.debug("Global contraint is not satisifed."); |
242 | context.backtrack(); | 232 | context.backtrack(); |
243 | } else if (!numericSolver.maySatisfiable()) { | 233 | } else if (!numericSolver.maySatisfiable()) { |
@@ -252,76 +242,84 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
252 | continue; | 242 | continue; |
253 | } | 243 | } |
254 | 244 | ||
255 | TrajectoryWithFitness nextTrajectoryWithFittness = new TrajectoryWithFitness( | 245 | TrajectoryWithFitness nextTrajectoryWithfitness = new TrajectoryWithFitness( |
256 | context.getTrajectory().toArray(), nextFitness); | 246 | context.getTrajectory().toArray(), nextFitness); |
257 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | 247 | trajectoiresToExplore.add(nextTrajectoryWithfitness); |
258 | 248 | ||
259 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | 249 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFitness.fitness, |
260 | nextTrajectoryWithFittness.fitness); | 250 | nextTrajectoryWithfitness.fitness); |
261 | if (compare < 0) { | 251 | if (compare < 0) { |
262 | logger.debug("Better fitness, moving on: " + nextFitness); | 252 | logger.debug("Better fitness, moving on: " + nextFitness); |
263 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 253 | currentTrajectoryWithFitness = nextTrajectoryWithfitness; |
264 | continue mainLoop; | 254 | continue mainLoop; |
265 | } else if (compare == 0) { | 255 | } else if (compare == 0) { |
266 | logger.debug("Equally good fitness, moving on: " + nextFitness); | 256 | logger.debug("Equally good fitness, moving on: " + nextFitness); |
267 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 257 | currentTrajectoryWithFitness = nextTrajectoryWithfitness; |
268 | continue mainLoop; | 258 | continue mainLoop; |
269 | } else { | 259 | } else { |
270 | logger.debug("Worse fitness."); | 260 | logger.debug("Worse fitness."); |
271 | currentTrajectoryWithFittness = null; | 261 | currentTrajectoryWithFitness = null; |
272 | continue mainLoop; | 262 | continue mainLoop; |
273 | } | 263 | } |
274 | } | 264 | } |
275 | } | 265 | } |
276 | 266 | ||
277 | logger.debug("State is fully traversed."); | 267 | logger.debug("State is fully traversed."); |
278 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); | 268 | trajectoiresToExplore.remove(currentTrajectoryWithFitness); |
279 | currentTrajectoryWithFittness = null; | 269 | currentTrajectoryWithFitness = null; |
280 | 270 | ||
281 | } | 271 | } |
282 | logger.info("Interrupted."); | 272 | logger.info("Interrupted."); |
283 | } | 273 | } |
284 | 274 | ||
275 | private boolean checkGlobalConstraints() { | ||
276 | long start = System.nanoTime(); | ||
277 | boolean result = context.checkGlobalConstraints(); | ||
278 | globalConstraintEvaluationTime += System.nanoTime() - start; | ||
279 | return result; | ||
280 | } | ||
281 | |||
282 | private Fitness calculateFitness() { | ||
283 | long start = System.nanoTime(); | ||
284 | Fitness fitness = context.calculateFitness(); | ||
285 | fitnessCalculationTime += System.nanoTime() - start; | ||
286 | return fitness; | ||
287 | } | ||
288 | |||
285 | private List<Object> selectActivation() { | 289 | private List<Object> selectActivation() { |
286 | List<Object> activationIds; | 290 | List<Object> activationIds; |
287 | try { | 291 | try { |
288 | activationIds = this.activationSelector.randomizeActivationIDs(context.getUntraversedActivationIds()); | 292 | activationIds = this.activationSelector.randomizeActivationIDs(context.getUntraversedActivationIds()); |
289 | } catch (NullPointerException e) { | 293 | } catch (NullPointerException e) { |
294 | // logger.warn("Unexpected state code: " + context.getDesignSpaceManager().getCurrentState()); | ||
290 | numberOfStatecoderFail++; | 295 | numberOfStatecoderFail++; |
291 | activationIds = Collections.emptyList(); | 296 | activationIds = Collections.emptyList(); |
292 | } | 297 | } |
293 | return activationIds; | 298 | return activationIds; |
294 | } | 299 | } |
295 | 300 | ||
296 | private void checkForSolution(final Fitness fittness) { | 301 | private void checkForSolution(final Fitness fitness) { |
297 | if (fittness.isSatisifiesHardObjectives()) { | 302 | solutionStore.newSolution(context); |
298 | if (solutionStoreWithDiversityDescriptor.isDifferent(context)) { | ||
299 | if(numericSolver.currentSatisfiable()) { | ||
300 | Map<EObject, EObject> trace = solutionStoreWithCopy.newSolution(context); | ||
301 | numericSolver.fillSolutionCopy(trace); | ||
302 | solutionStoreWithDiversityDescriptor.newSolution(context); | ||
303 | solutionStore.newSolution(context); | ||
304 | configuration.progressMonitor.workedModelFound(configuration.solutionScope.numberOfRequiredSolution); | ||
305 | saveTimes(); | ||
306 | logger.debug("Found a solution."); | ||
307 | } | ||
308 | } | ||
309 | } | ||
310 | } | 303 | } |
304 | |||
311 | public List<String> times = new LinkedList<String>(); | 305 | public List<String> times = new LinkedList<String>(); |
312 | private void saveTimes() { | 306 | private void saveTimes() { |
313 | long statecoderTime = ((NeighbourhoodBasedPartialInterpretationStateCoder)this.context.getStateCoder()).getStatecoderRuntime()/1000000; | 307 | long forwardTime = context.getDesignSpaceManager().getForwardTime()/1000000; |
314 | long solutionCopy = solutionStoreWithCopy.getSumRuntime()/1000000; | 308 | long backtrackingTime = context.getDesignSpaceManager().getBacktrackingTime()/1000000; |
315 | long activationSelection = this.activationSelector.getRuntime()/1000000; | 309 | long activationSelection = this.activationSelector.getRuntime()/1000000; |
310 | long solutionCopierTime = this.solutionSaver.getTotalCopierRuntime()/1000000; | ||
316 | long numericalSolverSumTime = this.numericSolver.getRuntime()/1000000; | 311 | long numericalSolverSumTime = this.numericSolver.getRuntime()/1000000; |
317 | long numericalSolverProblemForming = this.numericSolver.getSolverSolvingProblem()/1000000; | 312 | long numericalSolverProblemForming = this.numericSolver.getSolverSolvingProblem()/1000000; |
318 | long numericalSolverSolving = this.numericSolver.getSolverSolvingProblem()/1000000; | 313 | long numericalSolverSolving = this.numericSolver.getSolverSolvingProblem()/1000000; |
319 | long numericalSolverInterpreting = this.numericSolver.getSolverSolution()/1000000; | 314 | long numericalSolverInterpreting = this.numericSolver.getSolverSolution()/1000000; |
320 | this.times.add( | 315 | this.times.add( |
321 | "(TransformationExecutionTime"+method.getStatistics().transformationExecutionTime/1000000+ | 316 | "(TransformationExecutionTime"+method.getStatistics().transformationExecutionTime/1000000+ |
322 | "|StateCoderTime:"+statecoderTime+ | 317 | "|ForwardTime:"+forwardTime+ |
323 | "|SolutionCopyTime:"+solutionCopy+ | 318 | "|Backtrackingtime:"+backtrackingTime+ |
319 | "|GlobalConstraintEvaluationTime:"+(globalConstraintEvaluationTime/1000000)+ | ||
320 | "|FitnessCalculationTime:"+(fitnessCalculationTime/1000000)+ | ||
324 | "|ActivationSelectionTime:"+activationSelection+ | 321 | "|ActivationSelectionTime:"+activationSelection+ |
322 | "|SolutionCopyTime:"+solutionCopierTime+ | ||
325 | "|NumericalSolverSumTime:"+numericalSolverSumTime+ | 323 | "|NumericalSolverSumTime:"+numericalSolverSumTime+ |
326 | "|NumericalSolverProblemFormingTime:"+numericalSolverProblemForming+ | 324 | "|NumericalSolverProblemFormingTime:"+numericalSolverProblemForming+ |
327 | "|NumericalSolverSolvingTime:"+numericalSolverSolving+ | 325 | "|NumericalSolverSolvingTime:"+numericalSolverSolving+ |
@@ -355,16 +353,19 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
355 | } else { | 353 | } else { |
356 | return trajectoiresToExplore.element(); | 354 | return trajectoiresToExplore.element(); |
357 | } | 355 | } |
358 | } | 356 | } |
359 | 357 | ||
360 | public void visualiseCurrentState() { | 358 | public void visualiseCurrentState() { |
361 | PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugCongiguration.partialInterpretatioVisualiser; | 359 | PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugConfiguration.partialInterpretatioVisualiser; |
362 | if(partialInterpretatioVisualiser != null && this.configuration.documentationLevel == DocumentationLevel.FULL && workspace != null) { | 360 | if(partialInterpretatioVisualiser != null && this.configuration.documentationLevel == DocumentationLevel.FULL && workspace != null) { |
363 | PartialInterpretation p = (PartialInterpretation) (context.getModel()); | 361 | PartialInterpretation p = (PartialInterpretation) (context.getModel()); |
364 | int id = ++numberOfPrintedModel; | 362 | int id = ++numberOfPrintedModel; |
365 | if (id % configuration.debugCongiguration.partalInterpretationVisualisationFrequency == 0) { | 363 | if (id % configuration.debugConfiguration.partalInterpretationVisualisationFrequency == 0) { |
366 | PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p); | 364 | PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p); |
367 | visualisation.writeToFile(workspace, String.format("state%09d.png", id)); | 365 | logger.debug("Visualizing state: " + id + " (" + context.getDesignSpaceManager().getCurrentState() + ")"); |
366 | String name = String.format("state%09d", id); | ||
367 | visualisation.writeToFile(workspace, name + ".png"); | ||
368 | workspace.writeModel((EObject) context.getModel(), name + ".xmi"); | ||
368 | } | 369 | } |
369 | } | 370 | } |
370 | } | 371 | } |