Package: crop-coach
CropCoachDeveloped by Noureddine Ech-chouky, AgriEdge(c) 2022Initialing CropCoach with default parameters:importgym# -- Initializing the environment : with the default parametersenv=gym.make("CropCoach-v0")Examples of How To Use (Alpha Version)Initiation the environmentimportgym# -- Initializing the environment : with the default parametersenv=gym.make("CropCoach-v0")# -- Sample from observation space :print(env.observation_space.sample())# -- Sample from action space :print(env.action_space.sample())Simple Rl loopimportgymimportCropCoachimportshutupshutup.please()# -- Define the env :env=gym.make("CropCoach-v0")# -- Test the env : the rewarding mecanismepisodes=10forepisodeinrange(1,episodes+1):state=env.reset()done=Falsescore=0whilenotdone:# env.render()action=env.action_space.sample()# action = np.array([1000])print(f"----------- action :{action}-------------\n")n_state,reward,done,info=env.step(action)print(f"----------- done :{done}-------------\n")print(f"----------- state :{n_state}-------------\n")print(f"----------- reward :{reward}-------------\n")score+=rewardprint(f"Episode --:{episode}Score --:{score}")Train agents Using stable baselines3 : you need to install stable baselines3 and configure wandb platform, see :my wofost project# -- Importing dependencies :importgymfromCropCoachimportnumpyasnpimportrandomimportosimporttimefrompprintimportpprintfromstable_baselines3.common.callbacksimportBaseCallbackfromstable_baselines3importA2C,PPOdefcreate_list_dir(paths_list):forpathinpaths_list:ifnotos.path.exists(path):os.makedirs(path)# logs folder :fromdatetimeimportdatetime# datetime object containing current date and timenow=datetime.now().strftime("%a_%b_%Y_%I_%M_%p")Test_Objective="Default_PPO_vs_A2C_MultiDiscrete"logs_dir=f"logs_{Test_Objective}_{now}"models_dir=f"models_{Test_Objective}_{now}"paths_list=[logs_dir,models_dir]# create the logs and models folders :create_list_dir(paths_list)# # -- Init the env  :env=gym.make("wofost-v0")# -- Use weights and biases track training and evaluation :importwandbfromwandb.integration.sb3importWandbCallback# -- Init wandb configuration :config={"policy_type":"MlpPolicy","total_timesteps":10000}experiment_name=f"{Test_Objective}_{now}"run=wandb.init(name=experiment_name,project="Paper_Experiments",config=config,sync_tensorboard=True,# auto-upload sb3's tensorboard metricssave_code=True,# optional)TIMESTEPS=config["total_timesteps"]a2c_agent=A2C("MlpPolicy",env,tensorboard_log=logs_dir,verbose=1)ppo_agent=PPO("MlpPolicy",env,tensorboard_log=logs_dir,verbose=1)print("Training the A2C agent...")a2c_agent.learn(total_timesteps=TIMESTEPS,tb_log_name="A2C",callback=WandbCallback(gradient_save_freq=2,log="all",verbose=1,),)a2c_agent.save(f"{models_dir}/a2c_{TIMESTEPS}")print("Done training the A2C agent")print("Training the PPO agent...")ppo_agent.learn(total_timesteps=TIMESTEPS,tb_log_name="PPO",callback=WandbCallback(gradient_save_freq=2,log="all",verbose=1,),)ppo_agent.save(f"{models_dir}/ppo_{TIMESTEPS}")print("Done training the PPO agent")run.finish()env.close()Changing the default parameters :importgymimportcrop_coachimportshutupshutup.please()crop_path="C:/Users/noureddine/Desktop/default_data/crop/crop.cab"site_path="C:/Users/noureddine/Desktop/default_data/site/site.cab"soil_path="C:/Users/noureddine/Desktop/default_data/soil/soil.cab"args={"files_paths":{"site":site_path,"soil":soil_path,"crop":crop_path},"sample_year":True,"Agromanager_dict":{"crop_name":"wheat","crop_variety":"Winter_wheat_101","campaign_start_date":"-01-01","crop_start_type":"emergence","emergence_date":"-03-31","crop_end_type":"harvest","harvest_date":"-08-11","max_duration":300},"Costs_dict":{"Irrigation":150,"N":8,"P":8.5,"K":7,"Selling":2.5},"Discount_factors_dict":{"Irrigation":1,"N":1,"P":1,"K":1},"year":2019,"years_count":1,}# -- Define the env :env=gym.make("CropCoach-v0",**args)# -- Test the env : the rewarding mecanismepisodes=10forepisodeinrange(1,episodes+1):state=env.reset()done=Falsescore=0whilenotdone:env.render()action=env.action_space.sample()# action = np.array([1000])print(f"----------- action :{action}-------------\n")n_state,reward,done,info=env.step(action)print(f"----------- done :{done}-------------\n")print(f"----------- state :{n_state}-------------\n")print(f"----------- reward :{reward}-------------\n")score+=rewardprint(f"Episode --:{episode}Score --:{score}")