Skip to content
Snippets Groups Projects
Commit 91417fac authored by shinedday's avatar shinedday
Browse files

Add : Amas can now generate csv each cycle

parent 4d4a3f42
No related branches found
No related tags found
No related merge requests found
...@@ -221,3 +221,6 @@ class Agent: ...@@ -221,3 +221,6 @@ class Agent:
if other is None: if other is None:
return False return False
return self.__id == other.__id return self.__id == other.__id
def __repr__(self):
return str(self.__id)
...@@ -7,8 +7,10 @@ from typing import List ...@@ -7,8 +7,10 @@ from typing import List
import sys import sys
import pathlib import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.tools.loggable import Loggable
from pyAmakCore.classes.schedulable import Schedulable from pyAmakCore.classes.schedulable import Schedulable
from pyAmakCore.classes.scheduler import Scheduler from pyAmakCore.classes.scheduler import Scheduler
from pyAmakCore.classes.environment import Environment from pyAmakCore.classes.environment import Environment
...@@ -17,14 +19,15 @@ from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy ...@@ -17,14 +19,15 @@ from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
from pyAmakCore.exception.override import ToOverrideWarning from pyAmakCore.exception.override import ToOverrideWarning
class Amas(Schedulable): class Amas(Schedulable, Loggable):
""" """
Amas Class Amas Class
""" """
def __init__(self, environment: Environment) -> None: def __init__(self, environment: Environment) -> None:
super().__init__() Schedulable.__init__(self)
Loggable.__init__(self)
self.__environment: Environment = environment self.__environment: Environment = environment
self.__agents: List[Agent] = [] self.__agents: List[Agent] = []
self.__nbrcycle: int = 0 self.__nbrcycle: int = 0
...@@ -116,7 +119,6 @@ class Amas(Schedulable): ...@@ -116,7 +119,6 @@ class Amas(Schedulable):
Main behavior of Amas Main behavior of Amas
""" """
print("Cycle : ", self.__nbrcycle) print("Cycle : ", self.__nbrcycle)
self.__nbrcycle += 1
self.on_cycle_begin() self.on_cycle_begin()
self.synchronization() self.synchronization()
...@@ -144,6 +146,8 @@ class Amas(Schedulable): ...@@ -144,6 +146,8 @@ class Amas(Schedulable):
self.synchronization() self.synchronization()
self.on_cycle_end() self.on_cycle_end()
self.to_csv(self.get_cycle(), self.get_agents())
self.__nbrcycle += 1
def put_token(self) -> None: def put_token(self) -> None:
""" """
......
"""
class allowing to save the state of the system at a given moment
"""
from os import path
from pandas import DataFrame
class Loggable:
"""
class Loggable
"""
def __init__(self):
self.__do_log = False
self.__file_path = None
def to_csv(self, cycle, var_list):
"""
get cycle and agent list and print them
"""
if not self.__do_log:
return
ignore_attribute = ["_Agent__amas", "_Agent__environment"]
table = [{**{e: x[e] for e in x if e not in ignore_attribute},
**{'nombre_cycle': cycle}} for x in map(vars, var_list)]
dataframe = DataFrame(table)
if self.__file_path is None:
print(dataframe.to_csv(index=False))
else:
if path.exists(self.__file_path):
dataframe.to_csv(path_or_buf=self.__file_path, mode='a', header=False, index=False)
else:
dataframe.to_csv(path_or_buf=self.__file_path, index=False)
def set_do_log(self, boolean):
self.__do_log = boolean
def set_file_path(self, path_to_file):
self.__file_path = path_to_file
import pandas
from pyAmakCore.classes.amas import Amas
from pyAmakCore.classes.environment import Environment
from pyAmakCore.classes.agent import Agent
class SimpleAgent(Agent):
"""
test
"""
class SimpleAmas(Amas):
"""
test
"""
def on_initial_agents_creation(self) -> None:
for i in range(10):
self.add_agent(Agent(self))
def to_csv(self):
ignore_attribute = ["_Agent__amas", "_Agent__environment"]
table = [{**{e: x[e] for e in x if e not in ignore_attribute},
**{'nombre_cycle': self.get_cycle()}} for x in map(vars, self.get_agents())]
dataframe = pandas.DataFrame(table)
print(dataframe.to_csv(index=False))
class SimpleEnv(Environment):
"""
test
"""
env = SimpleEnv()
amas = SimpleAmas(env)
amas.to_csv()
"""
There are no visible memory leak in pyAmakCore
"""
\ No newline at end of file
pandas
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment