diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java new file mode 100644 index 00000000..0a1de875 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java | |||
@@ -0,0 +1,150 @@ | |||
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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | import java.util.Objects; | ||
13 | |||
14 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
15 | import org.eclipse.viatra.dse.objectives.Comparators; | ||
16 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
17 | |||
18 | /** | ||
19 | * This abstract class implements the basic functionality of an objective ({@link IObjective} namely its name, | ||
20 | * comparator, level and fitness hard constraint. | ||
21 | * | ||
22 | * @author Andras Szabolcs Nagy | ||
23 | * | ||
24 | */ | ||
25 | public abstract class BaseObjective implements IObjective { | ||
26 | |||
27 | protected final String name; | ||
28 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; | ||
29 | protected int level = 0; | ||
30 | |||
31 | protected double fitnessConstraint; | ||
32 | protected boolean isThereFitnessConstraint = false; | ||
33 | protected Comparator<Double> fitnessConstraintComparator; | ||
34 | |||
35 | public BaseObjective(String name) { | ||
36 | Objects.requireNonNull(name, "Name of the objective cannot be null."); | ||
37 | this.name = name; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public String getName() { | ||
42 | return name; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public void setComparator(Comparator<Double> comparator) { | ||
47 | this.comparator = comparator; | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public Comparator<Double> getComparator() { | ||
52 | return comparator; | ||
53 | } | ||
54 | |||
55 | @Override | ||
56 | public void setLevel(int level) { | ||
57 | this.level = level; | ||
58 | } | ||
59 | |||
60 | @Override | ||
61 | public int getLevel() { | ||
62 | return level; | ||
63 | } | ||
64 | |||
65 | public BaseObjective withLevel(int level) { | ||
66 | setLevel(level); | ||
67 | return this; | ||
68 | } | ||
69 | |||
70 | public BaseObjective withComparator(Comparator<Double> comparator) { | ||
71 | setComparator(comparator); | ||
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. | ||
78 | * | ||
79 | * @param fitnessConstraint | ||
80 | * Solutions should be better than this value. | ||
81 | * @param fitnessConstraintComparator | ||
82 | * {@link Comparator} to determine if the current state is better than the given value. | ||
83 | * @return The actual instance to enable builder pattern like usage. | ||
84 | */ | ||
85 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint, | ||
86 | Comparator<Double> fitnessConstraintComparator) { | ||
87 | this.fitnessConstraint = fitnessConstraint; | ||
88 | this.fitnessConstraintComparator = fitnessConstraintComparator; | ||
89 | this.isThereFitnessConstraint = true; | ||
90 | return this; | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
95 | * current state as a solution. The provided comparator will be used. | ||
96 | * | ||
97 | * @param fitnessConstraint | ||
98 | * Solutions should be better than this value. | ||
99 | * @return The actual instance to enable builder pattern like usage. | ||
100 | */ | ||
101 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint) { | ||
102 | return withHardConstraintOnFitness(fitnessConstraint, null); | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public void init(ThreadContext context) { | ||
107 | if (fitnessConstraintComparator == null) { | ||
108 | fitnessConstraintComparator = comparator; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | @Override | ||
113 | public boolean isHardObjective() { | ||
114 | return isThereFitnessConstraint; | ||
115 | } | ||
116 | |||
117 | @Override | ||
118 | public boolean satisifiesHardObjective(Double fitness) { | ||
119 | if (isThereFitnessConstraint) { | ||
120 | int compare = fitnessConstraintComparator.compare(fitness, fitnessConstraint); | ||
121 | if (compare < 0) { | ||
122 | return false; | ||
123 | } | ||
124 | } | ||
125 | return true; | ||
126 | } | ||
127 | |||
128 | @Override | ||
129 | public int hashCode() { | ||
130 | return name.hashCode(); | ||
131 | } | ||
132 | |||
133 | @Override | ||
134 | public boolean equals(Object obj) { | ||
135 | if (this == obj) { | ||
136 | return true; | ||
137 | } | ||
138 | if (obj instanceof BaseObjective) { | ||
139 | BaseObjective baseObjective = (BaseObjective) obj; | ||
140 | return name.equals(baseObjective.getName()); | ||
141 | } | ||
142 | return false; | ||
143 | } | ||
144 | |||
145 | @Override | ||
146 | public String toString() { | ||
147 | return name; | ||
148 | } | ||
149 | |||
150 | } | ||