aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java
blob: b76598fb3f2a50c831a61d4377dedd401bdd2bcf (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
/*******************************************************************************
 * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro
 * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.store.dse.objectives;

import tools.refinery.store.dse.DesignSpaceExplorationAdapter;

import java.util.Comparator;
import java.util.Objects;

/**
 * This abstract class implements the basic functionality of an objective ({@link Objective} namely its name,
 * comparator, level and fitness hard constraint.
 *
 * @author Andras Szabolcs Nagy
 *
 */
public abstract class BaseObjective implements Objective {

	protected final String name;
	protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER;

	protected double fitnessConstraint;
	protected boolean isThereFitnessConstraint = false;
	protected Comparator<Double> fitnessConstraintComparator;

	protected BaseObjective(String name) {
		Objects.requireNonNull(name, "Name of the objective cannot be null.");
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setComparator(Comparator<Double> comparator) {
		this.comparator = comparator;
	}

	@Override
	public Comparator<Double> getComparator() {
		return comparator;
	}

	public BaseObjective withComparator(Comparator<Double> comparator) {
		setComparator(comparator);
		return this;
	}

	/**
	 * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the
	 * current state as a solution.
	 *
	 * @param fitnessConstraint
	 *            Solutions should be better than this value.
	 * @param fitnessConstraintComparator
	 *            {@link Comparator} to determine if the current state is better than the given value.
	 * @return The actual instance to enable builder pattern like usage.
	 */
	public BaseObjective withHardConstraintOnFitness(double fitnessConstraint,
													 Comparator<Double> fitnessConstraintComparator) {
		this.fitnessConstraint = fitnessConstraint;
		this.fitnessConstraintComparator = fitnessConstraintComparator;
		this.isThereFitnessConstraint = true;
		return this;
	}

	/**
	 * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the
	 * current state as a solution. The provided comparator will be used.
	 *
	 * @param fitnessConstraint
	 *            Solutions should be better than this value.
	 * @return The actual instance to enable builder pattern like usage.
	 */
	public BaseObjective withHardConstraintOnFitness(double fitnessConstraint) {
		return withHardConstraintOnFitness(fitnessConstraint, null);
	}

	@Override
	public void init(DesignSpaceExplorationAdapter context) {
		if (fitnessConstraintComparator == null) {
			fitnessConstraintComparator = comparator;
		}
	}

	@Override
	public boolean isHardObjective() {
		return isThereFitnessConstraint;
	}

	@Override
	public boolean satisfiesHardObjective(Double fitness) {
		if (isThereFitnessConstraint) {
			int compare = fitnessConstraintComparator.compare(fitness, fitnessConstraint);
			if (compare < 0) {
				return false;
			}
		}
		return true;
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj instanceof BaseObjective baseObjective) {
			return name.equals(baseObjective.getName());
		}
		return false;
	}

	@Override
	public String toString() {
		return name;
	}

}