simulation.py 1.3KB

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