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 * @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 <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>. 042 * @param <V> Variable 043 * @param <T> Value 044 **/ 045public class DefaultSingleAssignment<V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentAbstract<V, T> { 046 private Set<V> iAssignedVariables = new HashSet<V>(); 047 048 public DefaultSingleAssignment() { 049 super(new DefaultSingleAssignmentContextHolder<V,T>()); 050 } 051 052 @Override 053 @SuppressWarnings("deprecation") 054 public long getIteration(V variable) { 055 return variable.getLastIteration(); 056 } 057 058 @Override 059 public Collection<V> assignedVariables() { 060 return iAssignedVariables; 061 } 062 063 @Override 064 @SuppressWarnings("deprecation") 065 protected T getValueInternal(V variable) { 066 return variable.getAssignment(); 067 } 068 069 @Override 070 @SuppressWarnings("deprecation") 071 protected void setValueInternal(long iteration, V variable, T value) { 072 variable.setAssignment(value); 073 variable.setLastIteration(iteration); 074 if (value == null) 075 iAssignedVariables.remove(variable); 076 else 077 iAssignedVariables.add(variable); 078 } 079 080 @Override 081 public int getIndex() { 082 return 0; 083 } 084}