diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java new file mode 100644 index 00000000..24e3280d --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java | |||
@@ -0,0 +1,131 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.store.dse.objectives; | ||
10 | |||
11 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
12 | |||
13 | import java.util.Comparator; | ||
14 | import java.util.Objects; | ||
15 | |||
16 | /** | ||
17 | * This abstract class implements the basic functionality of an objective ({@link Objective} namely its name, | ||
18 | * comparator, level and fitness hard constraint. | ||
19 | * | ||
20 | * @author Andras Szabolcs Nagy | ||
21 | * | ||
22 | */ | ||
23 | public abstract class BaseObjective implements Objective { | ||
24 | |||
25 | protected final String name; | ||
26 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; | ||
27 | |||
28 | protected double fitnessConstraint; | ||
29 | protected boolean isThereFitnessConstraint = false; | ||
30 | protected Comparator<Double> fitnessConstraintComparator; | ||
31 | |||
32 | public BaseObjective(String name) { | ||
33 | Objects.requireNonNull(name, "Name of the objective cannot be null."); | ||
34 | this.name = name; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public String getName() { | ||
39 | return name; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public void setComparator(Comparator<Double> comparator) { | ||
44 | this.comparator = comparator; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public Comparator<Double> getComparator() { | ||
49 | return comparator; | ||
50 | } | ||
51 | |||
52 | public BaseObjective withComparator(Comparator<Double> comparator) { | ||
53 | setComparator(comparator); | ||
54 | return this; | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
59 | * current state as a solution. | ||
60 | * | ||
61 | * @param fitnessConstraint | ||
62 | * Solutions should be better than this value. | ||
63 | * @param fitnessConstraintComparator | ||
64 | * {@link Comparator} to determine if the current state is better than the given value. | ||
65 | * @return The actual instance to enable builder pattern like usage. | ||
66 | */ | ||
67 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint, | ||
68 | Comparator<Double> fitnessConstraintComparator) { | ||
69 | this.fitnessConstraint = fitnessConstraint; | ||
70 | this.fitnessConstraintComparator = fitnessConstraintComparator; | ||
71 | this.isThereFitnessConstraint = true; | ||
72 | return this; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
77 | * current state as a solution. The provided comparator will be used. | ||
78 | * | ||
79 | * @param fitnessConstraint | ||
80 | * Solutions should be better than this value. | ||
81 | * @return The actual instance to enable builder pattern like usage. | ||
82 | */ | ||
83 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint) { | ||
84 | return withHardConstraintOnFitness(fitnessConstraint, null); | ||
85 | } | ||
86 | |||
87 | @Override | ||
88 | public void init(DesignSpaceExplorationAdapter context) { | ||
89 | if (fitnessConstraintComparator == null) { | ||
90 | fitnessConstraintComparator = comparator; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public boolean isHardObjective() { | ||
96 | return isThereFitnessConstraint; | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public boolean satisfiesHardObjective(Double fitness) { | ||
101 | if (isThereFitnessConstraint) { | ||
102 | int compare = fitnessConstraintComparator.compare(fitness, fitnessConstraint); | ||
103 | if (compare < 0) { | ||
104 | return false; | ||
105 | } | ||
106 | } | ||
107 | return true; | ||
108 | } | ||
109 | |||
110 | @Override | ||
111 | public int hashCode() { | ||
112 | return name.hashCode(); | ||
113 | } | ||
114 | |||
115 | @Override | ||
116 | public boolean equals(Object obj) { | ||
117 | if (this == obj) { | ||
118 | return true; | ||
119 | } | ||
120 | if (obj instanceof BaseObjective baseObjective) { | ||
121 | return name.equals(baseObjective.getName()); | ||
122 | } | ||
123 | return false; | ||
124 | } | ||
125 | |||
126 | @Override | ||
127 | public String toString() { | ||
128 | return name; | ||
129 | } | ||
130 | |||
131 | } | ||