    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 * Job constraint. <br>
    011 * <br>
    012 * Each job contians a given set of operations (variables). A job constraint is
    013 * satisfied, if all operations of the job do not overlap in time and are
    014 * processed in the given order.
    015 *
    016 * @author  Tomáš Müller
    017 * @version IFS 1.3 (Iterative Forward Search)<br>
    018 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    019 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    020 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    021 * <br>
    022 *          This library is free software; you can redistribute it and/or modify
    023 *          it under the terms of the GNU Lesser General Public License as
    024 *          published by the Free Software Foundation; either version 3 of the
    025 *          License, or (at your option) any later version. <br>
    026 * <br>
    027 *          This library is distributed in the hope that it will be useful, but
    028 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    029 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    030 *          Lesser General Public License for more details. <br>
    031 * <br>
    032 *          You should have received a copy of the GNU Lesser General Public
    033 *          License along with this library; if not see
    034 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    035 */
    036public class Job extends Constraint<Operation, Location> {
    037    private int iJobNumber = 0;
    038    private int iDueTime = -1;
    039
    040    /**
    041     * Constructor
    042     *
    043     * @param jobNumber
    044     *            job number
    045     */
    046    public Job(int jobNumber) {
    047        super();
    048        iJobNumber = jobNumber;
    049    }
    050
    051    /**
    052     * Set due time
    053     * @param dueTime due time
    054     */
    055    public void setDueTime(int dueTime) {
    056        iDueTime = dueTime;
    057    }
    058
    059    /**
    060     * Get due time
    061     * @return due time
    062     */
    063    public int getDueTime() {
    064        return iDueTime;
    065    }
    066
    067    /**
    068     * Get job number
    069     * @return job number
    070     */
    071    public int getJobNumner() {
    072        return iJobNumber;
    073    }
    074
    075    /**
    076     * Count job operations for the job (i.e., the number of variables in this
    077     * constraint)
    078     * @return number of operations in the problem
    079     */
    080    public int countOperations() {
    081        return variables().size();
    082    }
    083
    084    /**
    085     * Get operation of the given index (0..countOperations()-1)
    086     * @param opNumber operation number
    087     * @return operation
    088     */
    089    public Operation getOperation(int opNumber) {
    090        return variables().get(opNumber);
    091    }
    092
    093    /**
    094     * Adds conflicting operations into the set of conflicts.
    095     */
    096    @Override
    097    public void computeConflicts(Assignment<Operation, Location> assignment, Location location, Set<Location> conflicts) {
    098        for (Operation o : assignedVariables(assignment)) {
    099            if (o.getOperationNumber() == location.variable().getOperationNumber())
    100                continue;
    101            Location l = assignment.getValue(o);
    102            if (o.getOperationNumber() < location.variable().getOperationNumber()) {
    103                if (!l.before(location))
    104                    conflicts.add(l);
    105            } else {
    106                if (!l.after(location))
    107                    conflicts.add(l);
    108            }
    109        }
    110    }
    111
    112    /**
    113     * True if there is an operation from the same job which violates with the
    114     * given assignment.
    115     */
    116    @Override
    117    public boolean inConflict(Assignment<Operation, Location> assignment, Location location) {
    118        for (Operation o : assignedVariables(assignment)) {
    119            if (o.getOperationNumber() == location.variable().getOperationNumber())
    120                continue;
    121            Location l = assignment.getValue(o);
    122            if (o.getOperationNumber() < location.variable().getOperationNumber()) {
    123                if (!l.before(location))
    124                    return true;
    125            } else {
    126                if (!l.after(location))
    127                    return true;
    128            }
    129        }
    130        return false;
    131    }
    132
    133    /**
    134     * True if the two assignments (placement of opeartions of the same job in
    135     * time) violates each other.
    136     */
    137    @Override
    138    public boolean isConsistent(Location location1, Location location2) {
    139        Operation operation1 = location1.variable();
    140        Operation operation2 = location2.variable();
    141        if (operation1.getOperationNumber() < operation2.getOperationNumber()) {
    142            if (location1.before(location2))
    143                return true;
    144        } else {
    145            if (location2.before(location1))
    146                return true;
    147        }
    148        return false;
    149    }
    150
    151    /**
    152     * String representation -- for debuging and printing purposes
    153     */
    154    @Override
    155    public String toString() {
    156        return "J" + iJobNumber;
    157    }
    158
    159    /**
    160     * Name of the job (e.g. J10 where 10 is the job number)
    161     */
    162    @Override
    163    public String getName() {
    164        return "J" + iJobNumber;
    165    }
    166}