haifa.shopsim.fastkernel
Class FastShopRun

java.lang.Object
  |
  +--haifa.shopsim.fastkernel.FastShopRun
All Implemented Interfaces:
ShopSimulation, ShopState
Direct Known Subclasses:
ThreadFastShopRun

public class FastShopRun
extends java.lang.Object
implements ShopSimulation, ShopState

This class implements both a ShopSimualtion and a ShopState. It is a single thread "primitive" simulation of a job shop that does not keep information regarding the location of jobs. (It is called fast, because it is much faster than the intutive-to-program ShopRun class found in haifa.shopsim.kernel package (only up to version 0.3 of this project).

The FastShopRun is implemented by using an EventQ class.

Note that this class also implements a ShopState.

Listeners are to register to the FastShopRun using the addShopChangeListener() method.

Version:
1.1

Inner Class Summary
private  class FastShopRun.Buffer
          This class represents a queue entity.
private  class FastShopRun.Machine
          This class represents a machine in the job shop.
 
Field Summary
protected  EventQ eventQ
          The EventQ
private  double expectedLowerBound
          The expected machine lower bound.
private  int[] finishedJobs
          An array of integers counting how many finished jobs are in each route.
private  FastShopRun.Buffer[] firstBuffers
          An array of references to the first buffer of each route.
(package private)  double lastRestUpdateTime
          The last time during which the resting map was updated.
protected  java.util.ArrayList listeners
          A list of listeners that are notified whenever the state changes.
protected  java.io.PrintWriter log
          Message log.
private  FastShopRun.Machine[] machines
          An array of machine objects.
private  int numBusyMachines
          The number of busy machines.
private  int[] numSetJobs
          Stores the Nr[] array as it was defined by the problem size chooser.
protected  PostRunAction postRunAction
          The PostRunAction, the doAction() method is called after every run of the shop.
protected  ProblemSizeChooser problemSizeChooser
          The problem size chooser.
protected static long PROGRESS_STEP
          Number of milliseconds between updates to log of '~' character showing that the simulation is running.
private  java.util.HashMap Qmap
          A map of the buffers where the keys are operations and the values are Integer objects.
private  java.util.HashMap QPlusMap
          A map of the buffers where the keys are operations and the values are Integer objects.
protected  RandomTimeMaker randomTimeMaker
          The random time maker.
(package private)  java.util.HashMap restingMap
          This is a map where the keys are Integers for machine numbers and the values are doubles that specify the time at which a machine was scheduled to rest.
protected  ShopAlgorithm shopAlgorithm
          The algorithm that is used for the run.
protected  ShopData shopData
          The job shop data.
private  long startTimeMillis
          The start time of the last time the go() method was called.
protected  double time
          The system time
private  int totalFinishedJobs
          The total number of finsihed jobs (equals the sum of finishedJobs)
 
Fields inherited from interface haifa.shopsim.ShopSimulation
MAX_SIM_TIME
 
Constructor Summary
FastShopRun(ShopAlgorithm shopAlgorithm_, ShopData shopData_)
          Creates a new FastShopRun, setting the log to stdout.
FastShopRun(ShopAlgorithm shopAlgorithm_, ShopData shopData_, PostRunAction postRunAction_, java.io.PrintWriter log_)
          Creates new FastShopRun.
 
Method Summary
 void addShopChangeListener(ShopChangeListener scl)
          Registers an additional shop change listner.
 boolean askAgainAtCurrentTime(int machineNumber)
          Instructs the shop simulation to ask the algorithm for another ShopCommand for the specific machine number at the current simulation time.
private  void buildShop()
          Auxillary method for the constructors.
 int[] getBusyMachines()
          Returns a collection of all of the busy machines at the current time.
 double getExpectedMachineLowerBound()
          Returns the maximum of getExpectedProcessingTimes().
 double getExpectedProcessingTime(int machine)
          Uses the getInitialNr result and the means in the shop data to calcualte how much time a machine is expected to process.
 double[] getExpectedProcessingTimes()
          Returns an array with the expectedProcssing time for each machine.
 int[] getFreeMachines()
          Returns a collection of all of the free machines at the current time.
 int[] getInitialNr()
          Returns the size of the shop as it was set by the problem size chooser.
 Operation[] getMachineActivities()
          Returns an array of Operation objects, with a length the same as the number of machines in the shop.
 int getNumFinishedJobs(int route)
          Returns the number of finsihed jobs on a particular route.
 java.util.Map getQMap()
          Returns a Qmap.
 java.util.Map getQPlusMap()
          Returns a Map whose keys are operation objects (meaning the queues waiting for each opeartion).
 java.util.Collection getSchedulableOperations()
          Returns a collection of the schedulable operations.
 java.util.Collection getSchedulableOperations(int machineNumber)
          Returns a collection of the schedulable operations for a specific machine.
 double getTime()
          Returns the time as registered with the state.
 int getTotalFinishedJobs()
          Returns the total number of finished jobs.
 void go()
          Start the simulation.
 boolean isMachineDone(int machineNumber)
          Returns true if the machine does not have anymore jobs left to do in the job shop.
 boolean isMachineStarved(int machineNumber)
          Returns true if the machine has jobs upstream (as specified in the QPlusMap) and it's buffers are empty.
protected  void notifyListeners(ShopChangeEvent sce)
          Iterate over all of the listeners and send the the ShopChangeEvent.
private  void performSchedulingCommand(ShopCommand sc)
          Changes the state of the shop accoring to the shop command.
 void setProblemSizeChooser(ProblemSizeChooser problemSizeChooser_)
          Set the ProblemSizeChooser.
 void setRandomTimeMaker(RandomTimeMaker randomTimeMaker_)
          Set the randomTimeMaker.
private  void setUp()
          Sets up toward running the simulation.
 java.lang.String toRichString()
          Returns a rich represntation of the shop.
 java.lang.String toString()
           
private  void updateMachines(int[] wakingMachines)
          Take array of indexes of machines that came off the eventQ.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

log

protected java.io.PrintWriter log
Message log.

shopData

protected ShopData shopData
The job shop data.

shopAlgorithm

protected ShopAlgorithm shopAlgorithm
The algorithm that is used for the run.

postRunAction

protected PostRunAction postRunAction
The PostRunAction, the doAction() method is called after every run of the shop.

problemSizeChooser

protected ProblemSizeChooser problemSizeChooser
The problem size chooser.

randomTimeMaker

protected RandomTimeMaker randomTimeMaker
The random time maker.

listeners

protected java.util.ArrayList listeners
A list of listeners that are notified whenever the state changes.

PROGRESS_STEP

protected static final long PROGRESS_STEP
Number of milliseconds between updates to log of '~' character showing that the simulation is running.

eventQ

protected EventQ eventQ
The EventQ

machines

private FastShopRun.Machine[] machines
An array of machine objects.

finishedJobs

private int[] finishedJobs
An array of integers counting how many finished jobs are in each route.

totalFinishedJobs

private int totalFinishedJobs
The total number of finsihed jobs (equals the sum of finishedJobs)

firstBuffers

private FastShopRun.Buffer[] firstBuffers
An array of references to the first buffer of each route. The length of this array is used to signify the number of routes.

time

protected double time
The system time

numBusyMachines

private int numBusyMachines
The number of busy machines.

Qmap

private java.util.HashMap Qmap
A map of the buffers where the keys are operations and the values are Integer objects. This is used by the clients of the ShopState. Unlike the values of numInQ variables in the buffers and machines, the Integer values in the Qmap include jobs that are being processed.

QPlusMap

private java.util.HashMap QPlusMap
A map of the buffers where the keys are operations and the values are Integer objects. This is used by the clients of the ShopState. The value assiciated with operation (r,o) is the number of jobs that on route r, that still hasn't completed operation (r,o).

startTimeMillis

private long startTimeMillis
The start time of the last time the go() method was called.

numSetJobs

private int[] numSetJobs
Stores the Nr[] array as it was defined by the problem size chooser.

expectedLowerBound

private double expectedLowerBound
The expected machine lower bound.

restingMap

java.util.HashMap restingMap
This is a map where the keys are Integers for machine numbers and the values are doubles that specify the time at which a machine was scheduled to rest.

lastRestUpdateTime

double lastRestUpdateTime
The last time during which the resting map was updated.
Constructor Detail

FastShopRun

public FastShopRun(ShopAlgorithm shopAlgorithm_,
                   ShopData shopData_,
                   PostRunAction postRunAction_,
                   java.io.PrintWriter log_)
Creates new FastShopRun.

FastShopRun

public FastShopRun(ShopAlgorithm shopAlgorithm_,
                   ShopData shopData_)
Creates a new FastShopRun, setting the log to stdout. And the PostRunAction to null
Method Detail

buildShop

private void buildShop()
Auxillary method for the constructors. Sets up all of the data structures of the Shop.

go

public void go()
Start the simulation.
Specified by:
go in interface ShopSimulation

updateMachines

private void updateMachines(int[] wakingMachines)
Take array of indexes of machines that came off the eventQ. Each such machine may have either just finished processing or was resting (or resting because the simualtion has just started). The datastructures are updated accordingly.

performSchedulingCommand

private void performSchedulingCommand(ShopCommand sc)
Changes the state of the shop accoring to the shop command.

setUp

private void setUp()
Sets up toward running the simulation.

askAgainAtCurrentTime

public boolean askAgainAtCurrentTime(int machineNumber)
Instructs the shop simulation to ask the algorithm for another ShopCommand for the specific machine number at the current simulation time. This is needed when the algorithm has issued a rest command for the machine and when called again at the current time, it decides that it would like the machine not to rest. Note that the shop simulation object will do the asking again only at cases where the machine is scheduled as restiong.

If the machine is not scheduled as resting at the current time, then the shop simulation will not ask the algorithm and will return a false value (other wise, it returns a true value).

Specified by:
askAgainAtCurrentTime in interface ShopSimulation

getTime

public double getTime()
Returns the time as registered with the state.
Specified by:
getTime in interface ShopState

getBusyMachines

public int[] getBusyMachines()
Returns a collection of all of the busy machines at the current time.
Specified by:
getBusyMachines in interface ShopState

getFreeMachines

public int[] getFreeMachines()
Returns a collection of all of the free machines at the current time.
Specified by:
getFreeMachines in interface ShopState

getSchedulableOperations

public java.util.Collection getSchedulableOperations()
Returns a collection of the schedulable operations.
Specified by:
getSchedulableOperations in interface ShopState

getSchedulableOperations

public java.util.Collection getSchedulableOperations(int machineNumber)
Returns a collection of the schedulable operations for a specific machine.
Specified by:
getSchedulableOperations in interface ShopState

getTotalFinishedJobs

public int getTotalFinishedJobs()
Returns the total number of finished jobs.
Specified by:
getTotalFinishedJobs in interface ShopState

getNumFinishedJobs

public int getNumFinishedJobs(int route)
Returns the number of finsihed jobs on a particular route. (Route index starting at 1)
Specified by:
getNumFinishedJobs in interface ShopState

isMachineDone

public boolean isMachineDone(int machineNumber)
Returns true if the machine does not have anymore jobs left to do in the job shop.
Specified by:
isMachineDone in interface ShopState

isMachineStarved

public boolean isMachineStarved(int machineNumber)
Returns true if the machine has jobs upstream (as specified in the QPlusMap) and it's buffers are empty.
Specified by:
isMachineStarved in interface ShopState

getQMap

public java.util.Map getQMap()
Returns a Qmap. Where the keys are operation objects and the values are Integer objects saying how many jobs are in each Q. The Qmap Integers include jobs that are in process (they are supposidly processed by the machine while they are still in the Q.
Specified by:
getQMap in interface ShopState

getQPlusMap

public java.util.Map getQPlusMap()
Returns a Map whose keys are operation objects (meaning the queues waiting for each opeartion). And whose values are Integer objects, the values of the map say how jobs are still to pass/complete operation (r,o).
Specified by:
getQPlusMap in interface ShopState

getInitialNr

public int[] getInitialNr()
Returns the size of the shop as it was set by the problem size chooser.
Specified by:
getInitialNr in interface ShopState

getExpectedProcessingTime

public double getExpectedProcessingTime(int machine)
Uses the getInitialNr result and the means in the shop data to calcualte how much time a machine is expected to process.
Specified by:
getExpectedProcessingTime in interface ShopState

getExpectedProcessingTimes

public double[] getExpectedProcessingTimes()
Returns an array with the expectedProcssing time for each machine.
Specified by:
getExpectedProcessingTimes in interface ShopState

getExpectedMachineLowerBound

public double getExpectedMachineLowerBound()
Returns the maximum of getExpectedProcessingTimes().
Specified by:
getExpectedMachineLowerBound in interface ShopState

getMachineActivities

public Operation[] getMachineActivities()
Returns an array of Operation objects, with a length the same as the number of machines in the shop. The i'th spot in this array says what machine i+1 is doing. If the spot is null, the machine is idle. If the spot has an operation object the machine is doing that operation.
Specified by:
getMachineActivities in interface ShopState

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toRichString

public java.lang.String toRichString()
Returns a rich represntation of the shop. Good for debugging.

addShopChangeListener

public void addShopChangeListener(ShopChangeListener scl)
Registers an additional shop change listner.
Specified by:
addShopChangeListener in interface ShopSimulation

notifyListeners

protected void notifyListeners(ShopChangeEvent sce)
Iterate over all of the listeners and send the the ShopChangeEvent.

setProblemSizeChooser

public void setProblemSizeChooser(ProblemSizeChooser problemSizeChooser_)
Set the ProblemSizeChooser.
Specified by:
setProblemSizeChooser in interface ShopSimulation

setRandomTimeMaker

public void setRandomTimeMaker(RandomTimeMaker randomTimeMaker_)
Set the randomTimeMaker.
Specified by:
setRandomTimeMaker in interface ShopSimulation