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