aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java')
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java148
1 files changed, 148 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java
new file mode 100644
index 00000000..25ff45ae
--- /dev/null
+++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java
@@ -0,0 +1,148 @@
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.HashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.Map.Entry;
15import java.util.Objects;
16
17import org.eclipse.viatra.dse.base.DesignSpaceManager;
18import org.eclipse.viatra.dse.base.ThreadContext;
19import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo;
20import org.eclipse.viatra.dse.objectives.ActivationFitnessProcessor;
21import org.eclipse.viatra.dse.objectives.Comparators;
22import org.eclipse.viatra.dse.objectives.IObjective;
23import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
24import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule;
25
26/**
27 * This soft objective calculates a fitness value based on the length of the trajectory. Costs to the rules can be
28 * assigned.
29 *
30 * @author Andras Szabolcs Nagy
31 *
32 */
33public class TrajectoryCostSoftObjective extends BaseObjective {
34
35 public static final String DEFAULT_NAME = "TrajectoryCostObjective";
36 protected Map<BatchTransformationRule<?, ?>, Double> fixCosts;
37 protected Map<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> activationCostProcessors;
38 protected double trajectoryLengthWeight = 0.0;
39 protected boolean calculateTrajectoryLengthWeight;
40
41 public TrajectoryCostSoftObjective(String name) {
42 super(name);
43 comparator = Comparators.LOWER_IS_BETTER;
44 }
45
46 public TrajectoryCostSoftObjective() {
47 this(DEFAULT_NAME);
48 }
49
50 /**
51 * Sets the cost of a rule.
52 *
53 * @param rule
54 * @param cost
55 * @return The actual instance to enable builder pattern like usage.
56 */
57 public TrajectoryCostSoftObjective withRuleCost(BatchTransformationRule<?, ?> rule, double cost) {
58 Objects.requireNonNull(rule);
59 if (fixCosts == null) {
60 fixCosts = new HashMap<BatchTransformationRule<?, ?>, Double>();
61 }
62 Preconditions.checkArgument(!fixCosts.containsKey(rule));
63 fixCosts.put(rule, cost);
64 return this;
65 }
66
67 /**
68 * Sets an activation processor for a rule.
69 *
70 * @param rule
71 * @param activationCostProcessor
72 * @return The actual instance to enable builder pattern like usage.
73 */
74 public TrajectoryCostSoftObjective withActivationCost(BatchTransformationRule<?, ?> rule,
75 ActivationFitnessProcessor activationCostProcessor) {
76 Objects.requireNonNull(rule);
77 Objects.requireNonNull(activationCostProcessor);
78 if (activationCostProcessors == null) {
79 activationCostProcessors = new HashMap<BatchTransformationRule<?, ?>, ActivationFitnessProcessor>();
80 }
81 Preconditions.checkArgument(!activationCostProcessors.containsKey(rule));
82 activationCostProcessors.put(rule, activationCostProcessor);
83 return this;
84 }
85
86 /**
87 * The length of the trajectory multiplied with given parameter will be added to the fitness value.
88 *
89 * @param trajectoryLengthWeight
90 * The weight of a transformation rule application.
91 * @return The actual instance to enable builder pattern like usage.
92 */
93 public TrajectoryCostSoftObjective withTrajectoryLengthWeight(double trajectoryLengthWeight) {
94 this.trajectoryLengthWeight = trajectoryLengthWeight;
95 this.calculateTrajectoryLengthWeight = true;
96 return this;
97 }
98
99 @Override
100 public Double getFitness(ThreadContext context) {
101
102 DesignSpaceManager dsm = context.getDesignSpaceManager();
103 TrajectoryInfo trajectoryInfo = dsm.getTrajectoryInfo();
104 List<Object> trajectory = trajectoryInfo.getTrajectory();
105 List<BatchTransformationRule<?, ?>> rules = trajectoryInfo.getRules();
106
107 double result = 0;
108
109 for (int i = 0; i < trajectory.size(); i++) {
110 BatchTransformationRule<?, ?> rule = rules.get(i);
111
112 Double cost = fixCosts.get(rule);
113 if (cost != null) {
114 result += cost;
115 }
116
117 Map<String, Double> costs = trajectoryInfo.getMeasuredCosts().get(i);
118 if (costs != null) {
119 cost = costs.get(name);
120 if (cost != null) {
121 result += cost;
122 }
123 }
124 }
125
126 if (calculateTrajectoryLengthWeight) {
127 result += trajectory.size() * trajectoryLengthWeight;
128 }
129
130 return result;
131 }
132
133 @Override
134 public void init(ThreadContext context) {
135 super.init(context);
136 DesignSpaceManager dsm = context.getDesignSpaceManager();
137 if (activationCostProcessors != null) {
138 for (Entry<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> entry : activationCostProcessors.entrySet()) {
139 dsm.registerActivationCostProcessor(name, entry.getKey(), entry.getValue());
140 }
141 }
142 }
143
144 @Override
145 public IObjective createNew() {
146 return this;
147 }
148}