001package org.cpsolver.ifs.assignment; 002 003import java.util.Collection; 004import java.util.HashMap; 005import java.util.Map; 006import java.util.concurrent.locks.Lock; 007 008import org.cpsolver.ifs.assignment.context.AssignmentContextHolder; 009import org.cpsolver.ifs.assignment.context.DefaultParallelAssignmentContextHolder; 010import org.cpsolver.ifs.model.Model; 011import org.cpsolver.ifs.model.Value; 012import org.cpsolver.ifs.model.Variable; 013import org.cpsolver.ifs.solution.Solution; 014import org.cpsolver.ifs.solver.ParallelSolver; 015 016 017/** 018 * An assignment using the {@link Variable#getAssignments()} to store values of all the 019 * variables of the model. Besides of that, a set of assigned variables is kept in memory. 020 * Each extra contains an array of values, indexed by {@link Assignment#getIndex()}. 021 * Useful for a small, fixed number of assignments. Used by the {@link ParallelSolver}, 022 * where there is one assignment for each thread. 023 * 024 * @see Assignment 025 * @see ParallelSolver 026 * 027 * @author Tomáš Müller 028 * @version IFS 1.3 (Iterative Forward Search)<br> 029 * Copyright (C) 2014 Tomáš Müller<br> 030 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 031 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 032 * <br> 033 * This library is free software; you can redistribute it and/or modify 034 * it under the terms of the GNU Lesser General Public License as 035 * published by the Free Software Foundation; either version 3 of the 036 * License, or (at your option) any later version. <br> 037 * <br> 038 * This library is distributed in the hope that it will be useful, but 039 * WITHOUT ANY WARRANTY; without even the implied warranty of 040 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 041 * Lesser General Public License for more details. <br> 042 * <br> 043 * You should have received a copy of the GNU Lesser General Public 044 * License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>. 045 * @param <V> Variable 046 * @param <T> Value 047 **/ 048public class DefaultParallelAssignment <V extends Variable<V, T>, T extends Value<V, T>> extends AssignmentAbstract<V, T> { 049 private Map<V, Long> iAssignedVariables = new HashMap<V, Long>(); 050 private int iIndex; 051 052 public DefaultParallelAssignment(int threadIndex) { 053 super(new DefaultParallelAssignmentContextHolder<V, T>(threadIndex)); 054 iIndex = threadIndex; 055 } 056 057 public DefaultParallelAssignment() { 058 this(0); 059 } 060 061 public DefaultParallelAssignment(int threadIndex, Model<V, T> model, Assignment<V, T> assignment) { 062 this(threadIndex); 063 for (V variable: model.variables()) 064 setValueInternal(0, variable, assignment != null ? assignment.getValue(variable) : null); 065 } 066 067 public DefaultParallelAssignment(AssignmentContextHolder<V, T> contexts, int threadIndex, Solution<V, T> solution) { 068 super(contexts); 069 iIndex = threadIndex; 070 Lock lock = solution.getLock().readLock(); 071 lock.lock(); 072 try { 073 for (V variable: solution.getModel().variables()) 074 setValueInternal(0, variable, solution.getAssignment().getValue(variable)); 075 } finally { 076 lock.unlock(); 077 } 078 } 079 080 @Override 081 public long getIteration(V variable) { 082 Long it = iAssignedVariables.get(variable); 083 return (it == null ? 0 : it.longValue()); 084 } 085 086 @Override 087 public Collection<V> assignedVariables() { 088 return iAssignedVariables.keySet(); 089 } 090 091 @Override 092 @SuppressWarnings({ "deprecation", "unchecked" }) 093 protected T getValueInternal(V variable) { 094 return (T) variable.getAssignments()[iIndex]; 095 } 096 097 @Override 098 @SuppressWarnings("deprecation") 099 protected void setValueInternal(long iteration, V variable, T value) { 100 variable.getAssignments()[iIndex] = value; 101 if (value == null) 102 iAssignedVariables.remove(variable); 103 else 104 iAssignedVariables.put(variable, iteration); 105 } 106 107 @Override 108 public int getIndex() { 109 return iIndex; 110 } 111}