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