001package org.cpsolver.ifs.example.jobshop;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.cpsolver.ifs.model.Variable;
007
008
009/**
010 * Operation. <br>
011 * <br>
012 * Each operation has its number, job, machine and processing time
013 * 
014 * @author  Tomáš Müller
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 Operation extends Variable<Operation, Location> {
035    private Job iJob = null;
036    private Machine iMachine = null;
037    private int iProcessingTime = 0;
038    private int iOperationNumber = 0;
039
040    /**
041     * Constructor
042     * 
043     * @param job
044     *            job
045     * @param machine
046     *            machine
047     * @param operationNumber
048     *            operation number
049     * @param processingTime
050     *            processing time
051     */
052    public Operation(Job job, Machine machine, int operationNumber, int processingTime) {
053        super(null);
054        iJob = job;
055        iMachine = machine;
056        iProcessingTime = processingTime;
057        iOperationNumber = operationNumber;
058    }
059
060    /** Get job 
061     * @return job
062     **/
063    public Job getJob() {
064        return iJob;
065    }
066
067    /** Get job number
068     * @return job number
069     **/
070    public int getJobNumber() {
071        return iJob.getJobNumner();
072    }
073
074    /** Get operation number 
075     * @return operation number
076     **/
077    public int getOperationNumber() {
078        return iOperationNumber;
079    }
080
081    /** Get machine 
082     * @return machine
083     **/
084    public Machine getMachine() {
085        return iMachine;
086    }
087
088    /** Get machine number 
089     * @return machine number
090     **/
091    public int getMachineNumber() {
092        return iMachine.getMachineNumber();
093    }
094
095    /** Get processing time
096     * @return processing time
097     **/
098    public int getProcessingTime() {
099        return iProcessingTime;
100    }
101
102    /** Get the preceding operation (if any)
103     * @return preceding operation
104     **/
105    public Operation getPrecedingOperation() {
106        return (iOperationNumber == 0 ? null : iJob.getOperation(iOperationNumber - 1));
107    }
108
109    /** Get the subsequent operation (if any)
110     * @return subsequent operation
111     **/
112    public Operation getSubsequentOperation() {
113        return (iOperationNumber + 1 == iJob.countOperations() ? null : iJob.getOperation(iOperationNumber + 1));
114    }
115
116    /** Get minimal starting time
117     * @return minimal starting time
118     **/
119    public int getMinStartTime() {
120        if (iOperationNumber == 0)
121            return 0;
122        else
123            return getPrecedingOperation().getMinStartTime() + iProcessingTime;
124    }
125
126    /** Get maximal starting time
127     * @return maximal starting time
128     **/
129    public int getMaxStartTime() {
130        if (iOperationNumber + 1 == iJob.countOperations())
131            return ((JobShopModel) getModel()).getTotalNumberOfSlots() - iProcessingTime;
132        else
133            return getSubsequentOperation().getMaxStartTime() - iProcessingTime;
134    }
135
136    /** Compares two operations -- job number and operation number must match */
137    @Override
138    public boolean equals(Object o) {
139        if (o == null || !(o instanceof Operation))
140            return false;
141        Operation op = (Operation) o;
142        return getJobNumber() == op.getJobNumber() && getOperationNumber() == op.getOperationNumber();
143    }
144
145    /** Initialozation -- fills the variable's domain */
146    public void init() {
147        setValues(computeValues());
148    }
149
150    private List<Location> computeValues() {
151        List<Location> ret = new ArrayList<Location>();
152        for (int i = getMinStartTime(); i <= getMaxStartTime(); i++)
153            ret.add(new Location(this, i));
154        return ret;
155    }
156
157    /** string representation -- for debuging and printing purposes */
158    @Override
159    public String toString() {
160        return getName();
161    }
162
163    /**
164     * Operation's name (e.g., O[2,4] where 2 is the job number and 4 is the
165     * operation number
166     */
167    @Override
168    public String getName() {
169        return "O[" + getJobNumber() + "," + getOperationNumber() + "]";
170    }
171}