experiment.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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:
  16. from pypdevs.simulator 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, the most commonly used are:
  32. # A. Termination time (or termination condition)
  33. # Using a termination condition will execute a provided function at
  34. # every simulation step, making it possible to check for certain states
  35. # being reached.
  36. # It should return True to stop simulation, or Falso to continue.
  37. def terminate_whenStateIsReached(clock, model):
  38. return model.trafficLight.state.get() == "manual"
  39. sim.setTerminationCondition(terminate_whenStateIsReached)
  40. # A termination time is prefered over a termination condition,
  41. # as it is much simpler to use.
  42. # e.g. to simulate until simulation time 400.0 is reached
  43. sim.setTerminationTime(400.0)
  44. # B. Set the use of a tracer to show what happened during the simulation run
  45. # Both writing to stdout or file is possible:
  46. # pass None for stdout, or a filename for writing to that file
  47. sim.setVerbose(None)
  48. # C. Use Classic DEVS instead of Parallel DEVS
  49. # If your model uses Classic DEVS, this configuration MUST be set as
  50. # otherwise errors are guaranteed to happen.
  51. # Without this option, events will be remapped and the select function
  52. # will never be called.
  53. sim.setClassicDEVS()
  54. # ======================================================================
  55. # 4. Simulate the model
  56. sim.simulate()
  57. # ======================================================================
  58. # 5. (optional) Extract data from the simulated model
  59. print("Simulation terminated with traffic light in state %s" % (trafficSystem.trafficLight.state.get()))