476 Commits

Author SHA1 Message Date
Dominik Jain
eeb2081ca6 Fix AutoAlphaFactoryDefault using hard-coded Adam optimizer instead of passed factory 2024-02-14 20:43:38 +01:00
Dominik Jain
76cbd7efc2 Make OptimizerFactory more flexible by adding a second method which
allows the creation of an optimizer given arbitrary parameters
(rather than a module)
2024-02-14 20:42:06 +01:00
Dominik Jain
bf391853dc Allow to configure number of test episodes in high-level API 2024-02-14 19:14:28 +01:00
Carlo Cagnetta
5fc314bd4b
Docs/use nbqa on notebooks (#1041)
- Added nbqa to pyproject.toml
- Resolved mypy issues on notebooks and related files
- Conducting ruff checks on notebooks
- Add DataclassPPrintMixin for better stats representation
- Improved Notebooks wording and explanations

Resolve: #1004
Related to #974
2024-02-07 17:28:16 +01:00
Daniel Plop
eb0215cf76
Refactoring/mypy issues test (#1017)
Improves typing in examples and tests, towards mypy passing there.

Introduces the SpaceInfo utility
2024-02-06 14:24:30 +01:00
Michael Panchenko
6e1ffe58e5
Improvements in README and high-level API (#1022)
This makes several largely unrelated improvements in the high-level API
and in the README.

Main improvements in high-level API:
* Improve naming in trainer-related abstractions, moved some classes
from examples to the library
  * Improve environment factory abstraction
  * Some bug-fixes

Main changes in README:
  * Add high-level example and update procedural/low-level example
  * Improve language/wording
2024-01-16 15:24:41 +01:00
Dominik Jain
022cfb7f78 Cleaned up handling of output_dim retrieval, adding exceptions for erroneous cases 2024-01-16 14:52:31 +01:00
Dominik Jain
20074931d5 Improve docstrings 2024-01-16 14:52:31 +01:00
Dominik Jain
05a8cf4e74 Refactoring, improving class name EnvFactoryGymnasium -> EnvFactoryRegistered 2024-01-16 14:52:31 +01:00
Dominik Jain
c9cb41bf55 Make envpool usage configuration more explicit 2024-01-16 14:52:31 +01:00
Dominik Jain
1e5ebc2a2d Improve naming of callback classes and related methods/attributes
Add EpochStopCallbackRewardThreshold
2024-01-12 17:13:42 +01:00
Dominik Jain
24b7b82e56 Remove inappropriate warning (warns about supported case according to docstring) 2024-01-12 17:13:42 +01:00
Dominik Jain
ff398beed9 Move callbacks for setting DQN epsilon values to the library 2024-01-12 17:13:42 +01:00
Dominik Jain
eaab7b0a4b Improve environment factory abstractions in high-level API:
* EnvFactory now uses the creation of a single environment as
   the basic functionality which the more high-level functions build
   upon
 * Introduce enum EnvMode to indicate the purpose for which an env
   is created, allowing the factory creation process to change its
   behaviour accordingly
 * Add EnvFactoryGymnasium to provide direct support for envs that
   can be created via gymnasium.make
     - EnvPool is supported via an injectible EnvPoolFactory
     - Existing EnvFactory implementations are now derived from
       EnvFactoryGymnasium
 * Use a separate environment (which uses new EnvMode.WATCH) for
   watching agent performance after training (instead of using test
   environments, which the user may want to configure differently)
2024-01-12 17:13:42 +01:00
Dominik Jain
d4e4f4ff63 Experiment builders for DQN and IQN:
* Fix: Disable softmax in default models
  * Add method with_model_factory_default (for DQN)
2024-01-10 15:42:18 +01:00
Michael Panchenko
789340f8d6
Minor simplification in train_step (#1019) 2024-01-09 08:51:49 -08:00
Dominik Jain
f77d95da04 Fix: Missing type annotation of Experiment.watch_num_episodes 2024-01-08 18:00:37 +01:00
Dominik Jain
97a241a6fc Fix: DiscreteEnvironments.from_factory used incorrect EnvType 2024-01-08 15:58:41 +01:00
maxhuettenrauch
522f7fbf98
Feature/dataclasses (#996)
This PR adds strict typing to the output of `update` and `learn` in all
policies. This will likely be the last large refactoring PR before the
next release (0.6.0, not 1.0.0), so it requires some attention. Several
difficulties were encountered on the path to that goal:

1. The policy hierarchy is actually "broken" in the sense that the keys
of dicts that were output by `learn` did not follow the same enhancement
(inheritance) pattern as the policies. This is a real problem and should
be addressed in the near future. Generally, several aspects of the
policy design and hierarchy might deserve a dedicated discussion.
2. Each policy needs to be generic in the stats return type, because one
might want to extend it at some point and then also extend the stats.
Even within the source code base this pattern is necessary in many
places.
3. The interaction between learn and update is a bit quirky, we
currently handle it by having update modify special field inside
TrainingStats, whereas all other fields are handled by learn.
4. The IQM module is a policy wrapper and required a
TrainingStatsWrapper. The latter relies on a bunch of black magic.

They were addressed by:
1. Live with the broken hierarchy, which is now made visible by bounds
in generics. We use type: ignore where appropriate.
2. Make all policies generic with bounds following the policy
inheritance hierarchy (which is incorrect, see above). We experimented a
bit with nested TrainingStats classes, but that seemed to add more
complexity and be harder to understand. Unfortunately, mypy thinks that
the code below is wrong, wherefore we have to add `type: ignore` to the
return of each `learn`

```python

T = TypeVar("T", bound=int)


def f() -> T:
  return 3
```

3. See above
4. Write representative tests for the `TrainingStatsWrapper`. Still, the
black magic might cause nasty surprises down the line (I am not proud of
it)...

Closes #933

---------

Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de>
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
Dominik Jain
e8cc80f990 Environments: Add option to a use a different factory for test envs
to `from_factory` convenience construction mechanisms
2023-12-21 13:13:51 +01:00
Dominik Jain
45a1a3f259 SamplingConfig: Change default of repeat_per_collect to 1 (safest option) 2023-12-21 13:13:51 +01:00
Dominik Jain
408d51f9de SamplingConfig: Improve/extend docstrings, clearly explaining the parameters 2023-12-21 13:13:51 +01:00
Dominik Jain
1903a72ecb Improve logging 2023-12-14 19:31:30 +01:00
Dominik Jain
3caa3805f0 Fix: SamplingConfig.start_timesteps_random was not used 2023-12-14 11:47:32 +01:00
Michael Panchenko
0b67447541 Docs: fixing spelling, re-adding spellcheck to pipeline 2023-12-05 13:22:04 +01:00
Michael Panchenko
a846b52063 Typing: fixed multiple typing issues 2023-12-05 12:04:18 +01:00
Michael Panchenko
2e39a252e3 Docstring: minor changes to let ruff pass 2023-12-04 13:52:46 +01:00
Michael Panchenko
4cfefcf75d Docs: removed conflicting sphinx stuff from a docstring 2023-12-04 11:48:09 +01:00
Michael Panchenko
a5685619ce Docs: generate all api docs automatically
Reinstate the -W option
Several overall improvements in docs
Fixed multiple links
2023-12-04 11:48:09 +01:00
Michael Panchenko
8d3d1f164b
Support batch_size=None and use it in various scripts (#993)
Closes #986
2023-11-24 10:13:10 -08:00
Michael Panchenko
f134bc20b5
Bugfix/discrete bcq inf (#995)
Fixes a small bug with using np.inf instead of torch-based infinity

Closes #963

---------

Co-authored-by: ivan.rodriguez <ivan.rodriguez@unternehmertum.de>
2023-11-24 11:17:40 +01:00
Michael Panchenko
3a1bc18add
Method to compute actions from observations (#991)
This PR adds a new method for getting actions from an env's observation
and info. This is useful for standard inference and stands in contrast
to batch-based methods that are currently used in training and
evaluation. Without this, users have to do some kind of gymnastics to
actually perform inference with a trained policy. I have also added a
test for the new method.

In future PRs, this method should be included in the examples (in the
the "watch" section).

To add this required improving multiple typing things and, importantly,
_simplifying the signature of `forward` in many policies!_ This is a
**breaking change**, but it will likely affect no users. The `input`
parameter of forward was a rather hacky mechanism, I believe it is good
that it's gone now. It will also help with #948 .

The main functional change is the addition of `compute_action` to
`BasePolicy`.

Other minor changes:
- improvements in typing
- updated PR and Issue templates
- Improved handling of `max_action_num`

Closes #981
2023-11-16 17:27:53 +00:00
Dominik Jain
6d6c85e594
Fix an issue where policies built with LRSchedulerFactoryLinear were not picklable (#992)
- [X] I have marked all applicable categories:
    + [X] exception-raising fix
    + [ ] algorithm implementation fix
    + [ ] documentation modification
    + [ ] new feature
- [X] I have reformatted the code using `make format` (**required**)
- [X] I have checked the code using `make commit-checks` (**required**)
- [ ] If applicable, I have mentioned the relevant/related issue(s)
- [ ] If applicable, I have listed every items in this Pull Request
below

The cause was the use of a lambda function in the state of a generated
object.
2023-11-14 10:23:18 -08:00
Dominik Jain
dae4000cd2 Revert "Depend on sensAI instead of copying its utils (logging, string)"
This reverts commit fdb0eba93d81fa5e698770b4f7088c87fc1238da.
2023-11-08 19:11:39 +01:00
Dominik Jain
ac672f65d1 Add docstring for ActorFactoryTransientStorageDecorator 2023-11-06 17:18:10 +01:00
Dominik Jain
7e6d3d627e Rename class ActorCriticModuleOpt -> ActorCriticOpt 2023-11-06 16:51:41 +01:00
Dominik Jain
5c8d57a2d2
Fix index error in call to _with_critic_factory_default
Co-authored-by: Jiayi Weng <trinkle23897@gmail.com>
2023-11-06 16:17:14 +01:00
Dominik Jain
fdb0eba93d Depend on sensAI instead of copying its utils (logging, string) 2023-10-27 20:15:58 +02:00
Dominik Jain
5952993cfe Add option to disable file logging 2023-10-27 18:59:43 +02:00
Dominik Jain
a3dbe90515 Allow to configure the policy persistence mode, adding a new mode
which stores the entire policy (new default), supporting applications
where it is desired to be bale to load the policy without having
to instantiate an environment or recreate a corresponding policy
object
2023-10-26 13:19:33 +02:00
Dominik Jain
86cca8ffc3 Add comment explaining use of _logFormat 2023-10-26 12:50:08 +02:00
Dominik Jain
d684dae6cd Change default number of environments (train=#CPUs, test=1) 2023-10-26 12:50:08 +02:00
Dominik Jain
3cd6dcc307 BaseTrainer: Remove info on default values from docstrings 2023-10-26 10:55:03 +02:00
Dominik Jain
da2194eff6 Force kwargs in PolicyWrapperFactoryIntrinsicCuriosity init 2023-10-26 10:43:59 +02:00
Dominik Jain
96298eafd8 Add convenient construction mechanisms for Environments
(based on factory function for a single environment)
2023-10-25 21:20:07 +02:00
Dominik Jain
b5a891557f Revert to simplified environment factory, removing unnecessary config object
(configuration shall be part of the factory instance)
2023-10-24 13:14:23 +02:00
Dominik Jain
f7f20649e3 ExperimentConfig: Improve docstrings, remove obsolete item 'render' 2023-10-20 17:34:27 +02:00
Dominik Jain
7437131d79 Fix tianshou.highlevel depending on jsonargparse
(should be dev dependency only) by introducing a new
place where jsonargparse can be configured:
logging.run_cli, which is also slightly more convenient
2023-10-19 11:40:49 +02:00
Dominik Jain
6cbee188b8 Change interface of EnvFactory to ensure that configuration
of number of environments in SamplingConfig is used
(values are now passed to factory method)

This is clearer and removes the need to pass otherwise
unnecessary configuration to environment factories at
construction
2023-10-19 11:37:20 +02:00
Dominik Jain
bbfad01a9f Improve docstrings 2023-10-18 22:07:40 +02:00