Skip to content
Snippets Groups Projects
Commit 57db9529 authored by dansl's avatar dansl
Browse files

Commit Assignment 5 Sloan

parent 53f96992
No related branches found
No related tags found
No related merge requests found
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package mv3302;
import simkit.Entity;
/**
*
* @author dansl
*/
public class Ship extends Entity {
private double remainingUnloadingTime;
public Ship(double remainingUnloadingTime) {
super();
setRemainingUnloadingTime(remainingUnloadingTime);
}
public void work(double rate) {
if (rate < 0.0) {
throw new IllegalArgumentException("Input Rate must be > 0.0: " + rate);
}
remainingUnloadingTime -= getElapsedTime() * rate;
}
/**
* @return the remainingUnloadingTime
*/
public double getRemainingUnloadingTime() {
return remainingUnloadingTime;
}
/**
* @param remainingUnloadingTime the remainingUnloadingTime to set
*/
public void setRemainingUnloadingTime(double remainingUnloadingTime) {
this.remainingUnloadingTime = remainingUnloadingTime;
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package mv3302;
import simkit.random.RandomVariate;
/**
*
* @author dansl
*/
public class ShipArrivalProcess extends ArrivalProcess {
private RandomVariate interarrivalTimeGenerator;
private RandomVariate unloadingTimeGenerator;
public ShipArrivalProcess() {
}
public ShipArrivalProcess(RandomVariate interarrivalTimeGenerator, RandomVariate unloadingTimeGenerator) {
super(interarrivalTimeGenerator);
setInterarrivalTimeGenerator(interarrivalTimeGenerator);
setUnloadingTimeGenerator(unloadingTimeGenerator);
}
// public void doRun(){
// waitDelay(Arrival)
// }
@Override
public void doArrival() {
super.doArrival();
Ship ship = new Ship(unloadingTimeGenerator.generate());
waitDelay("ShipArrival", 0.0, ship);
}
/**
* @return the unloadingTimeGenerator
*/
public RandomVariate getUnloadingTimeGenerator() {
return unloadingTimeGenerator;
}
/**
* @param unloadingTimeGenerator the unloadingTimeGenerator to set
*/
public void setUnloadingTimeGenerator(RandomVariate unloadingTimeGenerator) {
this.unloadingTimeGenerator = unloadingTimeGenerator;
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package mv3302;
import static java.lang.Double.NaN;
import java.util.SortedSet;
import java.util.TreeSet;
import simkit.SimEntityBase;
/**
*
* @author dansl
*/
public class TwoCranesBerth extends SimEntityBase {
protected SortedSet<Ship> queue;
protected SortedSet<Ship> berths;
protected double delayInQueue;
protected double timeInSystem;
public TwoCranesBerth() {
queue = new TreeSet<>();
berths = new TreeSet<>();
}
@Override
public void reset() {
getQueue().clear();
delayInQueue = NaN;
timeInSystem = NaN;
}
public void doRun() {
queue.clear();
berths.clear();
delayInQueue = NaN;
timeInSystem = NaN;
}
public void doArrival(Ship ship) {
ship.stampTime();
queue.add(ship);
firePropertyChange("queue", getQueue());
if (berths.size() == 1) {
waitDelay("SwitchTo1Crane", 0.0);
}
if (berths.isEmpty()){
waitDelay("StartUnload2Cranes", 0.0);
}
}
public void doSwitchTo1Crane(){
Ship ship = berths.first();
ship.work(2);
ship.stampTime();
interrupt("EndUnload2Cranes");
waitDelay("StartUnload1Crane", 0.0);
System.out.println(ship.getRemainingUnloadingTime());
waitDelay("EndUnload1Crane", ship.getRemainingUnloadingTime(), ship);
}
public void doStartUnload1Crane(){
SortedSet<Ship> oldQueue = getQueue();
Ship ship = queue.first();
queue.remove(ship);
firePropertyChange("queue", oldQueue, getQueue());
double oldDelayInQueue = getDelayInQueue();
delayInQueue = ship.getElapsedTime();
firePropertyChange("delayInQueue", oldDelayInQueue, getDelayInQueue());
ship.stampTime();
SortedSet<Ship> oldBerths = getBerths();
berths.add(ship);
firePropertyChange("berths", oldBerths, getBerths());
waitDelay("EndUnload1Crane", ship.getRemainingUnloadingTime(), ship);
}
public void doStartUnload2Cranes(){
SortedSet<Ship> oldQueue = getQueue();
Ship ship = queue.first();
queue.remove(ship);
firePropertyChange("queue", oldQueue, getQueue());
double oldDelayInQueue = getDelayInQueue();
delayInQueue = ship.getElapsedTime();
firePropertyChange("delayInQueue", oldDelayInQueue, getDelayInQueue());
ship.stampTime();
SortedSet<Ship> oldBerths = getBerths();
berths.add(ship);
firePropertyChange("berths", oldBerths, getBerths());
waitDelay("EndUnload2Cranes", ship.getRemainingUnloadingTime()/2);
}
public void doEndUnload1Crane(Ship ship){
SortedSet<Ship> oldBerths = getBerths();
berths.remove(ship);
firePropertyChange("berths", oldBerths, getBerths());
double oldTimeInSystem = timeInSystem;
timeInSystem = ship.getAge();
firePropertyChange("timeInSystem", oldTimeInSystem, getTimeInSystem());
if (!queue.isEmpty()){
waitDelay("StartUnload1Crane", 0.0);
}
if (queue.isEmpty()){
waitDelay("SwitchTo2Cranes", 0.0);
}
}
public void doEndUnload2Cranes(){
SortedSet<Ship> oldBerths = getBerths();
Ship ship = berths.first();
berths.remove(ship);
firePropertyChange("berths", oldBerths, getBerths());
}
public void Switchto2Cranes(){
SortedSet<Ship> oldBerths = getBerths();
Ship ship = berths.first();
berths.remove(ship);
firePropertyChange("berths", oldBerths, getBerths());
ship.work(1);
ship.stampTime();
waitDelay("EndUnload2Cranes", ship.getRemainingUnloadingTime()/2);
interrupt("EndUnload1Crane", ship);
}
/**
* @return the queue
*/
public SortedSet<Ship> getQueue() {
return queue;
}
/**
* @return the berths
*/
public SortedSet<Ship> getBerths() {
return berths;
}
/**
* @return the delayInQueue
*/
public double getDelayInQueue() {
return delayInQueue;
}
/**
* @return the timeInSystem
*/
public double getTimeInSystem() {
return timeInSystem;
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package mv3302.run;
import mv3302.Ship;
import mv3302.ShipArrivalProcess;
import mv3302.TwoCranesBerth;
import simkit.Adapter;
import simkit.Schedule;
import simkit.SimEventListener;
import simkit.random.RandomVariate;
import simkit.random.RandomVariateFactory;
import simkit.util.SimplePropertyDumper;
/**
*
* @author dansl
*/
public class testShipArrivalProcess {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Ship ship = new Ship(5.0);
System.out.println(ship.getRemainingUnloadingTime());
ship.work(2);
System.out.println(ship.getRemainingUnloadingTime());
RandomVariate interarrivalTimeGenerator = RandomVariateFactory.getInstance("Exponential", .7);
RandomVariate unloadingTimeGenerator = RandomVariateFactory.getInstance("Uniform", 0.5, 1.5);
ShipArrivalProcess shipArrivalProcess = new ShipArrivalProcess(interarrivalTimeGenerator, unloadingTimeGenerator);
System.out.println(shipArrivalProcess);
TwoCranesBerth twoCranesBerth = new TwoCranesBerth();
System.out.println(twoCranesBerth);
// TwoCranesBerth will "hear" ShipArrivalProcess(ShipArrival) events
shipArrivalProcess.addSimEventListener(twoCranesBerth);
// The twoCranesBerth will "hear" the ShipArrival events of the
// first as Arrival events
Adapter adapter = new Adapter("ShipArrival", "Arrival");
adapter.connect(shipArrivalProcess, twoCranesBerth);
// This was used for debugging model
SimplePropertyDumper simplePropertyDumper = new SimplePropertyDumper(true);
twoCranesBerth.addPropertyChangeListener(simplePropertyDumper);
shipArrivalProcess.addPropertyChangeListener(simplePropertyDumper);
// Was "true" when debugging model
Schedule.setVerbose(true);
double stopTime = 10.0;
Schedule.stopAtTime(stopTime);
// Initialized & run
Schedule.reset();
Schedule.startSimulation();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment