aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/DseUtils.xtend
blob: 3c2e33190535bc2ad6803d76875f20ec182d7ef2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse

import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.IThreeValuedObjective
import org.eclipse.viatra.dse.base.ThreadContext
import org.eclipse.viatra.dse.objectives.Comparators
import org.eclipse.viatra.dse.objectives.Fitness
import org.eclipse.viatra.dse.objectives.IObjective

final class DseUtils {
	private new() {
		throw new IllegalStateException("This is a static utility class and should not be instantiated directly.")
	}

	static def calculateFitness(ThreadContext it, (IObjective)=>Double getFitness) {
		val result = new Fitness
		var boolean satisifiesHardObjectives = true
		for (objective : objectives) {
			val fitness = getFitness.apply(objective)
			result.put(objective.name, fitness)
			if (objective.isHardObjective() && !objective.satisifiesHardObjective(fitness)) {
				satisifiesHardObjectives = false
			}
		}
		result.satisifiesHardObjectives = satisifiesHardObjectives
		result
	}

	static def caclulateBestPossibleFitness(ThreadContext threadContext) {
		threadContext.calculateFitness [ objective |
			if (objective instanceof IThreeValuedObjective) {
				objective.getBestPossibleFitness(threadContext)
			} else {
				switch (objective.comparator) {
					case Comparators.LOWER_IS_BETTER:
						Double.NEGATIVE_INFINITY
					case Comparators.HIGHER_IS_BETTER:
						Double.POSITIVE_INFINITY
					case Comparators.DIFFERENCE_TO_ZERO_IS_BETTER:
						0.0
					default:
						throw new IllegalArgumentException("Unknown comparator for non-three-valued objective: " +
							objective.name)
				}
			}
		]
	}

	static def caclulateWorstPossibleFitness(ThreadContext threadContext) {
		threadContext.calculateFitness [ objective |
			if (objective instanceof IThreeValuedObjective) {
				objective.getWorstPossibleFitness(threadContext)
			} else {
				switch (objective.comparator) {
					case Comparators.LOWER_IS_BETTER,
					case Comparators.DIFFERENCE_TO_ZERO_IS_BETTER:
						Double.POSITIVE_INFINITY
					case Comparators.HIGHER_IS_BETTER:
						Double.NEGATIVE_INFINITY
					default:
						throw new IllegalArgumentException("Unknown comparator for non-three-valued objective: " +
							objective.name)
				}
			}
		]
	}
}