ui export
This commit is contained in:
@@ -25,6 +25,7 @@ from stable_baselines3.common.callbacks import BaseCallback
|
||||
import yaml
|
||||
import time
|
||||
from threading import Thread
|
||||
from db.export_to_mongo import MongoExporter
|
||||
|
||||
env_config = {
|
||||
# ===== SCENARIO CLASS =====
|
||||
@@ -52,7 +53,7 @@ env_config = {
|
||||
# ===== SCENARIO CLASS ARGUMENTS =====
|
||||
# (optional) kwargs that are added by the Scenario class (i.e. not defined in BaseEnvironment)
|
||||
|
||||
'starting_agent_coin': 20,
|
||||
'starting_agent_coin': 50,
|
||||
'fixed_four_skill_and_loc': True,
|
||||
|
||||
# ===== STANDARD ARGUMENTS ======
|
||||
@@ -82,63 +83,8 @@ env_config = {
|
||||
}
|
||||
|
||||
|
||||
eval_env_config = {
|
||||
# ===== SCENARIO CLASS =====
|
||||
# Which Scenario class to use: the class's name in the Scenario Registry (foundation.scenarios).
|
||||
# The environment object will be an instance of the Scenario class.
|
||||
'scenario_name': 'econ',
|
||||
|
||||
# ===== COMPONENTS =====
|
||||
# Which components to use (specified as list of ("component_name", {component_kwargs}) tuples).
|
||||
# "component_name" refers to the Component class's name in the Component Registry (foundation.components)
|
||||
# {component_kwargs} is a dictionary of kwargs passed to the Component class
|
||||
# The order in which components reset, step, and generate obs follows their listed order below.
|
||||
'components': [
|
||||
# (1) Building houses
|
||||
('Craft', {'skill_dist': "pareto", 'commodities': ["Gem"],'max_skill_amount_benefit':2}),
|
||||
# (2) Trading collectible resources
|
||||
('ContinuousDoubleAuction', {'max_num_orders': 10}),
|
||||
# (3) Movement and resource collection
|
||||
('SimpleGather', {}),
|
||||
('ExternalMarket',{'market_demand':{
|
||||
'Gem': 15
|
||||
}}),
|
||||
],
|
||||
|
||||
# ===== SCENARIO CLASS ARGUMENTS =====
|
||||
# (optional) kwargs that are added by the Scenario class (i.e. not defined in BaseEnvironment)
|
||||
|
||||
'starting_agent_coin': 20,
|
||||
'fixed_four_skill_and_loc': True,
|
||||
|
||||
# ===== STANDARD ARGUMENTS ======
|
||||
# kwargs that are used by every Scenario class (i.e. defined in BaseEnvironment)
|
||||
'agent_composition': {"BasicMobileAgent": 20,"TradingAgent":5}, # Number of non-planner agents (must be > 1)
|
||||
'world_size': [1, 1], # [Height, Width] of the env world
|
||||
'episode_length': 256, # Number of timesteps per episode
|
||||
'allow_observation_scaling': True,
|
||||
'isoelastic_eta':0.001,
|
||||
'dense_log_frequency': 1,
|
||||
'world_dense_log_frequency':1,
|
||||
'energy_cost':0,
|
||||
'energy_warmup_method': "auto",
|
||||
'energy_warmup_constant': 4000,
|
||||
|
||||
# In multi-action-mode, the policy selects an action for each action subspace (defined in component code).
|
||||
# Otherwise, the policy selects only 1 action.
|
||||
'multi_action_mode_agents': False,
|
||||
'multi_action_mode_planner': False,
|
||||
|
||||
# When flattening observations, concatenate scalar & vector observations before output.
|
||||
# Otherwise, return observations with minimal processing.
|
||||
'flatten_observations': False,
|
||||
# When Flattening masks, concatenate each action subspace mask into a single array.
|
||||
# Note: flatten_masks = True is required for masking action logits in the code below.
|
||||
'flatten_masks': True,
|
||||
}
|
||||
|
||||
num_frames=1
|
||||
|
||||
CONNECTION_STRING = "mongodb://root:econ@localhost:27017/"
|
||||
class TensorboardCallback(BaseCallback):
|
||||
"""
|
||||
Custom callback for plotting additional values in tensorboard.
|
||||
@@ -238,7 +184,6 @@ def printReplay(econ,agentid):
|
||||
|
||||
#Setup Env Objects
|
||||
econ=foundation.make_env_instance(**env_config)
|
||||
|
||||
market=econ.get_component("ContinuousDoubleAuction")
|
||||
action=market.get_n_actions("TradingAgent")
|
||||
baseEconWrapper=BaseEconWrapper(econ)
|
||||
@@ -246,8 +191,8 @@ baseEconWrapper.run()
|
||||
time.sleep(0.5)
|
||||
mobileRecieverEconWrapper=RecieverEconWrapper(base_econ=baseEconWrapper,agent_classname="BasicMobileAgent")
|
||||
tradeRecieverEconWrapper=RecieverEconWrapper(base_econ=baseEconWrapper,agent_classname="TradingAgent")
|
||||
sb3_traderConverter=SB3EconConverter(tradeRecieverEconWrapper,econ,"TradingAgent",True)
|
||||
sb3Converter=SB3EconConverter(mobileRecieverEconWrapper,econ,"BasicMobileAgent",True)
|
||||
sb3_traderConverter=SB3EconConverter(tradeRecieverEconWrapper,econ,"TradingAgent",False)
|
||||
sb3Converter=SB3EconConverter(mobileRecieverEconWrapper,econ,"BasicMobileAgent",False)
|
||||
# attach sb3 wrappers
|
||||
|
||||
monenv=VecMonitor(venv=sb3Converter,info_keywords=["social/productivity","trend/productivity"])
|
||||
@@ -257,28 +202,8 @@ stackenv_basic=vec_frame_stack.VecFrameStack(venv=monenv,n_stack=num_frames)
|
||||
stackenv_traid=vec_frame_stack.VecFrameStack(venv=montraidingenv,n_stack=num_frames)
|
||||
# Model setup complete
|
||||
|
||||
# Setup Eval Env
|
||||
econ_eval=foundation.make_env_instance(**eval_env_config)
|
||||
|
||||
|
||||
baseEconWrapper_eval=BaseEconWrapper(econ_eval)
|
||||
baseEconWrapper_eval.run()
|
||||
time.sleep(0.5)
|
||||
mobileRecieverEconWrapper_eval=RecieverEconWrapper(base_econ=baseEconWrapper_eval,agent_classname="BasicMobileAgent")
|
||||
tradeRecieverEconWrapper_eval=RecieverEconWrapper(base_econ=baseEconWrapper_eval,agent_classname="TradingAgent")
|
||||
sb3_traderConverter_eval=SB3EconConverter(tradeRecieverEconWrapper_eval,econ_eval,"TradingAgent",False)
|
||||
sb3Converter_eval=SB3EconConverter(mobileRecieverEconWrapper_eval,econ_eval,"BasicMobileAgent",False)
|
||||
# attach sb3 wrappers
|
||||
|
||||
monenv_eval=VecMonitor(venv=sb3Converter_eval,info_keywords=["social/productivity","trend/productivity"])
|
||||
montraidingenv_eval=VecMonitor(venv=sb3_traderConverter_eval)
|
||||
|
||||
stackenv_basic_eval=vec_frame_stack.VecFrameStack(venv=monenv_eval,n_stack=num_frames)
|
||||
stackenv_traid_eval=vec_frame_stack.VecFrameStack(venv=montraidingenv_eval,n_stack=num_frames)
|
||||
|
||||
|
||||
obs=monenv.reset()
|
||||
|
||||
obs=montraidingenv.reset()
|
||||
|
||||
# define training functions
|
||||
def train(model,timesteps, econ_call,process_bar,name,db,index):
|
||||
@@ -297,39 +222,41 @@ model_trade=MaskablePPO("MlpPolicy",n_steps=int(env_config['episode_length']*2),
|
||||
# Setup complete
|
||||
|
||||
# Load models
|
||||
model.load("basic.ai")
|
||||
model_trade.load("trade.ai")
|
||||
model=model.load("basic.ai")
|
||||
model_trade=model_trade.load("trade.ai")
|
||||
|
||||
exporter=MongoExporter(CONNECTION_STRING,"econ",econ)
|
||||
|
||||
while True:
|
||||
print("### EVAL ###")
|
||||
obs_basic=stackenv_basic_eval.reset()
|
||||
obs_trade=stackenv_traid_eval.reset()
|
||||
obs_basic=stackenv_basic.reset()
|
||||
obs_trade=stackenv_traid.reset()
|
||||
done=False
|
||||
for i in tqdm(range(eval_env_config['episode_length'])):
|
||||
for i in tqdm(range(env_config['episode_length'])):
|
||||
#create masks
|
||||
masks_basic=stackenv_basic_eval.action_masks()
|
||||
masks_trade=stackenv_traid_eval.action_masks()
|
||||
masks_basic=stackenv_basic.action_masks()
|
||||
masks_trade=stackenv_traid.action_masks()
|
||||
# get actions
|
||||
action_basic=model.predict(obs_basic,action_masks=masks_basic)
|
||||
action_trade=model_trade.predict(obs_trade,action_masks=masks_trade)
|
||||
#submit async directly for non blocking operation
|
||||
sb3Converter_eval.step_async(action_basic[0])
|
||||
sb3_traderConverter_eval.step_async(action_trade[0])
|
||||
sb3Converter.step_async(action_basic[0])
|
||||
sb3_traderConverter.step_async(action_trade[0])
|
||||
# retieve full results
|
||||
obs_basic,rew_basic,done_e,info=stackenv_basic_eval.step(action_basic[0])
|
||||
obs_trade,rew_trade,done_e,info=stackenv_traid_eval.step(action_trade[0])
|
||||
obs_basic,rew_basic,done_e,info=stackenv_basic.step(action_basic[0])
|
||||
obs_trade,rew_trade,done_e,info=stackenv_traid.step(action_trade[0])
|
||||
done=done_e[0]
|
||||
|
||||
|
||||
|
||||
market=econ_eval.get_component("ContinuousDoubleAuction")
|
||||
craft=econ_eval.get_component("Craft")
|
||||
exporter.submit_full_market()
|
||||
market=econ.get_component("ContinuousDoubleAuction")
|
||||
craft=econ.get_component("Craft")
|
||||
# trades=market.get_dense_log()
|
||||
build=craft.get_dense_log()
|
||||
met=econ_eval.previous_episode_metrics
|
||||
printReplay(econ_eval,21)
|
||||
# printMarket(trades)
|
||||
# printBuilds(builds=build)
|
||||
met=econ.previous_episode_metrics
|
||||
printReplay(econ,21)
|
||||
#printMarket(trades)
|
||||
#printBuilds(builds=build)
|
||||
print("social/productivity: {}".format(met["social/productivity"]))
|
||||
print("labor/weighted_cost: {}".format(met["labor/weighted_cost"]))
|
||||
print("labor/warmup_integrator: {}".format(met["labor/warmup_integrator"]))
|
||||
|
||||
Reference in New Issue
Block a user