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 * @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 Job extends Constraint<Operation, Location> {
036    private int iJobNumber = 0;
037    private int iDueTime = -1;
038
039    /**
040     * Constructor
041     * 
042     * @param jobNumber
043     *            job number
044     */
045    public Job(int jobNumber) {
046        super();
047        iJobNumber = jobNumber;
048    }
049
050    /**
051     * Set due time
052     * @param dueTime due time
053     */
054    public void setDueTime(int dueTime) {
055        iDueTime = dueTime;
056    }
057
058    /**
059     * Get due time
060     * @return due time
061     */
062    public int getDueTime() {
063        return iDueTime;
064    }
065
066    /**
067     * Get job number
068     * @return job number
069     */
070    public int getJobNumner() {
071        return iJobNumber;
072    }
073
074    /**
075     * Count job operations for the job (i.e., the number of variables in this
076     * constraint)
077     * @return number of operations in the problem
078     */
079    public int countOperations() {
080        return variables().size();
081    }
082
083    /**
084     * Get operation of the given index (0..countOperations()-1)
085     * @param opNumber operation number
086     * @return operation
087     */
088    public Operation getOperation(int opNumber) {
089        return variables().get(opNumber);
090    }
091
092    /**
093     * Adds conflicting operations into the set of conflicts.
094     */
095    @Override
096    public void computeConflicts(Assignment<Operation, Location> assignment, Location location, Set<Location> conflicts) {
097        for (Operation o : assignedVariables(assignment)) {
098            if (o.getOperationNumber() == location.variable().getOperationNumber())
099                continue;
100            Location l = assignment.getValue(o);
101            if (o.getOperationNumber() < location.variable().getOperationNumber()) {
102                if (!l.before(location))
103                    conflicts.add(l);
104            } else {
105                if (!l.after(location))
106                    conflicts.add(l);
107            }
108        }
109    }
110
111    /**
112     * True if there is an operation from the same job which violates with the
113     * given assignment.
114     */
115    @Override
116    public boolean inConflict(Assignment<Operation, Location> assignment, Location location) {
117        for (Operation o : assignedVariables(assignment)) {
118            if (o.getOperationNumber() == location.variable().getOperationNumber())
119                continue;
120            Location l = assignment.getValue(o);
121            if (o.getOperationNumber() < location.variable().getOperationNumber()) {
122                if (!l.before(location))
123                    return true;
124            } else {
125                if (!l.after(location))
126                    return true;
127            }
128        }
129        return false;
130    }
131
132    /**
133     * True if the two assignments (placement of opeartions of the same job in
134     * time) violates each other.
135     */
136    @Override
137    public boolean isConsistent(Location location1, Location location2) {
138        Operation operation1 = location1.variable();
139        Operation operation2 = location2.variable();
140        if (operation1.getOperationNumber() < operation2.getOperationNumber()) {
141            if (location1.before(location2))
142                return true;
143        } else {
144            if (location2.before(location1))
145                return true;
146        }
147        return false;
148    }
149
150    /**
151     * String representation -- for debuging and printing purposes
152     */
153    @Override
154    public String toString() {
155        return "J" + iJobNumber;
156    }
157
158    /**
159     * Name of the job (e.g. J10 where 10 is the job number)
160     */
161    @Override
162    public String getName() {
163        return "J" + iJobNumber;
164    }
165}