cycle.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import multiprocessing
  2. import collections
  3. from synergine2.processing import ProcessManager
  4. from synergine2.simulation import Subject, Behaviour, Mechanism
  5. from synergine2.utils import ChunkManager
  6. class CycleManager(object):
  7. def __init__(
  8. self,
  9. subjects: list,
  10. process_manager: ProcessManager=None,
  11. ):
  12. if process_manager is None:
  13. process_manager = ProcessManager(
  14. process_count=multiprocessing.cpu_count(),
  15. chunk_manager=ChunkManager(multiprocessing.cpu_count()),
  16. job_maker=self.computing,
  17. )
  18. self.subjects = subjects
  19. self.process_manager = process_manager
  20. self.current_cycle = 0
  21. def next(self):
  22. results = {}
  23. results_by_processes = self.process_manager.execute_jobs(self.subjects)
  24. actions = collections.defaultdict(dict)
  25. for process_results in results_by_processes:
  26. results.update(process_results)
  27. for subject in self.subjects[:]: # Duplicate list to prevent conflicts with behaviours subjects manipulations
  28. for behaviour_class in results[subject.id]:
  29. # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
  30. # TODO: les behaviour_class ont le même uniqueid apres le process ?
  31. action_result = subject.behaviours[behaviour_class].action(results[subject.id][behaviour_class])
  32. actions[subject.id][behaviour_class] = action_result
  33. return actions
  34. def computing(self, subjects):
  35. # compute mechanisms (prepare check to not compute slienced or not used mechanisms)
  36. # compute behaviours with mechanisms data
  37. # return list with per subject: [behaviour: return, behaviour: return] if behaviour return True something
  38. results = {}
  39. for subject in subjects:
  40. mechanisms = self.get_mechanisms_to_compute(subject)
  41. mechanisms_data = {}
  42. behaviours_data = {}
  43. for mechanism in mechanisms:
  44. mechanisms_data[type(mechanism)] = mechanism.run()
  45. for behaviour in self.get_behaviours_to_compute(subject):
  46. # We identify behaviour data with it's class to be able to intersect it after subprocess data collect
  47. behaviour_data = behaviour.run(mechanisms_data)
  48. if behaviour_data:
  49. behaviours_data[type(behaviour)] = behaviour_data
  50. results[subject.id] = behaviours_data
  51. # TODO: Tester les performances si on utilise un index numerique pour les results[subject]
  52. # et behaviours_data[type(behaviours_data)]
  53. return results
  54. def get_mechanisms_to_compute(self, subject: Subject) -> [Mechanism]:
  55. # TODO: Implementer un systeme qui inhibe des mechanisme (ex. someil inhibe l'ouie)
  56. return subject.mechanisms.values()
  57. def get_behaviours_to_compute(self, subject: Subject) -> [Behaviour]:
  58. # TODO: Implementer un systeme qui inhibe des behaviours (ex. someil inhibe avoir faim)
  59. return subject.behaviours.values()