001package org.cpsolver.ifs.algorithms.neighbourhoods;
002
003import java.util.List;
004
005import org.cpsolver.ifs.assignment.Assignment;
006import org.cpsolver.ifs.heuristics.NeighbourSelection;
007import org.cpsolver.ifs.model.Model;
008import org.cpsolver.ifs.model.Neighbour;
009import org.cpsolver.ifs.model.SimpleNeighbour;
010import org.cpsolver.ifs.model.Value;
011import org.cpsolver.ifs.model.Variable;
012import org.cpsolver.ifs.solution.Solution;
013import org.cpsolver.ifs.solver.Solver;
014import org.cpsolver.ifs.util.DataProperties;
015import org.cpsolver.ifs.util.ToolBox;
016
017
018/**
019 * Try to assign a variable with a new value. A variable is selected randomly, a
020 * different (available) value is randomly selected for the variable -- the variable is
021 * assigned with the new value if there is no conflict.
022 * <br>
023 * 
024 * @author  Tomáš Müller
025 * @version IFS 1.3 (Iterative Forward Search)<br>
026 *          Copyright (C) 2014 Tomáš Müller<br>
027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029 * <br>
030 *          This library is free software; you can redistribute it and/or modify
031 *          it under the terms of the GNU Lesser General Public License as
032 *          published by the Free Software Foundation; either version 3 of the
033 *          License, or (at your option) any later version. <br>
034 * <br>
035 *          This library is distributed in the hope that it will be useful, but
036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
038 *          Lesser General Public License for more details. <br>
039 * <br>
040 *          You should have received a copy of the GNU Lesser General Public
041 *          License along with this library; if not see
042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043 * @param <V> Variable
044 * @param <T> Value
045 */
046public class RandomMove<V extends Variable<V, T>, T extends Value<V, T>> implements NeighbourSelection<V, T>, HillClimberSelection {
047    protected boolean iHC = false;
048    
049    public RandomMove(DataProperties config) {
050    }
051
052    @Override
053    public void setHcMode(boolean hcMode) {
054        iHC = hcMode;
055    }
056
057    @Override
058    public void init(Solver<V, T> solver) {
059    }
060
061    @Override
062    public Neighbour<V, T> selectNeighbour(Solution<V, T> solution) {
063        Model<V, T> model = solution.getModel();
064        Assignment<V, T> assignment = solution.getAssignment();
065        int varIdx = ToolBox.random(model.variables().size());
066        for (int i = 0; i < model.variables().size(); i++) {
067            V variable = model.variables().get((i + varIdx) % model.variables().size());
068            List<T> values = variable.values(solution.getAssignment());
069            if (values.isEmpty()) continue;
070            int valIdx = ToolBox.random(values.size());
071            for (int j = 0; j < values.size(); j++) {
072                T value = values.get((j + valIdx) % values.size());
073                if (!model.inConflict(assignment, value)) {
074                    SimpleNeighbour<V, T> n = new SimpleNeighbour<V, T>(variable, value);
075                    if (!iHC || n.value(assignment) <= 0) return n;
076                }
077            }
078        }
079        return null;
080    }
081}