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

org.chocosolver.solver.constraints.reification.PropConditional 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.reification;

import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.constraints.Propagator;
import org.chocosolver.solver.constraints.PropagatorPriority;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.Variable;
import org.chocosolver.util.ESat;

import java.util.Arrays;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * A specific propagator which temporarily posts constraint on condition.
 * 
* The user declares a scope of variables to observe, * and a {@link BooleanSupplier} as a condition checker. * When the condition holds, a {@link Supplier} is called to * create on-the-fly the set of constraints to post. * These constraints will automatically be removed on backtrack. * * @author Charles Prud'homme * @since 31/08/2020 */ public class PropConditional extends Propagator { private final Function checker; private final Supplier constraints; /** * Declare a conditionnal propagator. * * @param vars2observe Set of variables to observe, their modifications triggers the condition checking * @param checker Check a specific condition anytime a variable in scope is modified. * @param constraints Provide constraints to be added temporarily to the model. */ public PropConditional(Variable[] vars2observe, Function checker, Supplier constraints) { super(vars2observe, PropagatorPriority.VERY_SLOW, false); this.checker = checker; this.constraints = constraints; } @Override public void propagate(int evtmask) throws ContradictionException { if (checker.apply(this.vars)) { setPassive(); model.postTemp(constraints.get()); } } @Override public ESat isEntailed() { return ESat.TRUE; } /** * Returns {@code true} when all variables are instantiated. */ public static final Function ALL_INSTANTIATED = (vars) -> Arrays.stream(vars).allMatch(Variable::isInstantiated); /** * Returns {@code true} when at least one variable is instantiated. */ public static final Function ONE_INSTANTIATED = (vars) -> Arrays.stream(vars).anyMatch(Variable::isInstantiated); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy