Skip to content

Commit e711f68

Browse files
authored
Merge pull request #1 from TunnRL/code_review
making code platform independent + fixing environment problem
2 parents 68c6b73 + 1635b3c commit e711f68

6 files changed

Lines changed: 51 additions & 17 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ DOI: XXXXXXXXXXXXXXXXXXXXXXXX
1414

1515
## Requirements and folder structure
1616

17-
Use the `requirements.txt` file to download the required packages to run the code. We recommend using a package management system like conda for this purpose.
17+
Use the `requirements.txt` or `environment.yaml` file to download the required packages to run the code. We recommend using a package management system like conda or pipenv for this purpose. Using conda:
18+
19+
1. Create an environment called `rl_cutter` using `environment.yaml` with the help of `conda`. If you get pip errors, install pip libraries manually, e.g. `pip install pandas`
20+
21+
```bash
22+
conda env create --file environment.yaml
23+
```
24+
25+
2. Activate the new environment with:
26+
27+
```bash
28+
conda activate rl_cutter
29+
```
1830

1931
The code framework depends on a certain folder structure. The python files should be placed in the main directory. The set up should be done in the following way:
2032
```
@@ -29,5 +41,5 @@ TunnRL_TBM_maintenance
2941
```
3042
Either set up the folder structure manually or on Linux run:
3143
```bash
32-
bash folder_structure.sh
44+
bash make_folder_structure.sh
3345
```

environment.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: rl_cutter
2+
channels:
3+
- conda-forge
4+
- defaults
5+
dependencies:
6+
- python=3.9.7
7+
- joblib=1.1.0
8+
- matplotlib=3.5.1
9+
- numpy
10+
- pandas
11+
- rich
12+
- pip
13+
- pip:
14+
- optuna==2.10.0
15+
- tensorforce
16+
- keras==2.6.0

make_folder_structure.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mkdir results checkpoints graphics

src/A_main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
import numpy as np
1717
import optuna
1818
import pandas as pd
19+
from pathlib import Path
1920
from tensorforce.agents import Agent
2021
from tensorforce.environments import Environment
22+
from rich.traceback import install
2123

2224
from XX_maintenance_lib import maintenance, CustomEnv, plotter
2325

26+
install() # for better error messages
2427

2528
###############################################################################
2629
# Constants and fixed variables
@@ -176,7 +179,7 @@ def objective(trial):
176179

177180
if OPTIMIZATION is True: # study
178181
try: # evtl. load already existing study if one exists
179-
study = joblib.load(fr'results\{STUDY}.pkl')
182+
study = joblib.load(Path(f'results/{STUDY}.pkl'))
180183
print('prev. study loaded')
181184
except FileNotFoundError: # or create a new study
182185
study = optuna.create_study(direction='maximize')
@@ -185,10 +188,10 @@ def objective(trial):
185188
# saved and can be checked every 2 trials
186189
for _ in range(200): # save every second study
187190
study.optimize(objective, n_trials=2)
188-
joblib.dump(study, fr"results\{STUDY}.pkl")
191+
joblib.dump(study, Path(f"results/{STUDY}.pkl"))
189192

190193
df = study.trials_dataframe()
191-
df.to_csv(fr'results\{STUDY}.csv')
194+
df.to_csv(Path(f'results/{STUDY}.csv'))
192195
# print results of study
193196
trial = study.best_trial
194197
print('\nhighest reward: {}'.format(trial.value))

src/B_optimization_analyzer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import matplotlib.pyplot as plt
1515
import numpy as np
1616
import pandas as pd
17+
from pathlib import Path
1718

1819

1920
###############################################################################
@@ -25,8 +26,8 @@
2526
# processing
2627

2728
# load data from completed OPTUNA study
28-
STUDY = fr"results\{name}.pkl" # name of the saved study file
29-
DF = fr'results\{name}.csv' # name of the csv. where logs from the study are
29+
STUDY = Path(f"results/{name}.pkl") # name of the saved study file
30+
DF = Path(f'results\{name}.csv') # name of the csv. where logs from the study are
3031
# load data
3132
df_study = pd.read_csv(DF)
3233
study = joblib.load(STUDY)
@@ -61,7 +62,7 @@
6162
ax.set_xlabel('trial number')
6263
ax.set_ylabel('reward')
6364
plt.tight_layout()
64-
plt.savefig(fr'graphics\{name}_optimization_progress.svg')
65+
plt.savefig(Path(f'graphics/{name}_optimization_progress.svg'))
6566
plt.close()
6667

6768
# scatterplot of indivdual hyperparameters vs. reward
@@ -91,7 +92,7 @@
9192
ax.set_xscale('log')
9293

9394
plt.tight_layout()
94-
plt.savefig(fr'graphics\{name}_optimization_scatter.svg')
95+
plt.savefig(Path(f'graphics/{name}_optimization_scatter.svg'))
9596
plt.close()
9697

9798
# plot of the progress of individual runs
@@ -114,5 +115,5 @@
114115
ax.set_xlabel('episodes')
115116
ax.set_ylabel('reward')
116117
plt.tight_layout()
117-
plt.savefig(fr'graphics\{name}_optimization_intermediates.svg')
118+
plt.savefig(Path(f'graphics/{name}_optimization_intermediates.svg'))
118119
plt.close()

src/XX_maintenance_lib.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import matplotlib.pyplot as plt
1616
import matplotlib.gridspec as gridspec
1717
import numpy as np
18+
from pathlib import Path
1819
from tensorforce.environments import Environment
1920

2021

@@ -105,8 +106,8 @@ def sample_ep_plot(self, states, actions, rewards, ep, savename):
105106
ax.grid(alpha=0.5)
106107

107108
plt.tight_layout()
108-
plt.savefig(fr'checkpoints\{savename}_sample.png', dpi=600)
109-
plt.savefig(fr'checkpoints\{savename}_sample.svg')
109+
plt.savefig(Path(f'checkpoints/{savename}_sample.png'), dpi=600)
110+
plt.savefig(Path(f'checkpoints/{savename}_sample.svg'))
110111
plt.close()
111112

112113
def trainingprogress_plot(self, df, summed_actions, name):
@@ -142,8 +143,8 @@ def trainingprogress_plot(self, df, summed_actions, name):
142143
ax3.set_xlabel('episodes')
143144

144145
plt.tight_layout()
145-
plt.savefig(fr'checkpoints\{name}_progress.png', dpi=600)
146-
plt.savefig(fr'checkpoints\{name}_progress.svg')
146+
plt.savefig(Path(f'checkpoints/{name}_progress.png'), dpi=600)
147+
plt.savefig(Path(f'checkpoints/{name}_progress.svg'))
147148
plt.close()
148149

149150

@@ -285,7 +286,7 @@ def save(self, AGENT, train_start, ep, states, actions, rewards, df,
285286
self.trainingprogress_plot(df, summed_actions, name)
286287

287288
agent.save(directory='checkpoints', filename=name, format='hdf5')
288-
df.to_csv(fr'checkpoints\{name}.csv', index=False)
289+
df.to_csv(Path(f'checkpoints/{name}.csv', index=False))
289290

290291

291292
if __name__ == "__main__":
@@ -314,7 +315,7 @@ def save(self, AGENT, train_start, ep, states, actions, rewards, df,
314315
ax.set_xlabel('cutters to change')
315316
ax.set_ylabel('total maintenance time [min]')
316317
plt.tight_layout()
317-
plt.savefig(r'graphics\cutter_changing_function.svg')
318+
plt.savefig(Path('graphics/cutter_changing_function.svg'))
318319

319320
##########################################################################
320321
# 3D plot of reward functions
@@ -366,4 +367,4 @@ def save(self, AGENT, train_start, ep, states, actions, rewards, df,
366367
ax.set_zlabel('reward')
367368
ax.set_zlim(top=1)
368369

369-
plt.savefig(r'graphics\reward_function.svg')
370+
plt.savefig(Path(f'graphics/reward_function.svg'))

0 commit comments

Comments
 (0)