    001package org.cpsolver.ifs.example.jobshop;
    002
    003import java.util.Set;
    004
    005import org.cpsolver.ifs.assignment.Assignment;
    006import org.cpsolver.ifs.model.Constraint;
    007
    008
    009/**
    010 * Machine constraint. <br>
    011 * <br>
    012 * Each machine contians a given set of operations (variables). A machine
    013 * constraint is satisfied, if all operations on it do not overlap in time.
    014 *
    015 * @version IFS 1.3 (Iterative Forward Search)<br>
    016 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    017 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    018 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    019 * <br>
    020 *          This library is free software; you can redistribute it and/or modify
    021 *          it under the terms of the GNU Lesser General Public License as
    022 *          published by the Free Software Foundation; either version 3 of the
    023 *          License, or (at your option) any later version. <br>
    024 * <br>
    025 *          This library is distributed in the hope that it will be useful, but
    026 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    027 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    028 *          Lesser General Public License for more details. <br>
    029 * <br>
    030 *          You should have received a copy of the GNU Lesser General Public
    031 *          License along with this library; if not see
    032 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    033 */
    034public class Machine extends Constraint<Operation, Location> {
    035    private int iMachineNumber = -1;
    036
    037    /**
    038     * Constructor
    039     *
    040     * @param machineNumber
    041     *            machine number
    042     */
    043    public Machine(int machineNumber) {
    044        super();
    045        iMachineNumber = machineNumber;
    046    }
    047
    048    /** Get machine number
    049     * @return machine number
    050     **/
    051    public int getMachineNumber() {
    052        return iMachineNumber;
    053    }
    054
    055    /**
    056     * Adds conflicting operations into the set of conflicts.
    057     */
    058    @Override
    059    public void computeConflicts(Assignment<Operation, Location> assignment, Location location, Set<Location> conflicts) {
    060        for (Operation o : assignedVariables(assignment)) {
    061            if (o.getOperationNumber() == location.variable().getOperationNumber() && o.getJobNumber() == location.variable().getJobNumber())
    062                continue;
    063            Location conf = assignment.getValue(o);
    064            if (conf.overlap(location))
    065                conflicts.add(conf);
    066        }
    067    }
    068
    069    /**
    070     * True if there is an operation from the machine which violates with the
    071     * given assignment.
    072     */
    073    @Override
    074    public boolean inConflict(Assignment<Operation, Location> assignment, Location location) {
    075        for (Operation o : assignedVariables(assignment)) {
    076            if (o.getOperationNumber() == location.variable().getOperationNumber()
    077                    && o.getJobNumber() == location.variable().getJobNumber())
    078                continue;
    079            if (assignment.getValue(o).overlap(location))
    080                return true;
    081        }
    082        return false;
    083    }
    084
    085    /**
    086     * True if the two assignments (placement of opeartions of the machine in
    087     * time) violates each other.
    088     */
    089    @Override
    090    public boolean isConsistent(Location value1, Location value2) {
    091        return !value1.overlap(value2);
    092    }
    093
    094    /** string representation -- for debuging and printing purposes */
    095    @Override
    096    public String toString() {
    097        return getName();
    098    }
    099
    100    /**
    101     * Name of the machine (e.g. M10 where 10 is the machine number)
    102     */
    103    @Override
    104    public String getName() {
    105        return "M" + iMachineNumber;
    106    }
    107}