aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java
blob: 25ff45aef723414ed919012758f3e23f276d69a5 (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
/*******************************************************************************
 * Copyright (c) 2010-2014, Miklos Foldenyi, 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import org.eclipse.viatra.dse.base.DesignSpaceManager;
import org.eclipse.viatra.dse.base.ThreadContext;
import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo;
import org.eclipse.viatra.dse.objectives.ActivationFitnessProcessor;
import org.eclipse.viatra.dse.objectives.Comparators;
import org.eclipse.viatra.dse.objectives.IObjective;
import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule;

/**
 * This soft objective calculates a fitness value based on the length of the trajectory. Costs to the rules can be
 * assigned.
 * 
 * @author Andras Szabolcs Nagy
 *
 */
public class TrajectoryCostSoftObjective extends BaseObjective {

    public static final String DEFAULT_NAME = "TrajectoryCostObjective";
    protected Map<BatchTransformationRule<?, ?>, Double> fixCosts;
    protected Map<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> activationCostProcessors;
    protected double trajectoryLengthWeight = 0.0;
    protected boolean calculateTrajectoryLengthWeight;

    public TrajectoryCostSoftObjective(String name) {
        super(name);
        comparator = Comparators.LOWER_IS_BETTER;
    }

    public TrajectoryCostSoftObjective() {
        this(DEFAULT_NAME);
    }

    /**
     * Sets the cost of a rule.
     * 
     * @param rule
     * @param cost
     * @return The actual instance to enable builder pattern like usage.
     */
    public TrajectoryCostSoftObjective withRuleCost(BatchTransformationRule<?, ?> rule, double cost) {
        Objects.requireNonNull(rule);
        if (fixCosts == null) {
            fixCosts = new HashMap<BatchTransformationRule<?, ?>, Double>();
        }
        Preconditions.checkArgument(!fixCosts.containsKey(rule));
        fixCosts.put(rule, cost);
        return this;
    }

    /**
     * Sets an activation processor for a rule.
     * 
     * @param rule
     * @param activationCostProcessor
     * @return The actual instance to enable builder pattern like usage.
     */
    public TrajectoryCostSoftObjective withActivationCost(BatchTransformationRule<?, ?> rule,
            ActivationFitnessProcessor activationCostProcessor) {
        Objects.requireNonNull(rule);
        Objects.requireNonNull(activationCostProcessor);
        if (activationCostProcessors == null) {
            activationCostProcessors = new HashMap<BatchTransformationRule<?, ?>, ActivationFitnessProcessor>();
        }
        Preconditions.checkArgument(!activationCostProcessors.containsKey(rule));
        activationCostProcessors.put(rule, activationCostProcessor);
        return this;
    }

    /**
     * The length of the trajectory multiplied with given parameter will be added to the fitness value.
     * 
     * @param trajectoryLengthWeight
     *            The weight of a transformation rule application.
     * @return The actual instance to enable builder pattern like usage.
     */
    public TrajectoryCostSoftObjective withTrajectoryLengthWeight(double trajectoryLengthWeight) {
        this.trajectoryLengthWeight = trajectoryLengthWeight;
        this.calculateTrajectoryLengthWeight = true;
        return this;
    }

    @Override
    public Double getFitness(ThreadContext context) {

        DesignSpaceManager dsm = context.getDesignSpaceManager();
        TrajectoryInfo trajectoryInfo = dsm.getTrajectoryInfo();
        List<Object> trajectory = trajectoryInfo.getTrajectory();
        List<BatchTransformationRule<?, ?>> rules = trajectoryInfo.getRules();

        double result = 0;

        for (int i = 0; i < trajectory.size(); i++) {
            BatchTransformationRule<?, ?> rule = rules.get(i);

            Double cost = fixCosts.get(rule);
            if (cost != null) {
                result += cost;
            }

            Map<String, Double> costs = trajectoryInfo.getMeasuredCosts().get(i);
            if (costs != null) {
                cost = costs.get(name);
                if (cost != null) {
                    result += cost;
                }
            }
        }

        if (calculateTrajectoryLengthWeight) {
            result += trajectory.size() * trajectoryLengthWeight;
        }

        return result;
    }

    @Override
    public void init(ThreadContext context) {
        super.init(context);
        DesignSpaceManager dsm = context.getDesignSpaceManager();
        if (activationCostProcessors != null) {
            for (Entry<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> entry : activationCostProcessors.entrySet()) {
                dsm.registerActivationCostProcessor(name, entry.getKey(), entry.getValue());
            }
        }
    }

    @Override
    public IObjective createNew() {
        return this;
    }
}