simulation.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import random
  2. from synergine2.config import Config
  3. from synergine2.simulation import SubjectMechanism, SubjectBehaviour, Event, Subject
  4. def compute(complexity: int):
  5. random_floats = []
  6. result = 0.0
  7. for i in range(complexity):
  8. random_floats.append(random.uniform(0, 100))
  9. for j, random_float in enumerate(random_floats):
  10. if not j % 2:
  11. result += random_float
  12. else:
  13. result -= random_float
  14. return result
  15. class ComputeMechanism(SubjectMechanism):
  16. def run(self):
  17. complexity = self.config.get('complexity')
  18. value = compute(complexity)
  19. return {
  20. 'mechanism_value': value,
  21. 'complexity': complexity,
  22. }
  23. class ComplexityEvent(Event):
  24. def __init__(self, complexity, *args, **kwargs):
  25. self.complexity = complexity
  26. class ComputeBehaviour(SubjectBehaviour):
  27. use = [ComputeMechanism]
  28. def run(self, data):
  29. return not data.get(ComputeMechanism).get('mechanism_value') % 2
  30. def action(self, data):
  31. mechanism_value = data.get(ComputeMechanism).get('mechanism_value')
  32. complexity = data.get(ComputeMechanism).get('complexity')
  33. if not int(str(mechanism_value)[-1]) % 2:
  34. compute(complexity)
  35. return [Event(complexity)]
  36. class ComputeSubject(Subject):
  37. behaviours_classes = [ComputeBehaviour]
  38. def __init__(
  39. self,
  40. config: Config,
  41. simulation: 'Simulation',
  42. data,
  43. ):
  44. super().__init__(config, simulation)
  45. self.data = data