aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/AbstractThreeValuedObjective.xtend
blob: 241bef2aff708fe632c7f43eef0278a545fc0e92 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization

import java.util.Comparator
import org.eclipse.viatra.dse.base.ThreadContext
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data

abstract class ObjectiveThreshold {
	public static val NO_THRESHOLD = new hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveThreshold {
		override isHard() {
			false
		}

		override satisfiesThreshold(double cost, Comparator<Double> comparator) {
			true
		}
	}

	private new() {
	}

	def boolean isHard() {
		true
	}

	def boolean satisfiesThreshold(double cost, Comparator<Double> comparator)

	@Data
	static class Exclusive extends ObjectiveThreshold {
		val double threshold

		override satisfiesThreshold(double cost, Comparator<Double> comparator) {
			comparator.compare(threshold, cost) > 0
		}
	}

	@Data
	static class Inclusive extends ObjectiveThreshold {
		val double threshold

		override satisfiesThreshold(double cost, Comparator<Double> comparator) {
			comparator.compare(threshold, cost) >= 0
		}
	}
}

abstract class AbstractThreeValuedObjective implements IThreeValuedObjective {
	@Accessors val String name
	@Accessors ObjectiveKind kind
	@Accessors ObjectiveThreshold threshold
	@Accessors int level

	protected new(String name, ObjectiveKind kind, ObjectiveThreshold threshold, int level) {
		this.name = name
		this.kind = kind
		this.threshold = threshold
		this.level = level
	}

	abstract def double getLowestPossibleFitness(ThreadContext threadContext)

	abstract def double getHighestPossibleFitness(ThreadContext threadContext)

	override getWorstPossibleFitness(ThreadContext threadContext) {
		switch (kind) {
			case LOWER_IS_BETTER:
				getHighestPossibleFitness(threadContext)
			case HIGHER_IS_BETTER:
				getLowestPossibleFitness(threadContext)
			default:
				throw new IllegalStateException("Unknown three valued objective kind: " + kind)
		}
	}

	override getBestPossibleFitness(ThreadContext threadContext) {
		switch (kind) {
			case LOWER_IS_BETTER:
				getLowestPossibleFitness(threadContext)
			case HIGHER_IS_BETTER:
				getHighestPossibleFitness(threadContext)
			default:
				throw new IllegalStateException("Unknown three valued objective kind: " + kind)
		}
	}

	override isHardObjective() {
		threshold.hard
	}

	override satisifiesHardObjective(Double fitness) {
		threshold.satisfiesThreshold(fitness, comparator)
	}

	override getComparator() {
		kind.comparator
	}

	override setComparator(Comparator<Double> comparator) {
		kind = ObjectiveKind.fromComparator(comparator)
	}

}