-
Notifications
You must be signed in to change notification settings - Fork 6
/
solver.py
85 lines (70 loc) · 3.05 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Author: Toshinori Kitamura
Affiliation: NAIST & OSX
"""
from typing import List, Type
import gym
import shinrl as srl
from ._build_calc_params_mixin import BuildCalcParamsDpMixIn, BuildCalcParamsRlMixIn
from ._build_net_act_mixin import BuildNetActMixIn
from ._build_net_mixin import BuildNetMixIn
from ._build_table_mixin import BuildTableMixIn
from ._step_mixin import (
DeepDpStepMixIn,
DeepRlStepMixIn,
TabularDpStepMixIn,
TabularRlStepMixIn,
)
from ._target_mixin import DoubleQTargetMixIn, MunchausenTargetMixIn, QTargetMixIn
from .config import ViConfig
class DiscreteViSolver(srl.BaseSolver):
"""Value iteration (VI) solver.
This solver implements some basic VI-based algorithms.
For example, DiscreteViSolver turns into DQN when approx == "nn" and explore != "oracle".
"""
DefaultConfig = ViConfig
@staticmethod
def make_mixins(env: gym.Env, config: ViConfig) -> List[Type[object]]:
approx, explore = config.approx, config.explore
APPROX, EXPLORE = config.APPROX, config.EXPLORE
if isinstance(env, gym.Wrapper):
is_shin_env = isinstance(env.unwrapped, srl.ShinEnv)
else:
is_shin_env = isinstance(env, srl.ShinEnv)
mixin_list: List[Type[object]] = [DiscreteViSolver]
# Add base mixins for evaluation and exploration.
if is_shin_env:
mixin_list += [srl.BaseShinEvalMixIn, srl.BaseShinExploreMixIn]
else:
mixin_list += [srl.BaseGymEvalMixIn, srl.BaseGymExploreMixIn]
# Add mixins to prepare networks.
if approx == APPROX.nn:
mixin_list += [BuildNetMixIn, BuildNetActMixIn]
# Add mixins to prepare tables.
if is_shin_env:
mixin_list += [BuildTableMixIn]
# Add algorithm mixins to compute Q-targets
is_q_learning = (config.er_coef == 0.0) * (config.kl_coef == 0.0)
use_double_q = config.use_double_q
if is_q_learning and not use_double_q: # Vanilla Q target
mixin_list += [QTargetMixIn]
elif is_q_learning and use_double_q: # Double Q target
mixin_list += [DoubleQTargetMixIn]
elif not is_q_learning and not use_double_q: # Munchausen Q target
mixin_list += [MunchausenTargetMixIn]
else:
raise NotImplementedError
# Branch to tabular DP, deep DP, tabular RL, or deep RL
if approx == APPROX.tabular and explore == EXPLORE.oracle:
mixin_list += [TabularDpStepMixIn]
elif approx == APPROX.nn and explore == EXPLORE.oracle:
mixin_list += [DeepDpStepMixIn, BuildCalcParamsDpMixIn]
elif config.approx == APPROX.tabular and explore != EXPLORE.oracle:
mixin_list += [TabularRlStepMixIn]
elif config.approx == APPROX.nn and config.explore != EXPLORE.oracle:
mixin_list += [DeepRlStepMixIn, BuildCalcParamsRlMixIn]
else:
raise NotImplementedError
# Reverse the order. The latter classes have the higher priorities.
mixin_list = mixin_list[::-1]
return mixin_list