In [ ]:
from spamdfba import toolkit as tk
from spamdfba import toymodels as tm
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import warnings
import multiprocessing as mp
import json
2023-10-31 10:44:38,664 INFO worker.py:1642 -- Started a local Ray instance.
Set parameter Username Academic license - for non-commercial use only - expires 2024-02-17
WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this. WARNING:cobra.medium.boundary_types:Could not identify an external compartment by name and choosing one with the most boundary reactions. That might be complete nonsense or change suddenly. Consider renaming your compartments using `Model.compartments` to fix this.
In [ ]:
NUM_CORES = 8
warnings.filterwarnings("ignore")
In [ ]:
agent1=tk.Agent("agent1",
model=tm.Toy_Model_NE_Aux_1,
actor_network=tk.NN,
critic_network=tk.NN,
clip=0.1,
lr_actor=0.0001,
lr_critic=0.001,
grad_updates=10,
optimizer_actor=torch.optim.Adam,
optimizer_critic=torch.optim.Adam,
observables=['agent1','agent2','S',"A","B"],
actions=['A_e','B_e'],
gamma=1,
)
agent2=tk.Agent("agent2",
model=tm.Toy_Model_NE_Aux_2,
actor_network=tk.NN,
critic_network=tk.NN,
clip=0.1,
lr_actor=0.0001,
lr_critic=0.001,
grad_updates=10,
optimizer_actor=torch.optim.Adam,
optimizer_critic=torch.optim.Adam,
observables=['agent1','agent2','S',"A","B"],
actions=['A_e','B_e'],
gamma=1)
agents=[agent1,agent2]
env_aux=tk.Environment(name="Toy-NECOM-Auxotrophs",
agents=agents,
dilution_rate=0.0001,
extracellular_reactions=[],
initial_condition={"S":100,"agent1":0.1,"agent2":0.1},
inlet_conditions={"S":100},
dt=0.1,
number_of_batches=5000,
episodes_per_batch=int(NUM_CORES/2),)
In [ ]:
sim_aux=tk.Simulation(name=env_aux.name,
env=env_aux,
save_dir="./Results/",
)
In [ ]:
sim_aux.run(verbose=False)
In [ ]:
fig_aux=sim_aux.plot_learning_curves()
In [ ]:
fig_aux.write_image(env_aux.name+"_learning_curve.svg")
In [ ]:
time_aux=sim_aux.print_training_times()
Simulation times ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Level ┃ Mean(s) ┃ STD(s) ┃ ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩ │ Optimization │ 0.00032502077644999 │ 6.097844251016754e-05 │ │ Step │ 0.0011583550271250106 │ 0.00022706778960370164 │ │ Batch │ 0.3642500801999978 │ 0.048618568158497945 │ │ Simulation │ 2561.5439219997766 │ NA │ └──────────────┴───────────────────────┴────────────────────────┘
In [ ]:
with open(env_aux.name+"_Runtime.json","w") as f:
json.dump(time_aux,f)
In [ ]:
agent1=tk.Agent("agent1",
model=tm.Toy_Model_NE_Mut_1,
actor_network=tk.NN,
critic_network=tk.NN,
clip=0.1,
lr_actor=0.0001,
lr_critic=0.001,
grad_updates=10,
optimizer_actor=torch.optim.Adam,
optimizer_critic=torch.optim.Adam,
observables=['agent1','agent2','S',"A","B"],
actions=['A_e','B_e'],
gamma=1,
)
agent2=tk.Agent("agent2",
model=tm.Toy_Model_NE_Mut_2,
actor_network=tk.NN,
critic_network=tk.NN,
clip=0.1,
lr_actor=0.0001,
lr_critic=0.001,
grad_updates=10,
optimizer_actor=torch.optim.Adam,
optimizer_critic=torch.optim.Adam,
observables=['agent1','agent2','S',"A","B"],
actions=['A_e','B_e'],
gamma=1)
agents=[agent1,agent2]
env_fac=tk.Environment(name="Toy-NECOM-Facultative",
agents=agents,
dilution_rate=0.0001,
extracellular_reactions=[],
initial_condition={"S":100,"agent1":0.1,"agent2":0.1,"A":0,"B":0},
inlet_conditions={"S":100},
dt=0.1,
number_of_batches=5000,
episodes_per_batch=4,)
In [ ]:
sim_fac=tk.Simulation(name=env_fac.name,
env=env_fac,
save_dir="./Results/",
)
In [ ]:
sim_fac.run(verbose=False)
In [ ]:
fig_fac=sim_fac.plot_learning_curves()
In [ ]:
fig_fac.write_image(env_fac.name+"_learning_curve.svg")
In [ ]:
time_fac=sim_fac.print_training_times()
Simulation times ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Level ┃ Mean(s) ┃ STD(s) ┃ ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩ │ Optimization │ 0.00035886619994984233 │ 2.4024276334344793e-05 │ │ Step │ 0.0012223559303251473 │ 7.378304576128785e-05 │ │ Batch │ 0.38118410700000394 │ 0.040204447228493395 │ │ Simulation │ 2658.2730999999026 │ NA │ └──────────────┴────────────────────────┴────────────────────────┘
In [ ]:
with open(env_fac.name+"_Runtime.json","w") as f:
json.dump(time_fac,f)