flopyarcade 0.7.2__tar.gz → 0.7.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flopyarcade-0.7.2/flopyarcade.egg-info → flopyarcade-0.7.3}/PKG-INFO +7 -4
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/README.md +5 -2
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/__init__.py +1 -1
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/agent.py +55 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/arcade.py +9 -3
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/env_base.py +9 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_dqn.py +12 -1
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/play.py +10 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/render.py +137 -11
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/render3d.py +12 -6
- flopyarcade-0.7.3/flopyarcade/simulators/simulators_linux.zip +0 -0
- flopyarcade-0.7.3/flopyarcade/simulators/simulators_mac.zip +0 -0
- flopyarcade-0.7.3/flopyarcade/simulators/simulators_win32.zip +0 -0
- flopyarcade-0.7.3/flopyarcade/simulators/simulators_win64.zip +0 -0
- flopyarcade-0.7.3/flopyarcade/tests/conftest.py +23 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_agent_dqn.py +123 -21
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_final_push.py +1 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_play_actions_obs.py +1 -1
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_render.py +2 -2
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_mock_rllib.py +11 -4
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_reward_reset.py +12 -7
- flopyarcade-0.7.3/flopyarcade/tests/test_smooth_trajectory.py +161 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3/flopyarcade.egg-info}/PKG-INFO +7 -4
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade.egg-info/SOURCES.txt +6 -1
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/pyproject.toml +2 -2
- flopyarcade-0.7.2/flopyarcade/tests/conftest.py +0 -18
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/LICENSE +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/MANIFEST.in +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/actions.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/archive_hdf5.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/benchmark.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/env_config.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/__init__.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/1s-d/1s-d +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/1s-d/1s-d.tune_metadata +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/2s-d/2s-d +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/2s-d/2s-d.tune_metadata +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/3s-d/3s-d +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/policymodels/3s-d/3s-d.tune_metadata +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_neuroevolution.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_rllib_apexdqn.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_rllib_appo.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_rllib_impala.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/examples/train_rllib_ppo.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/flopyarcade.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/mf6_mp7_runner.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/mf6_prt_runner.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/modflow_runner.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/modpath_runner.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/observations.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/play3d.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/play_againstai.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/reward.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_a10_overlap.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_agent_neuroevolution.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_archive_hdf5.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_blit.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_c2_novelty_length.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_c7_action_space.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_agent2.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_agent_bench.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_big.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_envbase.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_final_push2.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_modflow_deep.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_play_archive.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_play_rl.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_push2.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_cov_runners_actions.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_90.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_deep.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_final.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_gaps.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_gaps2.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_max.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_coverage_push.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_fps.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_mf6_runners.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_play.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_regression.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_regression_ci.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_render3d.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade/tests/test_rllib_cpu.py +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade.egg-info/dependency_links.txt +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade.egg-info/entry_points.txt +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade.egg-info/requires.txt +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/flopyarcade.egg-info/top_level.txt +0 -0
- {flopyarcade-0.7.2 → flopyarcade-0.7.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flopyarcade
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.3
|
|
4
4
|
Summary: Simulated groundwater flow environments for reinforcement learning.
|
|
5
5
|
Author-email: Philipp Hoehn <philipp.hoehn@yahoo.com>
|
|
6
6
|
License: GNU GPLv3
|
|
@@ -20,7 +20,7 @@ Classifier: Topic :: Scientific/Engineering
|
|
|
20
20
|
Classifier: Topic :: Scientific/Engineering :: Hydrology
|
|
21
21
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
22
|
Classifier: Topic :: Education
|
|
23
|
-
Requires-Python: <3.13,>=3.
|
|
23
|
+
Requires-Python: <3.13,>=3.10
|
|
24
24
|
Description-Content-Type: text/markdown
|
|
25
25
|
License-File: LICENSE
|
|
26
26
|
Requires-Dist: flopy>=3.9.2
|
|
@@ -195,7 +195,7 @@ flopyarcade/ # CORE — Gymnasium environments
|
|
|
195
195
|
### Install
|
|
196
196
|
|
|
197
197
|
```bash
|
|
198
|
-
# Python 3.
|
|
198
|
+
# Python >= 3.10 (numpy 2.x requirement)
|
|
199
199
|
# Environments only (lightweight, ~200 MB):
|
|
200
200
|
python -m pip install flopyarcade
|
|
201
201
|
|
|
@@ -220,10 +220,13 @@ from numpy.random import choice
|
|
|
220
220
|
|
|
221
221
|
env = FloPyEnv(ENVTYPE='3s-d')
|
|
222
222
|
reward_total = 0.0
|
|
223
|
+
|
|
223
224
|
while not env.done:
|
|
224
225
|
action = choice(env.actionSpace)
|
|
225
|
-
obs, reward,
|
|
226
|
+
obs, reward, *rest = env.step(action)
|
|
226
227
|
reward_total += reward
|
|
228
|
+
env.render()
|
|
229
|
+
|
|
227
230
|
print(f"Episode reward: {reward_total:.1f}")
|
|
228
231
|
env.close()
|
|
229
232
|
```
|
|
@@ -139,7 +139,7 @@ flopyarcade/ # CORE — Gymnasium environments
|
|
|
139
139
|
### Install
|
|
140
140
|
|
|
141
141
|
```bash
|
|
142
|
-
# Python 3.
|
|
142
|
+
# Python >= 3.10 (numpy 2.x requirement)
|
|
143
143
|
# Environments only (lightweight, ~200 MB):
|
|
144
144
|
python -m pip install flopyarcade
|
|
145
145
|
|
|
@@ -164,10 +164,13 @@ from numpy.random import choice
|
|
|
164
164
|
|
|
165
165
|
env = FloPyEnv(ENVTYPE='3s-d')
|
|
166
166
|
reward_total = 0.0
|
|
167
|
+
|
|
167
168
|
while not env.done:
|
|
168
169
|
action = choice(env.actionSpace)
|
|
169
|
-
obs, reward,
|
|
170
|
+
obs, reward, *rest = env.step(action)
|
|
170
171
|
reward_total += reward
|
|
172
|
+
env.render()
|
|
173
|
+
|
|
171
174
|
print(f"Episode reward: {reward_total:.1f}")
|
|
172
175
|
env.close()
|
|
173
176
|
```
|
|
@@ -256,6 +256,55 @@ class FloPyAgent:
|
|
|
256
256
|
self.replayMemory = deque(maxlen=self.hyParams['REPLAYMEMORYSIZE'])
|
|
257
257
|
self.epsilon = self.hyParams['EPSILONINITIAL']
|
|
258
258
|
self.targetUpdateCount = 0
|
|
259
|
+
self.dqnGameCounter = 0
|
|
260
|
+
|
|
261
|
+
def save_dqn_checkpoint(self, checkpoint_dir=None):
|
|
262
|
+
"""Save DQN training state to a checkpoint directory."""
|
|
263
|
+
if checkpoint_dir is None:
|
|
264
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
265
|
+
checkpoint_dir = join(self.wrkspc, 'dqn_checkpoint', timestamp)
|
|
266
|
+
self._create_folder(checkpoint_dir)
|
|
267
|
+
|
|
268
|
+
self.mainModel.save_weights(join(checkpoint_dir, 'mainModel.weights.h5'))
|
|
269
|
+
|
|
270
|
+
if hasattr(self, 'targetModel') and self.targetModel is not None:
|
|
271
|
+
self.targetModel.save_weights(join(checkpoint_dir, 'targetModel.weights.h5'))
|
|
272
|
+
|
|
273
|
+
self.pickleDump(join(checkpoint_dir, 'replayMemory.p'), list(self.replayMemory))
|
|
274
|
+
|
|
275
|
+
state = {
|
|
276
|
+
'epsilon': self.epsilon,
|
|
277
|
+
'gameCounter': self.dqnGameCounter,
|
|
278
|
+
'targetUpdateCount': self.targetUpdateCount,
|
|
279
|
+
}
|
|
280
|
+
self.pickleDump(join(checkpoint_dir, 'state.p'), state)
|
|
281
|
+
|
|
282
|
+
print(f'DQN checkpoint saved to {checkpoint_dir}')
|
|
283
|
+
return checkpoint_dir
|
|
284
|
+
|
|
285
|
+
def load_dqn_checkpoint(self, checkpoint_dir):
|
|
286
|
+
"""Load DQN training state from a checkpoint directory."""
|
|
287
|
+
if not exists(checkpoint_dir):
|
|
288
|
+
raise FileNotFoundError(f'Checkpoint directory not found: {checkpoint_dir}')
|
|
289
|
+
|
|
290
|
+
self.mainModel.load_weights(join(checkpoint_dir, 'mainModel.weights.h5'))
|
|
291
|
+
|
|
292
|
+
target_weights_path = join(checkpoint_dir, 'targetModel.weights.h5')
|
|
293
|
+
if exists(target_weights_path):
|
|
294
|
+
self.targetModel.load_weights(target_weights_path)
|
|
295
|
+
else:
|
|
296
|
+
self.targetModel.set_weights(self.mainModel.get_weights())
|
|
297
|
+
|
|
298
|
+
replay_list = self.pickleLoad(join(checkpoint_dir, 'replayMemory.p'))
|
|
299
|
+
self.replayMemory = deque(maxlen=self.hyParams['REPLAYMEMORYSIZE'])
|
|
300
|
+
self.replayMemory.extend(replay_list)
|
|
301
|
+
|
|
302
|
+
state = self.pickleLoad(join(checkpoint_dir, 'state.p'))
|
|
303
|
+
self.epsilon = state['epsilon']
|
|
304
|
+
self.dqnGameCounter = state['gameCounter']
|
|
305
|
+
self.targetUpdateCount = state['targetUpdateCount']
|
|
306
|
+
|
|
307
|
+
print(f'DQN checkpoint loaded from {checkpoint_dir}')
|
|
259
308
|
|
|
260
309
|
def initializeGeneticAgents(self):
|
|
261
310
|
if self.envSettings['KEEPMODELHISTORY']:
|
|
@@ -306,6 +355,12 @@ class FloPyAgent:
|
|
|
306
355
|
|
|
307
356
|
self._decay_epsilon()
|
|
308
357
|
|
|
358
|
+
# periodic checkpointing
|
|
359
|
+
self.dqnGameCounter += 1
|
|
360
|
+
checkpoint_every = self.hyParams.get('CHECKPOINTEVERY', 0)
|
|
361
|
+
if checkpoint_every > 0 and self.dqnGameCounter % checkpoint_every == 0:
|
|
362
|
+
self.save_dqn_checkpoint()
|
|
363
|
+
|
|
309
364
|
def runGenetic(self, env, searchNovelty=False):
|
|
310
365
|
"""Run main pipeline for genetic agent optimization."""
|
|
311
366
|
self._validate_hyperparams()
|
|
@@ -156,7 +156,8 @@ class FloPyArcade:
|
|
|
156
156
|
PATHMP6=None, surrogateSimulator=None, flagSavePlot=False,
|
|
157
157
|
flagManualControl=False, actions=None, flagRender=False,
|
|
158
158
|
keepTimeSeries=False, ENVTYPE='1s-d', nLay=1, nRow=100,
|
|
159
|
-
nCol=100, OBSPREP='perceptron', flagBlit=True
|
|
159
|
+
nCol=100, OBSPREP='perceptron', flagBlit=True,
|
|
160
|
+
smoothFrames=4, smoothSubdiv=10):
|
|
160
161
|
"""Constructor.
|
|
161
162
|
|
|
162
163
|
Args:
|
|
@@ -185,14 +186,15 @@ class FloPyArcade:
|
|
|
185
186
|
agent, modelNameLoad, modelName, animationFolder, NAGENTSTEPS,
|
|
186
187
|
PATHMF2005, PATHMP6, surrogateSimulator, flagSavePlot,
|
|
187
188
|
flagManualControl, actions, flagRender, keepTimeSeries,
|
|
188
|
-
ENVTYPE, nLay, nRow, nCol, OBSPREP, flagBlit
|
|
189
|
+
ENVTYPE, nLay, nRow, nCol, OBSPREP, flagBlit,
|
|
190
|
+
smoothFrames, smoothSubdiv
|
|
189
191
|
)
|
|
190
192
|
|
|
191
193
|
def _init_config(self, agent, modelNameLoad, modelName, animationFolder,
|
|
192
194
|
NAGENTSTEPS, PATHMF2005, PATHMP6, surrogateSimulator,
|
|
193
195
|
flagSavePlot, flagManualControl, actions, flagRender,
|
|
194
196
|
keepTimeSeries, ENVTYPE, nLay, nRow, nCol, OBSPREP,
|
|
195
|
-
flagBlit=True):
|
|
197
|
+
flagBlit=True, smoothFrames=4, smoothSubdiv=10):
|
|
196
198
|
"""Initialize configuration for FloPyArcade.
|
|
197
199
|
|
|
198
200
|
Args:
|
|
@@ -209,6 +211,8 @@ class FloPyArcade:
|
|
|
209
211
|
self.MANUALCONTROL = flagManualControl
|
|
210
212
|
self.RENDER = flagRender
|
|
211
213
|
self.BLIT = flagBlit
|
|
214
|
+
self.SMOOTH_FRAMES = smoothFrames
|
|
215
|
+
self.SMOOTH_SUBDIV = smoothSubdiv
|
|
212
216
|
self.MODELNAME = modelName or modelNameLoad or "untitled"
|
|
213
217
|
self.ANIMATIONFOLDER = (
|
|
214
218
|
animationFolder or modelName or modelNameLoad or "untitled"
|
|
@@ -276,6 +280,8 @@ class FloPyArcade:
|
|
|
276
280
|
nCol=self.nCol, OBSPREP=self.OBSPREP, initWithSolution=True
|
|
277
281
|
)
|
|
278
282
|
self.env._blit_enabled = self.BLIT
|
|
283
|
+
self.env.SMOOTH_TRAJ_FRAMES = self.SMOOTH_FRAMES
|
|
284
|
+
self.env.SMOOTH_TRAJ_SUBDIV = self.SMOOTH_SUBDIV
|
|
279
285
|
else:
|
|
280
286
|
self.env = FloPyEnvSurrogate(
|
|
281
287
|
self.SURROGATESIMULATOR, ENVTYPE, MODELNAME=self.MODELNAME,
|
|
@@ -277,6 +277,9 @@ class EnvBaseMixin:
|
|
|
277
277
|
self._blit_enabled = False
|
|
278
278
|
self._blit_artists = {}
|
|
279
279
|
self._fps = 0.0
|
|
280
|
+
self.SMOOTH_TRAJ_FRAMES = 4
|
|
281
|
+
self.SMOOTH_TRAJ_SUBDIV = 10
|
|
282
|
+
self._prevParticleCoords = None
|
|
280
283
|
from collections import deque as _deque
|
|
281
284
|
self._frame_times = _deque(maxlen=60)
|
|
282
285
|
self.renderFlag = self.RENDER
|
|
@@ -1913,6 +1916,12 @@ class EnvBaseMixin:
|
|
|
1913
1916
|
self.render_blit_frame()
|
|
1914
1917
|
else:
|
|
1915
1918
|
self.fig.canvas.draw()
|
|
1919
|
+
# B1: smooth particle transition between steps
|
|
1920
|
+
if getattr(self, 'SMOOTH_TRAJ_FRAMES', 1) > 1:
|
|
1921
|
+
self.render_particle_transition(
|
|
1922
|
+
n_frames=self.SMOOTH_TRAJ_FRAMES,
|
|
1923
|
+
pause_sec=0.03
|
|
1924
|
+
)
|
|
1916
1925
|
self.renderFPS()
|
|
1917
1926
|
show(block=False)
|
|
1918
1927
|
pause(self.MANUALCONTROLTIME)
|
|
@@ -96,6 +96,10 @@ parser.add_argument('--crossvalidateevery', default=50, type=int,
|
|
|
96
96
|
help='integer defining the number of games between cross-validation')
|
|
97
97
|
parser.add_argument('--ngamescrossvalidated', default=10, type=int,
|
|
98
98
|
help='integer defining the number of games during cross-validation')
|
|
99
|
+
parser.add_argument('--checkpointevery', default=10, type=int,
|
|
100
|
+
help='integer defining the number of games between automatic checkpoints (0 to disable)')
|
|
101
|
+
parser.add_argument('--resume', default=None, type=str,
|
|
102
|
+
help='path to checkpoint directory to resume training from')
|
|
99
103
|
args, _ = parser.parse_known_args()
|
|
100
104
|
|
|
101
105
|
|
|
@@ -142,7 +146,9 @@ hyParams = {
|
|
|
142
146
|
'EPSILONDECAY': args.epsilondecay, # float defining the factor to decay the exploration fraction
|
|
143
147
|
'EPSILONMIN': args.epsilonmin, # float defining the lowest exploration fraction following decay
|
|
144
148
|
'CROSSVALIDATEEVERY': args.crossvalidateevery, # integer defining the number of games between cross-validation
|
|
145
|
-
'NGAMESCROSSVALIDATED': args.ngamescrossvalidated
|
|
149
|
+
'NGAMESCROSSVALIDATED': args.ngamescrossvalidated, # integer defining the number of games during cross-validation
|
|
150
|
+
'CHECKPOINTEVERY': args.checkpointevery, # integer defining games between automatic checkpoints
|
|
151
|
+
'RESUME': args.resume # path to checkpoint directory for resuming
|
|
146
152
|
}
|
|
147
153
|
|
|
148
154
|
|
|
@@ -166,6 +172,11 @@ def main(envSettings, hyParams):
|
|
|
166
172
|
'DQN')
|
|
167
173
|
agent.GPUAllowMemoryGrowth()
|
|
168
174
|
|
|
175
|
+
# loading checkpoint if resuming
|
|
176
|
+
if hyParams.get('RESUME'):
|
|
177
|
+
agent.load_dqn_checkpoint(hyParams['RESUME'])
|
|
178
|
+
print(f'Resumed from game {agent.dqnGameCounter}')
|
|
179
|
+
|
|
169
180
|
# running training of DQN agent
|
|
170
181
|
agent.runDQN(env)
|
|
171
182
|
|
|
@@ -59,6 +59,8 @@ def main(
|
|
|
59
59
|
'MANUALCONTROL': False,
|
|
60
60
|
'RENDER': True,
|
|
61
61
|
'BLIT': True,
|
|
62
|
+
'SMOOTH_FRAMES': 4,
|
|
63
|
+
'SMOOTH_SUBDIV': 10,
|
|
62
64
|
'MODELNAMELOAD': None,
|
|
63
65
|
'MODELNAME': 'FloPyArcade',
|
|
64
66
|
'PATHMF2005': None,
|
|
@@ -89,6 +91,10 @@ def main(
|
|
|
89
91
|
help='boolean defining if displaying runs')
|
|
90
92
|
parser.add_argument('--blit', default=True, type=str_to_bool,
|
|
91
93
|
help='boolean enabling matplotlib blitting for 60+ FPS (default: True)')
|
|
94
|
+
parser.add_argument('--smooth-frames', default=4, type=int,
|
|
95
|
+
help='number of sub-frames for smooth particle transition (default: 4, set 1 to disable)')
|
|
96
|
+
parser.add_argument('--smooth-subdiv', default=10, type=int,
|
|
97
|
+
help='PCHIP subdivision factor for trajectory smoothing (default: 10)')
|
|
92
98
|
parser.add_argument('--modelnameload', default=None, type=str,
|
|
93
99
|
help='string defining model to load')
|
|
94
100
|
parser.add_argument('--modelname', default='FloPyArcade', type=str,
|
|
@@ -119,6 +125,8 @@ def main(
|
|
|
119
125
|
'MANUALCONTROL': args.manualcontrol, # boolean defining manual control model
|
|
120
126
|
'RENDER': args.render, # boolean defining if displaying runs
|
|
121
127
|
'BLIT': args.blit, # boolean enabling matplotlib blitting
|
|
128
|
+
'SMOOTH_FRAMES': args.smooth_frames, # int sub-frames for smooth particle
|
|
129
|
+
'SMOOTH_SUBDIV': args.smooth_subdiv, # int PCHIP subdivision factor
|
|
122
130
|
'MODELNAMELOAD': args.modelnameload, # string defining model to load
|
|
123
131
|
'MODELNAME': args.modelname, # string defining model basename
|
|
124
132
|
'PATHMF2005': args.pathmf2005, # string defining local path to MODFLOW 2005 executable
|
|
@@ -146,6 +154,8 @@ def main(
|
|
|
146
154
|
flagManualControl=envSettings['MANUALCONTROL'],
|
|
147
155
|
flagRender=envSettings['RENDER'],
|
|
148
156
|
flagBlit=envSettings['BLIT'],
|
|
157
|
+
smoothFrames=envSettings['SMOOTH_FRAMES'],
|
|
158
|
+
smoothSubdiv=envSettings['SMOOTH_SUBDIV'],
|
|
149
159
|
nLay=envSettings['NLAY'],
|
|
150
160
|
nRow=envSettings['NROW'],
|
|
151
161
|
nCol=envSettings['NCOL'],
|
|
@@ -83,6 +83,10 @@ if 'ipykernel' in modules:
|
|
|
83
83
|
from IPython import display
|
|
84
84
|
from time import sleep, time
|
|
85
85
|
from xmipy import XmiWrapper
|
|
86
|
+
try:
|
|
87
|
+
from scipy.interpolate import PchipInterpolator as _Pchip
|
|
88
|
+
except ImportError:
|
|
89
|
+
_Pchip = None
|
|
86
90
|
|
|
87
91
|
import flopy.utils.binaryfile as bf
|
|
88
92
|
|
|
@@ -529,14 +533,16 @@ class RenderMixin:
|
|
|
529
533
|
zorder (int, optional): Drawing order for the plot. Defaults to 5.
|
|
530
534
|
|
|
531
535
|
"""
|
|
536
|
+
# B15: ideal (max-reward) line is now a solid, more prominent guide
|
|
537
|
+
# instead of a faint dashed reference.
|
|
532
538
|
self.ax2.plot(
|
|
533
539
|
[self.minX, self.minX + self.extentX],
|
|
534
540
|
[self.particleY, self.particleY],
|
|
535
|
-
lw=
|
|
541
|
+
lw=2.5,
|
|
536
542
|
c='white',
|
|
537
|
-
ls='
|
|
543
|
+
ls='-',
|
|
538
544
|
zorder=zorder,
|
|
539
|
-
alpha=0.
|
|
545
|
+
alpha=0.85
|
|
540
546
|
)
|
|
541
547
|
|
|
542
548
|
def renderContourLines(self, n=30, zorder=4):
|
|
@@ -859,8 +865,15 @@ class RenderMixin:
|
|
|
859
865
|
zorder (int): Drawing order for the particle.
|
|
860
866
|
|
|
861
867
|
"""
|
|
862
|
-
|
|
863
|
-
|
|
868
|
+
# B15: the initial spawn marker (timeStep==0) recedes visually
|
|
869
|
+
# (thin + translucent) so the live particle + trajectory dominate.
|
|
870
|
+
if self.timeStep == 0:
|
|
871
|
+
linewidth = 1.5
|
|
872
|
+
alpha = 0.35
|
|
873
|
+
else:
|
|
874
|
+
linewidth = 2
|
|
875
|
+
alpha = 1.0
|
|
876
|
+
self.ax2.scatter(x, y, lw=linewidth, c='red', zorder=zorder, alpha=alpha)
|
|
864
877
|
|
|
865
878
|
def renderParticleTrajectory(self, zorder=6):
|
|
866
879
|
"""Plot particle trajectory up to the current state.
|
|
@@ -1218,8 +1231,56 @@ class RenderMixin:
|
|
|
1218
1231
|
self.ax2.transData.transform([(x, y)])
|
|
1219
1232
|
)
|
|
1220
1233
|
|
|
1234
|
+
def _smooth_segment(self, x_raw, y_raw, n_subdiv=10):
|
|
1235
|
+
"""PCHIP-interpolate a trajectory segment for smooth rendering.
|
|
1236
|
+
|
|
1237
|
+
Args:
|
|
1238
|
+
x_raw (array): Raw x-coordinates of the segment.
|
|
1239
|
+
y_raw (array): Raw y-coordinates of the segment.
|
|
1240
|
+
n_subdiv (int): Subdivision factor for interpolation.
|
|
1241
|
+
|
|
1242
|
+
Returns:
|
|
1243
|
+
tuple: (xs, ys) smoothed arrays.
|
|
1244
|
+
"""
|
|
1245
|
+
x_raw = np.asarray(x_raw, dtype=np.float64)
|
|
1246
|
+
y_raw = np.asarray(y_raw, dtype=np.float64)
|
|
1247
|
+
if len(x_raw) < 3:
|
|
1248
|
+
if len(x_raw) == 2:
|
|
1249
|
+
return (np.linspace(x_raw[0], x_raw[1], 20),
|
|
1250
|
+
np.linspace(y_raw[0], y_raw[1], 20))
|
|
1251
|
+
return x_raw, y_raw
|
|
1252
|
+
dx = np.diff(x_raw)
|
|
1253
|
+
dy = np.diff(y_raw)
|
|
1254
|
+
seg = np.sqrt(dx**2 + dy**2)
|
|
1255
|
+
s_orig = np.concatenate([[0.0], np.cumsum(seg)])
|
|
1256
|
+
if s_orig[-1] < 1e-10:
|
|
1257
|
+
return x_raw, y_raw
|
|
1258
|
+
keep = np.concatenate([[True], np.diff(s_orig) > 1e-12])
|
|
1259
|
+
x_dedup = x_raw[keep]
|
|
1260
|
+
y_dedup = y_raw[keep]
|
|
1261
|
+
s_dedup = s_orig[keep]
|
|
1262
|
+
if len(s_dedup) >= 3:
|
|
1263
|
+
n_pts = len(s_dedup) * n_subdiv
|
|
1264
|
+
if n_pts < 20:
|
|
1265
|
+
n_pts = 20
|
|
1266
|
+
s_new = np.linspace(0.0, float(s_dedup[-1]), int(n_pts))
|
|
1267
|
+
if _Pchip is not None:
|
|
1268
|
+
try:
|
|
1269
|
+
xs = _Pchip(s_dedup, x_dedup)(s_new)
|
|
1270
|
+
ys = _Pchip(s_dedup, y_dedup)(s_new)
|
|
1271
|
+
except Exception:
|
|
1272
|
+
xs = np.interp(s_new, s_dedup, x_dedup)
|
|
1273
|
+
ys = np.interp(s_new, s_dedup, y_dedup)
|
|
1274
|
+
else:
|
|
1275
|
+
xs = np.interp(s_new, s_dedup, x_dedup)
|
|
1276
|
+
ys = np.interp(s_new, s_dedup, y_dedup)
|
|
1277
|
+
else:
|
|
1278
|
+
xs = np.linspace(x_dedup[0], x_dedup[-1], 20)
|
|
1279
|
+
ys = np.linspace(y_dedup[0], y_dedup[-1], 20)
|
|
1280
|
+
return xs, ys
|
|
1281
|
+
|
|
1221
1282
|
def _blit_update_trajectory(self):
|
|
1222
|
-
"""Update the trajectory artists for blitting mode."""
|
|
1283
|
+
"""Update the trajectory artists for blitting mode (PCHIP-smoothed)."""
|
|
1223
1284
|
if self.timeStep <= 0:
|
|
1224
1285
|
return
|
|
1225
1286
|
for line in self._blit_artists.get('trajectory', []):
|
|
@@ -1228,16 +1289,81 @@ class RenderMixin:
|
|
|
1228
1289
|
total, lengths = self._count_trajectory_points()
|
|
1229
1290
|
colors = self._generate_fade_colors(total)
|
|
1230
1291
|
idx = 0
|
|
1292
|
+
n_sub = getattr(self, 'SMOOTH_TRAJ_SUBDIV', 10)
|
|
1231
1293
|
for i, length in enumerate(lengths):
|
|
1232
1294
|
color = colors[idx + length - 1]
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
)
|
|
1295
|
+
x_raw = self.trajectories['x'][i]
|
|
1296
|
+
y_raw = self.trajectories['y'][i]
|
|
1297
|
+
xs, ys = self._smooth_segment(x_raw, y_raw, n_sub)
|
|
1298
|
+
line, = self.ax2.plot(xs, ys, lw=2, c=color, zorder=6)
|
|
1238
1299
|
self._blit_artists['trajectory'].append(line)
|
|
1239
1300
|
idx += length
|
|
1240
1301
|
|
|
1302
|
+
def render_particle_transition(self, n_frames=4, pause_sec=0.03):
|
|
1303
|
+
"""Animate the particle from its previous position to the current one.
|
|
1304
|
+
|
|
1305
|
+
Interpolates the particle dot along the straight-line path between
|
|
1306
|
+
the last stored position and the current position, rendering
|
|
1307
|
+
intermediate sub-frames for smooth visual motion.
|
|
1308
|
+
|
|
1309
|
+
Uses blit for fast updates when available; falls back to a single
|
|
1310
|
+
canvas.draw() at the end for non-blit mode (avoids per-frame redraw).
|
|
1311
|
+
|
|
1312
|
+
Args:
|
|
1313
|
+
n_frames (int): Number of sub-frames to interpolate.
|
|
1314
|
+
pause_sec (float): Pause duration per sub-frame in seconds.
|
|
1315
|
+
"""
|
|
1316
|
+
if n_frames <= 1:
|
|
1317
|
+
return
|
|
1318
|
+
try:
|
|
1319
|
+
x_now, y_now = self._get_particle_coords()
|
|
1320
|
+
except (IndexError, TypeError):
|
|
1321
|
+
return
|
|
1322
|
+
prev = getattr(self, '_prevParticleCoords', None)
|
|
1323
|
+
if prev is None:
|
|
1324
|
+
self._prevParticleCoords = (x_now, y_now)
|
|
1325
|
+
return
|
|
1326
|
+
x_prev, y_prev = prev
|
|
1327
|
+
# Skip if no movement
|
|
1328
|
+
if abs(x_now - x_prev) < 1e-8 and abs(y_now - y_prev) < 1e-8:
|
|
1329
|
+
self._prevParticleCoords = (x_now, y_now)
|
|
1330
|
+
return
|
|
1331
|
+
|
|
1332
|
+
use_blit = (self._blit_enabled and
|
|
1333
|
+
self._blit_artists.get('particle') is not None)
|
|
1334
|
+
|
|
1335
|
+
if use_blit:
|
|
1336
|
+
# Fast path: blit-based sub-frame animation
|
|
1337
|
+
particle = self._blit_artists['particle']
|
|
1338
|
+
dx = x_now - x_prev
|
|
1339
|
+
dy = y_now - y_prev
|
|
1340
|
+
for i in range(1, n_frames + 1):
|
|
1341
|
+
t = i / n_frames
|
|
1342
|
+
x_i = x_prev + t * dx
|
|
1343
|
+
y_i = y_prev + t * dy
|
|
1344
|
+
particle.set_offsets(
|
|
1345
|
+
self.ax2.transData.transform([(x_i, y_i)])
|
|
1346
|
+
)
|
|
1347
|
+
self.fig.canvas.blit(self.fig.bbox)
|
|
1348
|
+
if pause_sec > 0:
|
|
1349
|
+
sleep(pause_sec)
|
|
1350
|
+
else:
|
|
1351
|
+
# Slow path: single draw at final position (avoids artist leak)
|
|
1352
|
+
# Remove any stale transition artists
|
|
1353
|
+
for art in self._blit_artists.get('_transition_artists', []):
|
|
1354
|
+
art.remove()
|
|
1355
|
+
self._blit_artists['_transition_artists'] = []
|
|
1356
|
+
# Draw a single line showing the transition path
|
|
1357
|
+
art, = self.ax2.plot([x_prev, x_now], [y_prev, y_now],
|
|
1358
|
+
'r-', lw=1, alpha=0.5, zorder=5)
|
|
1359
|
+
dot, = self.ax2.plot([x_now], [y_now], 'ro', markersize=6,
|
|
1360
|
+
zorder=6, alpha=0.9)
|
|
1361
|
+
self._blit_artists['_transition_artists'] = [art, dot]
|
|
1362
|
+
self.fig.canvas.draw()
|
|
1363
|
+
|
|
1364
|
+
# Final position
|
|
1365
|
+
self._prevParticleCoords = (x_now, y_now)
|
|
1366
|
+
|
|
1241
1367
|
def _blit_update_score_text(self):
|
|
1242
1368
|
"""Update the score/time text for blitting mode."""
|
|
1243
1369
|
score_text = self.format_score()
|
|
@@ -137,15 +137,21 @@ class Render3D:
|
|
|
137
137
|
n = len(traj)
|
|
138
138
|
# Cubic spline subdivision for smooth curved rendering
|
|
139
139
|
if n >= 3:
|
|
140
|
-
from scipy.interpolate import PchipInterpolator
|
|
141
140
|
d = np.diff(traj, axis=0)
|
|
142
141
|
seg = np.sqrt((d**2).sum(axis=1))
|
|
143
142
|
s_orig = np.concatenate([[0.0], np.cumsum(seg)])
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
143
|
+
# Deduplicate: PCHIP requires strictly increasing x
|
|
144
|
+
s_unique, idx_unique = np.unique(s_orig, return_index=True)
|
|
145
|
+
traj_u = traj[idx_unique]
|
|
146
|
+
if len(s_unique) >= 3:
|
|
147
|
+
from scipy.interpolate import PchipInterpolator
|
|
148
|
+
s_new = np.linspace(0.0, s_unique[-1], len(s_unique) * 10)
|
|
149
|
+
traj = np.column_stack([
|
|
150
|
+
PchipInterpolator(s_unique, traj_u[:, c])(s_new)
|
|
151
|
+
for c in range(3)
|
|
152
|
+
])
|
|
153
|
+
else:
|
|
154
|
+
traj = np.linspace(traj_u[0], traj_u[-1], 20)
|
|
149
155
|
n = len(traj)
|
|
150
156
|
elif n == 2:
|
|
151
157
|
traj = np.linspace(traj[0], traj[1], 20)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Conftest to handle ray 2.56.1 missing ray.rllib.agents.dqn submodule."""
|
|
2
|
+
import sys
|
|
3
|
+
from unittest.mock import MagicMock
|
|
4
|
+
|
|
5
|
+
# ray >= 2.43 removed ray.rllib.agents from public API
|
|
6
|
+
# play_againstai.py does: from ray.rllib.agents.dqn import ApexTrainer
|
|
7
|
+
try:
|
|
8
|
+
import ray.rllib
|
|
9
|
+
if not hasattr(ray.rllib, 'agents'):
|
|
10
|
+
_agents_mod = MagicMock()
|
|
11
|
+
_agents_mod.dqn = MagicMock()
|
|
12
|
+
_agents_mod.dqn.ApexTrainer = type("ApexTrainer", (), {})
|
|
13
|
+
ray.rllib.agents = _agents_mod
|
|
14
|
+
sys.modules['ray.rllib.agents'] = _agents_mod
|
|
15
|
+
sys.modules['ray.rllib.agents.dqn'] = _agents_mod.dqn
|
|
16
|
+
except ImportError:
|
|
17
|
+
# ray not installed (CI without [ray] extra) — test_mock_rllib.py
|
|
18
|
+
# injects its own mocks, so no patching needed here.
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
# Set sys.argv before play_againstai module-level parse_args()
|
|
22
|
+
sys.argv = ["play_againstai.py", "--envtype", "1s-d", "--cpus", "4", "--gpus", "0",
|
|
23
|
+
"--playbenchmark", "False", "--external", "True", "--suffix", "_test"]
|