aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/api/strategy/impl/HillClimbingStrategy.java
blob: 0ccb0668db85b0b6ad5ccbdb6abbea831c202fa8 (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
/*******************************************************************************
 * Copyright (c) 2010-2016, Andras Szabolcs Nagy, 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.api.strategy.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;
import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy;
import org.eclipse.viatra.dse.base.ThreadContext;
import org.eclipse.viatra.dse.objectives.Fitness;
import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper;
import org.eclipse.viatra.dse.objectives.TrajectoryFitness;

public class HillClimbingStrategy implements IStrategy {

    private AtomicBoolean isInterrupted = new AtomicBoolean(false);
    private ThreadContext context;

    private Logger logger = Logger.getLogger(IStrategy.class);

    private Random random = new Random();
    private double percentOfOpenedStates;
    private ObjectiveComparatorHelper objectiveComparatorHelper;

    public HillClimbingStrategy() {
        this(2);
    }

    public HillClimbingStrategy(double percentOfOpenedStates) {
        this.percentOfOpenedStates = percentOfOpenedStates;
    }

    @Override
    public void initStrategy(ThreadContext context) {
        this.context = context;
        objectiveComparatorHelper = context.getObjectiveComparatorHelper();
        logger.info("Hill climbing exploration strategy is initied.");
    }

    @Override
    public void explore() {

        boolean globalConstraintsAreSatisfied = context.checkGlobalConstraints();
        if (!globalConstraintsAreSatisfied) {
            boolean isSuccessfulUndo = context.backtrack();
            if (!isSuccessfulUndo) {
                logger.info("Global contraint is not satisifed and cannot backtrack.");
                return;
            }
        }

        mainloop: do {

            Fitness previousFitness = context.calculateFitness();

            logger.debug("Current depth: " + context.getDepth() + " Fitness: " + previousFitness);

            Collection<Object> transitionsFromCurrentState = context.getCurrentActivationIds();

            while (transitionsFromCurrentState.isEmpty()) {
                logger.debug("No transitions from current state: considered as a solution.");
                saveSolutionAndBacktrack();
                continue mainloop;
            }

            ArrayList<Object> transitionsToTry = new ArrayList<>(transitionsFromCurrentState.size());
            for (Object transition : transitionsFromCurrentState) {
                transitionsToTry.add(transition);
            }
            double numberOfTransitionsToTry = transitionsToTry.size() * percentOfOpenedStates;

            for (; numberOfTransitionsToTry > 0 && !transitionsToTry.isEmpty(); numberOfTransitionsToTry--) {
                int index = random.nextInt(transitionsToTry.size());
                Object transition = transitionsToTry.remove(index);

                context.executeAcitvationId(transition);

                if (!context.checkGlobalConstraints()) {
                    logger.debug("Global contraint is not satisifed, backtrack.");
                    context.backtrack();
                    continue;
                }
                if (context.isCurrentStateInTrajectory()) {
                    logger.debug("Current state is in trajectory, backtrack.");
                    context.backtrack();
                    continue;
                }

                Fitness fitness = context.calculateFitness();
                objectiveComparatorHelper.addTrajectoryFitness(
                        new TrajectoryFitness(context.getTrajectoryInfo().getLastActivationId(), fitness));
                context.backtrack();
            }

            if (objectiveComparatorHelper.getTrajectoryFitnesses().isEmpty()) {
                logger.debug("No viable transitions from current state: considered as a solution.");
                saveSolutionAndBacktrack();
                continue;
            }

            TrajectoryFitness randomBestFitness = objectiveComparatorHelper.getRandomBest();
            objectiveComparatorHelper.clearTrajectoryFitnesses();

            int compare = objectiveComparatorHelper.compare(previousFitness, randomBestFitness.fitness);

            if (compare > 0) {
                saveSolutionAndBacktrack();
                continue;
            } else {
                previousFitness = randomBestFitness.fitness;
                Object transition = randomBestFitness.trajectory[randomBestFitness.trajectory.length - 1];
                context.executeAcitvationId(transition);
            }

        } while (!isInterrupted.get());

        logger.info("Terminated.");
    }

    private void saveSolutionAndBacktrack() {
        context.calculateFitness();
        context.newSolution();
        logger.debug("Found solution: " + context.getTrajectoryInfo().toString());
        logger.debug("Backtrack for more solutions, if needed.");
        context.backtrackUntilRoot();
    }

    @Override
    public void interruptStrategy() {
        isInterrupted.set(true);
    }

}