1 package org.opentrafficsim.demo;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashSet;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.function.Supplier;
9
10 import org.djunits.unit.DurationUnit;
11 import org.djunits.unit.LengthUnit;
12 import org.djunits.unit.SpeedUnit;
13 import org.djunits.unit.util.UNITS;
14 import org.djunits.value.vdouble.scalar.Direction;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Frequency;
17 import org.djunits.value.vdouble.scalar.Length;
18 import org.djunits.value.vdouble.scalar.Speed;
19 import org.djutils.draw.point.Point2d;
20 import org.opentrafficsim.base.parameters.ParameterException;
21 import org.opentrafficsim.base.parameters.ParameterSet;
22 import org.opentrafficsim.core.definitions.DefaultsNl;
23 import org.opentrafficsim.core.distributions.ConstantSupplier;
24 import org.opentrafficsim.core.distributions.FrequencyAndObject;
25 import org.opentrafficsim.core.distributions.ObjectDistribution;
26 import org.opentrafficsim.core.dsol.AbstractOtsModel;
27 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
28 import org.opentrafficsim.core.gtu.GtuType;
29 import org.opentrafficsim.core.idgenerator.IdSupplier;
30 import org.opentrafficsim.core.network.NetworkException;
31 import org.opentrafficsim.core.network.Node;
32 import org.opentrafficsim.core.network.route.FixedRouteGenerator;
33 import org.opentrafficsim.core.network.route.Route;
34 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
35 import org.opentrafficsim.road.definitions.DefaultsRoadNl;
36 import org.opentrafficsim.road.gtu.generator.GeneratorPositions;
37 import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator;
38 import org.opentrafficsim.road.gtu.generator.TtcRoomChecker;
39 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuTemplate;
40 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuTemplateDistribution;
41 import org.opentrafficsim.road.gtu.generator.headway.HeadwayGenerator;
42 import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractIdm;
43 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.Lmrs;
44 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
45 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory.Setting;
46 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
47 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
48 import org.opentrafficsim.road.network.RoadNetwork;
49 import org.opentrafficsim.road.network.factory.LaneFactory;
50 import org.opentrafficsim.road.network.lane.CrossSectionLink;
51 import org.opentrafficsim.road.network.lane.Lane;
52 import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
53 import org.opentrafficsim.road.network.lane.LanePosition;
54 import org.opentrafficsim.road.network.lane.LaneType;
55 import org.opentrafficsim.road.network.lane.object.detector.SinkDetector;
56 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
57 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
58
59 import nl.tudelft.simulation.dsol.SimRuntimeException;
60 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
61 import nl.tudelft.simulation.jstats.distributions.DistUniform;
62 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
63 import nl.tudelft.simulation.jstats.streams.StreamInterface;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public class StraightModel extends AbstractOtsModel implements UNITS
85 {
86
87 private final RoadNetwork network = new RoadNetwork("network", getSimulator());
88
89
90 private double carProbability;
91
92
93 private TrafficLight block = null;
94
95
96 private Length maximumDistance = new Length(5000, METER);
97
98
99 private Lane lane;
100
101
102 private StreamInterface stream = new MersenneTwister(12345);
103
104
105 private List<Lane> path = new ArrayList<>();
106
107
108 private Speed speedLimit = new Speed(120, KM_PER_HOUR);
109
110
111
112
113
114 public StraightModel(final OtsSimulatorInterface simulator)
115 {
116 super(simulator);
117 InputParameterHelper.makeInputParameterMapCarTruck(this.inputParameterMap, 1.0);
118 }
119
120 @Override
121 public final void constructModel() throws SimRuntimeException
122 {
123 try
124 {
125 Node from = new Node(this.network, "From", new Point2d(0.0, 0), Direction.ZERO);
126 Node to = new Node(this.network, "To", new Point2d(this.maximumDistance.getSI(), 0), Direction.ZERO);
127 Node end = new Node(this.network, "End", new Point2d(this.maximumDistance.getSI() + 50.0, 0), Direction.ZERO);
128 LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
129 this.lane = LaneFactory.makeLane(this.network, "Lane", from, to, null, laneType, this.speedLimit, this.simulator,
130 DefaultsNl.VEHICLE);
131 this.path.add(this.lane);
132 CrossSectionLink endLink = LaneFactory.makeLink(this.network, "endLink", to, end, null, this.simulator);
133
134 Lane sinkLane = LaneGeometryUtil.createStraightLane(endLink, "sinkLane", this.lane.getLateralCenterPosition(1.0),
135 this.lane.getLateralCenterPosition(1.0), this.lane.getWidth(1.0), this.lane.getWidth(1.0), laneType,
136 Map.of(DefaultsNl.VEHICLE, this.speedLimit));
137 new SinkDetector(sinkLane, new Length(10.0, METER), DefaultsNl.ROAD_USERS);
138 this.path.add(sinkLane);
139
140 this.carProbability = (double) getInputParameter("generic.carProbability");
141
142
143 TtcRoomChecker roomChecker = new TtcRoomChecker(new Duration(10.0, DurationUnit.SI));
144 IdSupplier idGenerator = new IdSupplier("");
145 ParameterSet params = new ParameterSet();
146 params.setDefaultParameter(AbstractIdm.DELTA);
147 GtuType car = DefaultsNl.CAR;
148 GtuType truck = DefaultsNl.TRUCK;
149 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> speedCar =
150 new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 90.0, 110.0), SpeedUnit.KM_PER_HOUR);
151 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> speedTruck =
152 new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 80, 95), SpeedUnit.KM_PER_HOUR);
153 Supplier<Route> routeGenerator = new FixedRouteGenerator(null);
154 LaneBasedStrategicalPlannerFactory<?> strategicalPlannerFactoryCars = new LaneBasedStrategicalRoutePlannerFactory(
155 new LmrsFactory<>(Lmrs::new).setStream(this.stream).set(Setting.ACCELERATION_TRAFFIC_LIGHTS, true));
156 LaneBasedStrategicalPlannerFactory<?> strategicalPlannerFctoryTrucks = new LaneBasedStrategicalRoutePlannerFactory(
157 new LmrsFactory<>(Lmrs::new).setStream(this.stream).set(Setting.ACCELERATION_TRAFFIC_LIGHTS, true));
158 LaneBasedGtuTemplate carTemplate = new LaneBasedGtuTemplate(car, new ConstantSupplier<>(Length.ofSI(4.0)),
159 new ConstantSupplier<>(Length.ofSI(2.0)), speedCar, strategicalPlannerFactoryCars, routeGenerator);
160 LaneBasedGtuTemplate truckTemplate = new LaneBasedGtuTemplate(truck, new ConstantSupplier<>(Length.ofSI(15.0)),
161 new ConstantSupplier<>(Length.ofSI(2.5)), speedTruck, strategicalPlannerFctoryTrucks, routeGenerator);
162 ObjectDistribution<LaneBasedGtuTemplate> gtuTypeDistribution = new ObjectDistribution<>(this.stream);
163 gtuTypeDistribution.add(new FrequencyAndObject<>(this.carProbability, carTemplate));
164 gtuTypeDistribution.add(new FrequencyAndObject<>(1.0 - this.carProbability, truckTemplate));
165 Supplier<Duration> headwayGenerator =
166 new HeadwayGenerator(new Frequency(1500.0, PER_HOUR), new MersenneTwister(4L));
167 Set<LanePosition> initialLongitudinalPositions = new LinkedHashSet<>();
168 initialLongitudinalPositions.add(new LanePosition(this.lane, new Length(5.0, LengthUnit.SI)));
169 LaneBasedGtuTemplateDistribution characteristicsGenerator =
170 new LaneBasedGtuTemplateDistribution(gtuTypeDistribution);
171 new LaneBasedGtuGenerator("Generator", headwayGenerator, characteristicsGenerator,
172 GeneratorPositions.create(initialLongitudinalPositions, this.stream), this.network, getSimulator(),
173 roomChecker, idGenerator);
174
175
176 this.block =
177 new TrafficLight(this.lane.getId() + "_TL", this.lane, new Length(new Length(4000.0, LengthUnit.METER)));
178 this.block.setTrafficLightColor(TrafficLightColor.GREEN);
179
180 this.simulator.scheduleEventAbs(Duration.ofSI(300.0), () -> createBlock());
181
182 this.simulator.scheduleEventAbs(Duration.ofSI(420.0), () -> removeBlock());
183 }
184 catch (SimRuntimeException | NetworkException | InputParameterException | ParameterException exception)
185 {
186 exception.printStackTrace();
187 }
188 }
189
190
191
192
193 protected final void createBlock()
194 {
195 this.block.setTrafficLightColor(TrafficLightColor.RED);
196 }
197
198
199
200
201 protected final void removeBlock()
202 {
203 this.block.setTrafficLightColor(TrafficLightColor.GREEN);
204 }
205
206 @Override
207 public RoadNetwork getNetwork()
208 {
209 return this.network;
210 }
211
212
213
214
215
216 public final List<Lane> getPath()
217 {
218 return this.path;
219 }
220
221 }