aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java')
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java137
1 files changed, 137 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java
new file mode 100644
index 00000000..cc48d22e
--- /dev/null
+++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java
@@ -0,0 +1,137 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2014, Miklos Foldenyi, 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Objects;
14
15import org.eclipse.viatra.dse.base.ThreadContext;
16import org.eclipse.viatra.dse.objectives.IObjective;
17import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
18
19/**
20 * This objective collects a list of other objectives. It returns the weighted sum of the objectives.
21 *
22 * @author Andras Szabolcs Nagy
23 *
24 */
25public class CompositeObjective extends BaseObjective {
26
27 public static final String DEFAULT_NAME = "CompositeObjective";
28 protected List<IObjective> objectives;
29 protected List<Double> weights;
30 protected boolean hardObjective;
31
32 public CompositeObjective(String name, List<IObjective> objectives, List<Double> weights) {
33 super(name);
34 Objects.requireNonNull(objectives, "The list of objectives cannot be null.");
35 Objects.requireNonNull(weights, "The list of weights cannot be null.");
36 Preconditions.checkState(objectives.size() == weights.size(), "The size of the objectives and weights must match.");
37 this.objectives = objectives;
38 this.weights = weights;
39 }
40
41 public CompositeObjective(List<IObjective> objectives, List<Double> weights) {
42 this(DEFAULT_NAME, objectives, weights);
43 }
44
45 public CompositeObjective(String name) {
46 this(name, new ArrayList<IObjective>(), new ArrayList<Double>());
47 }
48
49 public CompositeObjective() {
50 this(DEFAULT_NAME, new ArrayList<IObjective>(), new ArrayList<Double>());
51 }
52
53 /**
54 * Adds a new objective.
55 *
56 * @param objective
57 * @return The actual instance to enable builder pattern like usage.
58 */
59 public CompositeObjective withObjective(IObjective objective) {
60 objectives.add(objective);
61 weights.add(1d);
62 return this;
63 }
64
65 /**
66 * Adds a new objective.
67 *
68 * @param objective
69 * @return The actual instance to enable builder pattern like usage.
70 */
71 public CompositeObjective withObjective(IObjective objective, double weight) {
72 objectives.add(objective);
73 weights.add(weight);
74 return this;
75 }
76
77 @Override
78 public Double getFitness(ThreadContext context) {
79
80 double result = 0;
81
82 for (int i = 0; i < objectives.size(); i++) {
83 IObjective objective = objectives.get(i);
84 Double weight = weights.get(i);
85 result += objective.getFitness(context) * weight;
86 }
87 return result;
88 }
89
90 @Override
91 public void init(ThreadContext context) {
92 super.init(context);
93 hardObjective = false;
94 for (IObjective objective : objectives) {
95 objective.init(context);
96 if (objective.isHardObjective()) {
97 hardObjective = true;
98 }
99 }
100 }
101
102 @Override
103 public IObjective createNew() {
104
105 List<IObjective> newObjectives = new ArrayList<IObjective>();
106
107 for (IObjective objective : objectives) {
108 newObjectives.add(objective.createNew());
109 }
110
111 CompositeObjective objective = new CompositeObjective(name, newObjectives, weights);
112 if (isThereFitnessConstraint) {
113 objective.withHardConstraintOnFitness(fitnessConstraint, fitnessConstraintComparator);
114 }
115
116 return objective.withComparator(comparator).withLevel(level);
117 }
118
119 @Override
120 public boolean isHardObjective() {
121 return hardObjective;
122 }
123
124 @Override
125 public boolean satisifiesHardObjective(Double fitness) {
126
127 boolean hardObjectiveSatisfied = true;
128
129 for (IObjective objective : objectives) {
130 hardObjectiveSatisfied = objective.satisifiesHardObjective(fitness) ? hardObjectiveSatisfied : false;
131 }
132
133 hardObjectiveSatisfied = super.satisifiesHardObjective(fitness) ? hardObjectiveSatisfied : false;
134
135 return hardObjectiveSatisfied;
136 }
137}