experiment.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Import code for model simulation, but using the minimal kernel:
  16. from pypdevs.minimal import Simulator
  17. # Import the model to be simulated
  18. from model import TrafficSystem
  19. # ======================================================================
  20. # 1. Instantiate the (Coupled or Atomic) DEVS at the root of the
  21. # hierarchical model. This effectively instantiates the whole model
  22. # thanks to the recursion in the DEVS model constructors (__init__).
  23. #
  24. trafficSystem = TrafficSystem(name="trafficSystem")
  25. # ======================================================================
  26. # 2. Link the model to a DEVS Simulator:
  27. # i.e., create an instance of the 'Simulator' class,
  28. # using the model as a parameter.
  29. sim = Simulator(trafficSystem)
  30. # ======================================================================
  31. # 3. Perform all necessary configurations, with the minimal kernel, only setTerminationTime is supported.
  32. # e.g. to simulate until simulation time 400.0 is reached
  33. sim.setTerminationTime(400.0)
  34. # ======================================================================
  35. # 4. Simulate the model
  36. sim.simulate()
  37. # ======================================================================
  38. # 5. (optional) Extract data from the simulated model
  39. print("Simulation terminated with traffic light in state %s" % (trafficSystem.trafficLight.state.get()))