aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java
blob: 0a1de875620188cf402048f5ca9791196ec04c41 (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
/*******************************************************************************
 * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro
 * 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 org.eclipse.viatra.dse.objectives.impl;

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

import org.eclipse.viatra.dse.base.ThreadContext;
import org.eclipse.viatra.dse.objectives.Comparators;
import org.eclipse.viatra.dse.objectives.IObjective;

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

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

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

    public 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;
    }

    @Override
    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public int getLevel() {
        return level;
    }

    public BaseObjective withLevel(int level) {
        setLevel(level);
        return this;
    }

    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(ThreadContext context) {
        if (fitnessConstraintComparator == null) {
            fitnessConstraintComparator = comparator;
        }   
    }

    @Override
    public boolean isHardObjective() {
        return isThereFitnessConstraint;
    }
    
    @Override
    public boolean satisifiesHardObjective(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 baseObjective = (BaseObjective) obj;
            return name.equals(baseObjective.getName());
        }
        return false;
    }

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

}