All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chocosolver.solver.variables.impl.FixedIntVarImpl Maven / Gradle / Ivy

There is a newer version: 4.10.16
Show newest version
/**
 * Copyright (c) 2016, Ecole des Mines de Nantes
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by the .
 * 4. Neither the name of the  nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY  ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL  BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.chocosolver.solver.variables.impl;

import org.chocosolver.solver.ICause;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IVariableMonitor;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.Variable;
import org.chocosolver.solver.variables.delta.IIntDeltaMonitor;
import org.chocosolver.solver.variables.delta.NoDelta;
import org.chocosolver.solver.variables.events.IEventType;
import org.chocosolver.solver.variables.view.IView;
import org.chocosolver.util.iterators.DisposableRangeIterator;
import org.chocosolver.util.iterators.DisposableValueIterator;
import org.chocosolver.util.iterators.IntVarValueIterator;
import org.chocosolver.util.objects.setDataStructures.iterable.IntIterableSet;

import java.util.Iterator;

/**
 * A IntVar with one domain value.
 * 

* Based on "Views and Iterators for Generic Constraint Implementations", * C. Schulte and G. Tack *
* * @author Charles Prud'homme * @since 04/02/11 */ public class FixedIntVarImpl extends AbstractVariable implements IntVar { /** * The constant this variable relies on. */ protected final int constante; /** * Reusable iterator over values. */ private DisposableValueIterator _viterator; /** * Reusable iterator over ranges. */ private DisposableRangeIterator _riterator; /** * Value iterator allowing for(int i:this) loops */ private IntVarValueIterator _javaIterator = new IntVarValueIterator(this); /** * Creates a variable whom domain is natively reduced to the singleton {constante}. * @param name name of the variable * @param constante value its domain is reduced to * @param model the declaring model */ public FixedIntVarImpl(String name, int constante, Model model) { super(name, model); this.constante = constante; } @Override public boolean removeValue(int value, ICause cause) throws ContradictionException { if (value == constante) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "unique value removal"); } return false; } @Override public boolean removeValues(IntIterableSet values, ICause cause) throws ContradictionException { if (values.contains(constante)) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "unique value removal"); } return false; } @Override public boolean removeAllValuesBut(IntIterableSet values, ICause cause) throws ContradictionException { if (!values.contains(constante)) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "unique value removal"); } return false; } @Override public boolean removeInterval(int from, int to, ICause cause) throws ContradictionException { if (from <= constante && constante <= to) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "unique value removal"); } return false; } @Override public boolean instantiateTo(int value, ICause cause) throws ContradictionException { if (value != constante) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "outside domain instantitation"); } return false; } @Override public boolean updateLowerBound(int value, ICause cause) throws ContradictionException { if (value > constante) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "outside domain update bound"); } return false; } @Override public boolean updateUpperBound(int value, ICause cause) throws ContradictionException { if (value < constante) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "outside domain update bound"); } return false; } @Override public boolean updateBounds(int lb, int ub, ICause cause) throws ContradictionException { if (lb > constante || ub < constante) { assert cause != null; model.getSolver().getExplainer().removeValue(this, constante, cause); this.contradiction(cause, "outside domain update bound"); } return false; } @Override public boolean contains(int value) { return constante == value; } @Override public boolean isInstantiatedTo(int value) { return constante == value; } @Override public int getValue() { return constante; } @Override public int getLB() { return constante; } @Override public int getUB() { return constante; } @Override public int getDomainSize() { return 1; } @Override public int getRange() { return 1; } @Override public int nextValue(int v) { if (v < constante) { return constante; } else { return Integer.MAX_VALUE; } } @Override public int nextValueOut(int v) { if(v == constante -1){ return constante + 1; }else{ return v + 1; } } @Override public int previousValue(int v) { if (v > constante) { return constante; } else { return Integer.MIN_VALUE; } } @Override public int previousValueOut(int v) { if(v == constante + 1){ return constante - 1; }else{ return v - 1; } } @Override public boolean hasEnumeratedDomain() { return true; } @Override public NoDelta getDelta() { return NoDelta.singleton; } @Override public boolean isInstantiated() { return true; } @Override//void (a constant receives no event) public void addMonitor(IVariableMonitor monitor) { } @Override//void (a constant receives no event) public void subscribeView(IView view) { } @SuppressWarnings("unchecked") @Override public IIntDeltaMonitor monitorDelta(ICause propagator) { return IIntDeltaMonitor.Default.NONE; } @Override public void createDelta() { } @Override//void (a constant receives no event) public void notifyPropagators(IEventType event, ICause cause) throws ContradictionException { } @Override//void (a constant receives no event) public void notifyMonitors(IEventType event) throws ContradictionException { } @Override//void (a constant receives no event) public void notifyViews(IEventType event, ICause cause) throws ContradictionException { } @Override public String toString() { return name + " = " + String.valueOf(constante); } @Override public void contradiction(ICause cause, String message) throws ContradictionException { model.getSolver().getEngine().fails(cause, this, message); } @Override public int getTypeAndKind() { return Variable.INT | Variable.CSTE; } @Override public DisposableValueIterator getValueIterator(boolean bottomUp) { if (_viterator == null || _viterator.isNotReusable()) { _viterator = new DisposableValueIterator() { /** * Set to true if iteration has not started yet, false otherwise */ boolean _next; @Override public void bottomUpInit() { super.bottomUpInit(); _next = true; } @Override public void topDownInit() { super.topDownInit(); _next = true; } @Override public boolean hasNext() { return _next; } @Override public boolean hasPrevious() { return _next; } @Override public int next() { _next = false; return constante; } @Override public int previous() { _next = false; return constante; } }; } if (bottomUp) { _viterator.bottomUpInit(); } else { _viterator.topDownInit(); } return _viterator; } @Override public DisposableRangeIterator getRangeIterator(boolean bottomUp) { if (_riterator == null || _riterator.isNotReusable()) { _riterator = new DisposableRangeIterator() { /** * Set to true if the iteration has not started yet, false otherwise */ boolean _next; @Override public void bottomUpInit() { super.bottomUpInit(); _next = true; } @Override public void topDownInit() { super.topDownInit(); _next = true; } @Override public boolean hasNext() { return _next; } @Override public boolean hasPrevious() { return _next; } @Override public void next() { _next = false; } @Override public void previous() { _next = false; } @Override public int min() { return constante; } @Override public int max() { return constante; } }; } if (bottomUp) { _riterator.bottomUpInit(); } else { _riterator.topDownInit(); } return _riterator; } @Override public Iterator iterator() { _javaIterator.reset(); return _javaIterator; } }