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