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

org.chocosolver.solver.constraints.ternary.PropTimesNaiveWithLong Maven / Gradle / Ivy

There is a newer version: 4.10.17
Show newest version
/*
 * This file is part of choco-solver, http://choco-solver.org/
 *
 * Copyright (c) 2022, IMT Atlantique. All rights reserved.
 *
 * Licensed under the BSD 4-clause license.
 *
 * See LICENSE file in the project root for full license information.
 */
package org.chocosolver.solver.constraints.ternary;

import org.chocosolver.solver.constraints.Propagator;
import org.chocosolver.solver.constraints.PropagatorPriority;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.util.ESat;

/**
 * V0 * V1 = V2
 * 
* * @author Charles Prud'homme * @since 26/01/11 */ public class PropTimesNaiveWithLong extends Propagator { protected static final long MAX = Long.MAX_VALUE - 1, MIN = Long.MIN_VALUE + 1; private final IntVar v0; private final IntVar v1; private final IntVar v2; public PropTimesNaiveWithLong(IntVar v1, IntVar v2, IntVar result) { super(new IntVar[]{v1, v2, result}, PropagatorPriority.TERNARY, false); this.v0 = vars[0]; this.v1 = vars[1]; this.v2 = vars[2]; } @Override public final int getPropagationConditions(int vIdx) { return IntEventType.boundAndInst(); } @Override public final void propagate(int evtmask) throws ContradictionException { boolean hasChanged = true; while (hasChanged) { hasChanged = div(v0, v2.getLB(), v2.getUB(), v1.getLB(), v1.getUB()); hasChanged |= div(v1, v2.getLB(), v2.getUB(), v0.getLB(), v0.getUB()); hasChanged |= mul(v2, v0.getLB(), v0.getUB(), v1.getLB(), v1.getUB()); } if (v2.isInstantiatedTo(0) && (v0.isInstantiatedTo(0) || v1.isInstantiatedTo(0))) { setPassive(); } } @Override public final ESat isEntailed() { if (isCompletelyInstantiated()) { return ESat.eval(v0.getValue() * v1.getValue() == v2.getValue()); } return ESat.UNDEFINED; } private boolean div(IntVar var, int a, int b, int c, int d) throws ContradictionException { long min, max; if (a <= 0 && b >= 0 && c <= 0 && d >= 0) { // case 1 min = MIN; max = MAX; return var.updateLowerBound(min, this) | var.updateUpperBound(max, this); } else if (c == 0 && d == 0 && (a > 0 || b < 0)) // case 2 fails(); // TODO: could be more precise, for explanation purpose else if (c < 0 && d > 0 && (a > 0 || b < 0)) { // case 3 max = Math.max(Math.abs(a), Math.abs(b)); min = -max; return var.updateLowerBound(min, this) | var.updateUpperBound(max, this); } else if (c == 0 && d != 0 && (a > 0 || b < 0)) // case 4 a return div(var, a, b, 1, d); else if (c != 0 && d == 0 && (a > 0 || b < 0)) // case 4 b return div(var, a, b, c, -1); else { // if (c > 0 || d < 0) { // case 5 float ac = (float) a / c, ad = (float) a / d, bc = (float) b / c, bd = (float) b / d; float low = Math.min(Math.min(ac, ad), Math.min(bc, bd)); float high = Math.max(Math.max(ac, ad), Math.max(bc, bd)); min = Math.round(Math.ceil(low)); max = Math.round(Math.floor(high)); if (min > max) this.fails(); // TODO: could be more precise, for explanation purpose return var.updateLowerBound(min, this) | var.updateUpperBound(max, this); } return false; } private boolean mul(IntVar var, int a, int b, int c, int d) throws ContradictionException { long min = Math.min(Math.min(a * c, a * d), Math.min(b * c, b * d)); long max = Math.max(Math.max(a * c, a * d), Math.max(b * c, b * d)); return var.updateLowerBound(min, this) | var.updateUpperBound(max, this); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy