001package net.sf.cpsolver.ifs.model; 002 003/** 004 * A neighbour consisting of a change (either assignment or unassignment) of a 005 * single variable. 006 * 007 * @see net.sf.cpsolver.ifs.heuristics.NeighbourSelection 008 * 009 * @version IFS 1.2 (Iterative Forward Search)<br> 010 * Copyright (C) 2006 - 2010 Tomáš Müller<br> 011 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 012 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 013 * <br> 014 * This library is free software; you can redistribute it and/or modify 015 * it under the terms of the GNU Lesser General Public License as 016 * published by the Free Software Foundation; either version 3 of the 017 * License, or (at your option) any later version. <br> 018 * <br> 019 * This library is distributed in the hope that it will be useful, but 020 * WITHOUT ANY WARRANTY; without even the implied warranty of 021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 022 * Lesser General Public License for more details. <br> 023 * <br> 024 * You should have received a copy of the GNU Lesser General Public 025 * License along with this library; if not see 026 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 027 */ 028 029public class SimpleNeighbour<V extends Variable<V, T>, T extends Value<V, T>> extends Neighbour<V, T> { 030 private V iVariable = null; 031 private T iValue = null; 032 033 /** 034 * Model 035 * 036 * @param variable 037 * variable to be assigned 038 * @param value 039 * value to be assigned to the given variable, null if the 040 * variable should be unassigned 041 */ 042 public SimpleNeighbour(V variable, T value) { 043 iVariable = variable; 044 iValue = value; 045 } 046 047 /** Selected variable */ 048 public V getVariable() { 049 return iVariable; 050 } 051 052 /** Selected value */ 053 public T getValue() { 054 return iValue; 055 } 056 057 /** Perform assignment */ 058 @Override 059 public void assign(long iteration) { 060 if (iVariable == null) 061 return; 062 if (iValue != null) 063 iVariable.assign(iteration, iValue); 064 else 065 iVariable.unassign(iteration); 066 } 067 068 /** Improvement in the solution value if this neighbour is accepted. */ 069 @Override 070 public double value() { 071 return (iValue == null ? 0 : iValue.toDouble()) 072 - (iVariable == null || iVariable.getAssignment() == null ? 0 : iVariable.getAssignment().toDouble()); 073 } 074 075 @Override 076 public String toString() { 077 return iVariable.getName() + " " 078 + (iVariable.getAssignment() == null ? "null" : iVariable.getAssignment().getName()) + " -> " 079 + (iValue == null ? "null" : iValue.getName()); 080 } 081}