diff --git a/MV3302ClassCode/src/mv3302/Ship.java b/MV3302ClassCode/src/mv3302/Ship.java new file mode 100644 index 0000000000000000000000000000000000000000..2098bd386f0995ec98b32c9f7c53651758c7a0de --- /dev/null +++ b/MV3302ClassCode/src/mv3302/Ship.java @@ -0,0 +1,42 @@ +/* + * 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; + } +} diff --git a/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java b/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java new file mode 100644 index 0000000000000000000000000000000000000000..f714e555c70e00110e8abc48b920d968f5f94975 --- /dev/null +++ b/MV3302ClassCode/src/mv3302/ShipArrivalProcess.java @@ -0,0 +1,53 @@ +/* + * 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; + } +} diff --git a/MV3302ClassCode/src/mv3302/TwoCranesBerth.java b/MV3302ClassCode/src/mv3302/TwoCranesBerth.java new file mode 100644 index 0000000000000000000000000000000000000000..24286c70bdc741ac4549a11a00a28b616ae64f3c --- /dev/null +++ b/MV3302ClassCode/src/mv3302/TwoCranesBerth.java @@ -0,0 +1,154 @@ +/* + * 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; + } +} diff --git a/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java b/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java new file mode 100644 index 0000000000000000000000000000000000000000..5d099a2356ae15a21a1c51699ccfb52e2df0adc0 --- /dev/null +++ b/MV3302ClassCode/src/mv3302/run/testShipArrivalProcess.java @@ -0,0 +1,66 @@ +/* + * 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(); + + } + +}