001    package net.sf.cpsolver.ifs.model;
002    
003    /**
004     * A neighbour consisting of a change (either assignment or unassignment) of a single variable. 
005     *
006     * @see net.sf.cpsolver.ifs.heuristics.NeighbourSelection
007     *
008     * @version
009     * IFS 1.1 (Iterative Forward Search)<br>
010     * Copyright (C) 2006 Tomáš Müller<br>
011     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
012     * Lazenska 391, 76314 Zlin, Czech Republic<br>
013     * <br>
014     * This library is free software; you can redistribute it and/or
015     * modify it under the terms of the GNU Lesser General Public
016     * License as published by the Free Software Foundation; either
017     * version 2.1 of the License, or (at your option) any later version.
018     * <br><br>
019     * This library is distributed in the hope that it will be useful,
020     * but 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.
023     * <br><br>
024     * You should have received a copy of the GNU Lesser General Public
025     * License along with this library; if not, write to the Free Software
026     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
027     */
028    
029    public class SimpleNeighbour extends Neighbour {
030            private Variable iVariable = null;
031            private Value iValue = null;
032            
033            /** Model
034             * @param variable variable to be assigned
035             * @param value value to be assigned to the given variable, null if the variable should be unassigned
036             */
037            public SimpleNeighbour(Variable variable, Value value) {
038                    iVariable = variable;
039                    iValue = value;
040            }
041            
042            /** Selected variable */
043            public Variable getVariable() {
044                return iVariable;
045            }
046            
047            /** Selected value */
048            public Value getValue() {
049                return iValue;
050            }
051            
052            /** Perform assignment */ 
053            public void assign(long iteration) {
054                    if (iVariable==null) return;
055                    if (iValue!=null)
056                            iVariable.assign(iteration, iValue);
057                    else
058                            iVariable.unassign(iteration);
059            }
060    
061            /** Improvement in the solution value if this neighbour is accepted. */
062            public double value() {
063                return
064                    (iValue==null?0:iValue.toDouble()) -
065                    (iVariable==null || iVariable.getAssignment()==null?0:iVariable.getAssignment().toDouble());
066            }
067    
068            
069            public String toString() {
070                    return iVariable.getName()+" "+(iVariable.getAssignment()==null?"null":iVariable.getAssignment().getName())+" -> "+(iValue==null?"null":iValue.getName());
071            }
072    }