aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/DirectionalThresholdObjective.xtend
blob: 376e3d1a9739e27da1cdda87393e457c2f60b793 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization

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

abstract class ObjectiveThreshold {
	public static val NO_THRESHOLD = new ObjectiveThreshold {
		override isHard() {
			false
		}

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

		override protected postProcessSatisfactoryCost(double cost, ObjectiveKind kind) {
			cost
		}

		override ObjectiveThreshold merge(ObjectiveThreshold other) {
			if (other == NO_THRESHOLD) {
				NO_THRESHOLD
			} else {
				throw new IllegalArgumentException("Merged thresholds must have the same type")
			}
		}
	}

	private new() {
	}

	def boolean isHard() {
		true
	}

	def boolean satisfiesThreshold(double cost, ObjectiveKind kind) {
		satisfiesThreshold(cost, kind.comparator)
	}

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

	def double postProcessCost(double cost, ObjectiveKind kind) {
		if (satisfiesThreshold(cost, kind)) {
			postProcessSatisfactoryCost(cost, kind)
		} else {
			cost
		}
	}

	protected def double postProcessSatisfactoryCost(double cost, ObjectiveKind kind)

	def ObjectiveThreshold merge(ObjectiveThreshold other)

	@Data
	static class Exclusive extends ObjectiveThreshold {
		static val EPSILON = 0.1

		val double threshold
		val boolean clampToThreshold

		@FinalFieldsConstructor
		new() {
		}

		new(double threshold) {
			this(threshold, true)
		}

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

		override protected postProcessSatisfactoryCost(double cost, ObjectiveKind kind) {
			if (clampToThreshold) {
				threshold + Math.signum(kind.satisfiedValue) * EPSILON
			} else {
				cost
			}
		}

		override ObjectiveThreshold merge(ObjectiveThreshold other) {
			if (other instanceof Exclusive) {
				new Exclusive(threshold + other.threshold)
			} else {
				throw new IllegalArgumentException("Merged thresholds must have the same type")
			}
		}
	}

	@Data
	static class Inclusive extends ObjectiveThreshold {
		val double threshold
		val boolean clampToThreshold

		@FinalFieldsConstructor
		new() {
		}

		new(double threshold) {
			this(threshold, true)
		}

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

		override protected postProcessSatisfactoryCost(double cost, ObjectiveKind kind) {
			if (clampToThreshold) {
				threshold
			} else {
				cost
			}
		}
		
		override ObjectiveThreshold merge(ObjectiveThreshold other) {
			if (other instanceof Inclusive) {
				new Inclusive(threshold + other.threshold)
			} else {
				throw new IllegalArgumentException("Merged thresholds must have the same type")
			}
		}
	}
}

abstract class DirectionalThresholdObjective implements IObjective {
	@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
	}

	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)
	}

	override getFitness(ThreadContext context) {
		val fitness = getRawFitness(context)
		threshold.postProcessCost(fitness, kind)
	}

	protected def double getRawFitness(ThreadContext context)
}