1 package nl.tudelft.simulation.dsol.tutorial.section42;
2
3 import org.djutils.event.EventType;
4 import org.djutils.event.LocalEventProducer;
5
6 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
7 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
8 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
9 import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
10 import nl.tudelft.simulation.dsol.tutorial.section42.policies.OrderingPolicy;
11 import nl.tudelft.simulation.dsol.tutorial.section42.policies.StationaryPolicy;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class Retailer extends LocalEventProducer implements BuyerInterface, SellerInterface
25 {
26
27 private static final long serialVersionUID = 1L;
28
29
30 public static final EventType TOTAL_ORDERING_COST_EVENT = new EventType("TOTAL_ORDERING_COST_EVENT");
31
32
33 public static final EventType INVENTORY_LEVEL_EVENT = new EventType("INVENTORY_LEVEL_EVENT");
34
35
36 public static final EventType BACKLOG_LEVEL = new EventType("BACKLOG_LEVEL");
37
38
39 private long inventory = 60L;
40
41
42 private long backLog = 0L;
43
44
45 private DevsSimulatorInterface<Double> simulator = null;
46
47
48 private SellerInterface warehouse = null;
49
50
51 private OrderingPolicy orderingPolicy = null;
52
53
54 private double backlogCosts;
55
56
57 private double holdingCosts;
58
59
60 private double marginalCosts;
61
62
63 private double setupCosts;
64
65
66
67
68
69
70 public Retailer(final DevsSimulatorInterface<Double> simulator, final SellerInterface warehouse)
71 {
72 super();
73 this.simulator = simulator;
74 this.warehouse = warehouse;
75 this.orderingPolicy = new StationaryPolicy(simulator);
76 try
77 {
78 InputParameterMap parameters = simulator.getModel().getInputParameterMap();
79 this.backlogCosts = (Double) parameters.get("retailer.backlogCosts").getCalculatedValue();
80 this.holdingCosts = (Double) parameters.get("retailer.holdingCosts").getCalculatedValue();
81 this.marginalCosts = (Double) parameters.get("retailer.marginalCosts").getCalculatedValue();
82 this.setupCosts = (Double) parameters.get("retailer.setupCosts").getCalculatedValue();
83 }
84 catch (InputParameterException ipe)
85 {
86 ipe.printStackTrace();
87 System.exit(-1);
88 }
89 this.reviewInventory();
90 }
91
92
93 @Override
94 public void receiveProduct(final long amount)
95 {
96 long served = this.backLog - Math.max(0, this.backLog - amount);
97 this.backLog = Math.max(0, this.backLog - amount);
98 this.inventory = this.inventory + Math.max(0, amount - served);
99 this.fireTimedEvent(INVENTORY_LEVEL_EVENT, this.inventory, this.simulator.getSimulatorTime());
100 this.fireTimedEvent(BACKLOG_LEVEL, this.backLog, this.simulator.getSimulatorTime());
101 }
102
103
104
105
106 private void reviewInventory()
107 {
108 double costs = this.holdingCosts * this.inventory + this.backlogCosts * this.backLog;
109 long amount = this.orderingPolicy.computeAmountToOrder(this.inventory);
110 if (amount > 0)
111 {
112 costs = costs + this.setupCosts + amount * this.marginalCosts;
113 this.fireTimedEvent(TOTAL_ORDERING_COST_EVENT, costs, this.simulator.getSimulatorTime());
114 this.warehouse.order(this, amount);
115 }
116 try
117 {
118 this.simulator.scheduleEvent(
119 new SimEvent<Double>(this.simulator.getSimulatorTime() + 1.0, this,"reviewInventory", null));
120 }
121 catch (Exception exception)
122 {
123 this.simulator.getLogger().always().error(exception, "reviewInventory");
124 }
125 }
126
127
128 @Override
129 public void order(final BuyerInterface buyer, final long amount)
130 {
131 long actualOrderSize = Math.min(amount, this.inventory);
132 this.inventory = this.inventory - actualOrderSize;
133 if (actualOrderSize < amount)
134 {
135 this.backLog = this.backLog + (amount - actualOrderSize);
136 }
137 this.fireTimedEvent(INVENTORY_LEVEL_EVENT, this.inventory, this.simulator.getSimulatorTime());
138 this.fireTimedEvent(BACKLOG_LEVEL, this.backLog, this.simulator.getSimulatorTime());
139 buyer.receiveProduct(actualOrderSize);
140 }
141
142 }