001package org.cpsolver.ifs.assignment;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Set;
006
007import org.cpsolver.ifs.assignment.context.DefaultSingleAssignmentContextHolder;
008import org.cpsolver.ifs.model.Model;
009import org.cpsolver.ifs.model.Value;
010import org.cpsolver.ifs.model.Variable;
011import org.cpsolver.ifs.solver.Solver;
012
013
014/**
015 * An assignment using the old {@link Variable#getAssignment()} to store values of all the
016 * variables of the model. Besides of that, a set of assigned variables is kept in memory.
017 * It is fast, but there can be only one such assignment at a time.
018 * Ideal for single threaded solvers. Also used as a default assignment, see
019 * {@link Model#getDefaultAssignment()}. Used by {@link Solver} where there is only one
020 * assignment kept in memory.
021 * 
022 * @see Assignment
023 * @see Solver
024 * 
025 * @author  Tomáš Müller
026 * @version IFS 1.3 (Iterative Forward Search)<br>
027 *          Copyright (C) 2014 Tomáš Müller<br>
028 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
029 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
030 * <br>
031 *          This library is free software; you can redistribute it and/or modify
032 *          it under the terms of the GNU Lesser General Public License as
033 *          published by the Free Software Foundation; either version 3 of the
034 *          License, or (at your option) any later version. <br>
035 * <br>
036 *          This library is distributed in the hope that it will be useful, but
037 *          WITHOUT ANY WARRANTY; without even the implied warranty of
038 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
039 *          Lesser General Public License for more details. <br>
040 * <br>
041 *          You should have received a copy of the GNU Lesser General Public
042 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
043 * @param <V> Variable
044 * @param <T> Value
045 **/
046public class DefaultSingleAssignment<V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentAbstract<V, T> {
047    private Set<V> iAssignedVariables = new HashSet<V>();
048
049    public DefaultSingleAssignment() {
050        super(new DefaultSingleAssignmentContextHolder<V,T>());
051    }
052
053    @Override
054    @SuppressWarnings("deprecation")
055    public long getIteration(V variable) {
056        return variable.getLastIteration();
057    }
058
059    @Override
060    public Collection<V> assignedVariables() {
061        return iAssignedVariables;
062    }
063
064    @Override
065    @SuppressWarnings("deprecation")
066    protected T getValueInternal(V variable) {
067        return variable.getAssignment();
068    }
069
070    @Override
071    @SuppressWarnings("deprecation")
072    protected void setValueInternal(long iteration, V variable, T value) {
073        variable.setAssignment(value);
074        variable.setLastIteration(iteration);
075        if (value == null)
076            iAssignedVariables.remove(variable);
077        else
078            iAssignedVariables.add(variable);
079    }
080    
081    @Override
082    public int getIndex() {
083        return 0;
084    }
085}