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