kozax 0.0.1__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.
- kozax-0.0.1/.gitignore +1 -0
- kozax-0.0.1/Kozax/environments/SR_environments/lorenz_attractor.py +82 -0
- kozax-0.0.1/Kozax/environments/SR_environments/lotka_volterra.py +103 -0
- kozax-0.0.1/Kozax/environments/SR_environments/time_series_environment_base.py +67 -0
- kozax-0.0.1/Kozax/environments/SR_environments/vd_pol_oscillator.py +48 -0
- kozax-0.0.1/Kozax/environments/control_environments/acrobot.py +231 -0
- kozax-0.0.1/Kozax/environments/control_environments/cart_pole.py +99 -0
- kozax-0.0.1/Kozax/environments/control_environments/control_environment_base.py +85 -0
- kozax-0.0.1/Kozax/environments/control_environments/harmonic_oscillator.py +243 -0
- kozax-0.0.1/Kozax/environments/control_environments/reactor.py +155 -0
- kozax-0.0.1/Kozax/evaluators/SR_evaluator.py +122 -0
- kozax-0.0.1/Kozax/evaluators/dynamic_evaluate.py +271 -0
- kozax-0.0.1/Kozax/evaluators/static_evaluate.py +277 -0
- kozax-0.0.1/Kozax/genetic_operators/crossover.py +255 -0
- kozax-0.0.1/Kozax/genetic_operators/initialization.py +166 -0
- kozax-0.0.1/Kozax/genetic_operators/mutation.py +600 -0
- kozax-0.0.1/Kozax/genetic_operators/reproduction.py +203 -0
- kozax-0.0.1/Kozax/genetic_programming.py +812 -0
- kozax-0.0.1/LICENSE.md +16 -0
- kozax-0.0.1/PKG-INFO +105 -0
- kozax-0.0.1/README.md +71 -0
- kozax-0.0.1/pyproject.toml +30 -0
kozax-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.pyc
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
from Kozax.environments.SR_environments.time_series_environment_base import EnvironmentBase
|
|
24
|
+
|
|
25
|
+
class LorenzAttractor(EnvironmentBase):
|
|
26
|
+
def __init__(self, process_noise, obs_noise, n_obs=3):
|
|
27
|
+
n_var = 3
|
|
28
|
+
super().__init__(process_noise, obs_noise, n_var, n_obs)
|
|
29
|
+
|
|
30
|
+
self.init_mu = jnp.array([1,1,1])
|
|
31
|
+
self.init_sd = 1
|
|
32
|
+
|
|
33
|
+
self.sigma = 10
|
|
34
|
+
self.rho = 28
|
|
35
|
+
self.beta = 8/3
|
|
36
|
+
self.V = self.process_noise * jnp.eye(self.n_var)
|
|
37
|
+
self.W = self.obs_noise * jnp.eye(self.n_obs)[:self.n_obs]
|
|
38
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
39
|
+
|
|
40
|
+
def sample_init_states(self, batch_size, key):
|
|
41
|
+
return self.init_mu + self.init_sd*jrandom.normal(key, shape=(batch_size,3))
|
|
42
|
+
# return jnp.ones((batch_size, 3))
|
|
43
|
+
|
|
44
|
+
def drift(self, t, state, args):
|
|
45
|
+
return jnp.array([self.sigma*(state[1]-state[0]), state[0]*(self.rho-state[2])-state[1], state[0]*state[1]-self.beta*state[2]])
|
|
46
|
+
|
|
47
|
+
def diffusion(self, t, state, args):
|
|
48
|
+
return self.V
|
|
49
|
+
|
|
50
|
+
def terminate_event(self, state, **kwargs):
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
class Lorenz96(EnvironmentBase):
|
|
54
|
+
def __init__(self, process_noise, obs_noise, n_dim = 3, n_obs=3):
|
|
55
|
+
n_var = n_dim
|
|
56
|
+
super().__init__(process_noise, obs_noise, n_var, n_obs)
|
|
57
|
+
|
|
58
|
+
self.F = 8
|
|
59
|
+
self.init_mu = jnp.ones(self.n_var)*self.F*0
|
|
60
|
+
self.init_sd = 1
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
self.V = self.process_noise * jnp.eye(self.n_var)
|
|
64
|
+
self.W = self.obs_noise * jnp.eye(self.n_obs)[:self.n_obs]
|
|
65
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
66
|
+
|
|
67
|
+
def sample_init_states(self, batch_size, key):
|
|
68
|
+
return self.init_mu + self.init_sd*jrandom.normal(key, shape=(batch_size,self.n_var))
|
|
69
|
+
# return jnp.ones((batch_size, 3))
|
|
70
|
+
|
|
71
|
+
def sample_init_state2(self, ys, batch_size, key):
|
|
72
|
+
return ys[jrandom.choice(key, jnp.arange(ys.shape[0]), shape=(batch_size,), replace=False)]
|
|
73
|
+
|
|
74
|
+
def drift(self, t, state, args):
|
|
75
|
+
f = lambda x_cur, x_next, x_prev1, x_prev2: (x_next - x_prev2) * x_prev1 - x_cur + self.F
|
|
76
|
+
return jax.vmap(f)(state, jnp.roll(state, -1), jnp.roll(state, 1), jnp.roll(state, 2))
|
|
77
|
+
|
|
78
|
+
def diffusion(self, t, state, args):
|
|
79
|
+
return self.V
|
|
80
|
+
|
|
81
|
+
def terminate_event(self, state, **kwargs):
|
|
82
|
+
return False
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
from Kozax.environments.SR_environments.time_series_environment_base import EnvironmentBase
|
|
24
|
+
|
|
25
|
+
class LotkaVolterra(EnvironmentBase):
|
|
26
|
+
def __init__(self, process_noise, obs_noise, n_obs=2):
|
|
27
|
+
n_var = 2
|
|
28
|
+
super().__init__(process_noise, obs_noise, n_var, n_obs)
|
|
29
|
+
|
|
30
|
+
self.init_mu = jnp.array([10, 10])
|
|
31
|
+
self.init_sd = 2
|
|
32
|
+
|
|
33
|
+
self.alpha = 1.1
|
|
34
|
+
self.beta = 0.4
|
|
35
|
+
self.delta = 0.1
|
|
36
|
+
self.gamma = 0.4
|
|
37
|
+
self.V = self.process_noise * jnp.eye(self.n_var)
|
|
38
|
+
self.W = self.obs_noise * jnp.eye(self.n_obs)[:self.n_obs]
|
|
39
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
40
|
+
|
|
41
|
+
def sample_init_states(self, batch_size, key):
|
|
42
|
+
return jrandom.uniform(key, shape = (batch_size,2), minval=5, maxval=15)
|
|
43
|
+
|
|
44
|
+
def sample_init_state2(self, ys, batch_size, key):
|
|
45
|
+
return ys[jrandom.choice(key, jnp.arange(ys.shape[0]), shape=(batch_size,), replace=False)]
|
|
46
|
+
|
|
47
|
+
def drift(self, t, state, args):
|
|
48
|
+
return jnp.array([self.alpha * state[0] - self.beta * state[0] * state[1], self.delta * state[0] * state[1] - self.gamma * state[1]])
|
|
49
|
+
|
|
50
|
+
def diffusion(self, t, state, args):
|
|
51
|
+
return self.V
|
|
52
|
+
|
|
53
|
+
def terminate_event(self, state, **kwargs):
|
|
54
|
+
return False
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class LotkaVolterraN(EnvironmentBase):
|
|
58
|
+
def __init__(self, key, process_noise, obs_noise, n_dim=1, n_obs=2):
|
|
59
|
+
n_var = 2*n_dim
|
|
60
|
+
n_obs = n_dim * n_obs
|
|
61
|
+
super().__init__(process_noise, obs_noise, n_var, n_obs)
|
|
62
|
+
|
|
63
|
+
self.init_mu = 10 * jnp.ones(n_var)
|
|
64
|
+
self.init_sd = 2
|
|
65
|
+
|
|
66
|
+
keys = jrandom.split(key, (2+n_var,))
|
|
67
|
+
|
|
68
|
+
# self.rate = jrandom.uniform(keys[0], shape=n_var, minval=0.2, maxval=0.8)*jnp.repeat(jnp.array([[1.0,-1.0]]), n_dim,axis=0).ravel()
|
|
69
|
+
prey_rates = jrandom.uniform(keys[0], shape=n_dim, minval=0.5, maxval=1.0)
|
|
70
|
+
predator_rates = jrandom.uniform(keys[1], shape=n_dim, minval=-0.5, maxval=-0.2)
|
|
71
|
+
self.rate = jnp.array([[prey_rates[i], predator_rates[i]] for i in range(n_dim)]).ravel()
|
|
72
|
+
interaction = jnp.zeros((n_var, n_var))
|
|
73
|
+
for i in range(n_var):
|
|
74
|
+
a1_key, a2_key = jrandom.split(keys[2+i])
|
|
75
|
+
a1 = jrandom.uniform(a1_key, minval=0.01, maxval=0.1) * (-1 + 2*((i%2) > 0))
|
|
76
|
+
interaction = interaction.at[i, (i-1)%n_var].set(a1)
|
|
77
|
+
a2 = jrandom.uniform(a2_key, minval=0.0, maxval=0.1-a1) * (-1 + 2*((i%2) > 0))
|
|
78
|
+
interaction = interaction.at[i, (i+1)%n_var].set(a2)
|
|
79
|
+
|
|
80
|
+
self.interaction = interaction
|
|
81
|
+
|
|
82
|
+
print(self.rate)
|
|
83
|
+
print(self.interaction)
|
|
84
|
+
|
|
85
|
+
self.V = self.process_noise * jnp.eye(self.n_var)
|
|
86
|
+
self.W = self.obs_noise * jnp.eye(self.n_obs)[:self.n_obs]
|
|
87
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
88
|
+
|
|
89
|
+
def sample_init_states(self, batch_size, key):
|
|
90
|
+
return jrandom.uniform(key, shape = (batch_size,self.n_var), minval=5, maxval=15)
|
|
91
|
+
|
|
92
|
+
def sample_init_state2(self, ys, batch_size, key):
|
|
93
|
+
return ys[jrandom.choice(key, jnp.arange(ys.shape[0]), shape=(batch_size,), replace=False)]
|
|
94
|
+
|
|
95
|
+
def drift(self, t, state, args):
|
|
96
|
+
# return jnp.array([self.alpha * state[0] - self.beta * state[0] * state[1], self.delta * state[0] * state[1] - self.gamma * state[1]])
|
|
97
|
+
return state * (self.rate + self.interaction@state)
|
|
98
|
+
|
|
99
|
+
def diffusion(self, t, state, args):
|
|
100
|
+
return self.V
|
|
101
|
+
|
|
102
|
+
def terminate_event(self, state, **kwargs):
|
|
103
|
+
return False
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
import abc
|
|
24
|
+
|
|
25
|
+
_itemsize_kind_type = {
|
|
26
|
+
(1, "i"): jnp.int8,
|
|
27
|
+
(2, "i"): jnp.int16,
|
|
28
|
+
(4, "i"): jnp.int32,
|
|
29
|
+
(8, "i"): jnp.int64,
|
|
30
|
+
(2, "f"): jnp.float16,
|
|
31
|
+
(4, "f"): jnp.float32,
|
|
32
|
+
(8, "f"): jnp.float64,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
def force_bitcast_convert_type(val, new_type=jnp.int32):
|
|
36
|
+
val = jnp.asarray(val)
|
|
37
|
+
intermediate_type = _itemsize_kind_type[new_type.dtype.itemsize, val.dtype.kind]
|
|
38
|
+
val = val.astype(intermediate_type)
|
|
39
|
+
return jax.lax.bitcast_convert_type(val, new_type)
|
|
40
|
+
|
|
41
|
+
class EnvironmentBase(abc.ABC):
|
|
42
|
+
def __init__(self, process_noise, obs_noise, n_var, n_obs):
|
|
43
|
+
self.process_noise = process_noise
|
|
44
|
+
self.obs_noise = obs_noise
|
|
45
|
+
self.n_var = n_var
|
|
46
|
+
self.n_obs = n_obs
|
|
47
|
+
|
|
48
|
+
@abc.abstractmethod
|
|
49
|
+
def sample_init_states(self, batch_size, key):
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
def f_obs(self, key, t_x):
|
|
53
|
+
t, x = t_x
|
|
54
|
+
new_key = jrandom.fold_in(key, force_bitcast_convert_type(t))
|
|
55
|
+
out = self.C@x + jrandom.normal(new_key, shape=(self.n_obs,))@self.W
|
|
56
|
+
return key, out
|
|
57
|
+
|
|
58
|
+
@abc.abstractmethod
|
|
59
|
+
def drift(self, t, state, args):
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
@abc.abstractmethod
|
|
63
|
+
def diffusion(self, t, state, args):
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
def terminate_event(self, state, **kwargs):
|
|
67
|
+
return False
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
from Kozax.environments.SR_environments.time_series_environment_base import EnvironmentBase
|
|
24
|
+
|
|
25
|
+
class VanDerPolOscillator(EnvironmentBase):
|
|
26
|
+
def __init__(self, process_noise, obs_noise, n_obs=2):
|
|
27
|
+
n_var = 2
|
|
28
|
+
super().__init__(process_noise, obs_noise, n_var, n_obs)
|
|
29
|
+
|
|
30
|
+
self.init_mu = jnp.array([0,0])
|
|
31
|
+
self.init_sd = jnp.array([1.0,1.0])
|
|
32
|
+
|
|
33
|
+
self.mu = 1
|
|
34
|
+
self.V = self.process_noise * jnp.eye(self.n_var)
|
|
35
|
+
self.W = self.obs_noise * jnp.eye(self.n_obs)[:self.n_obs]
|
|
36
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
37
|
+
|
|
38
|
+
def sample_init_states(self, batch_size, key):
|
|
39
|
+
return self.init_mu + self.init_sd*jrandom.normal(key, shape=(batch_size,2))
|
|
40
|
+
|
|
41
|
+
def drift(self, t, state, args):
|
|
42
|
+
return jnp.array([state[1], self.mu*(1-state[0]**2)*state[1] - state[0]])
|
|
43
|
+
|
|
44
|
+
def diffusion(self, t, state, args):
|
|
45
|
+
return self.V
|
|
46
|
+
|
|
47
|
+
def terminate_event(self, state, **kwargs):
|
|
48
|
+
return False
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
|
|
24
|
+
from Kozax.environments.control_environments.control_environment_base import EnvironmentBase
|
|
25
|
+
|
|
26
|
+
class Acrobot(EnvironmentBase):
|
|
27
|
+
def __init__(self, process_noise, obs_noise, n_obs=4):
|
|
28
|
+
self.n_var = 4
|
|
29
|
+
self.n_control = 1
|
|
30
|
+
self.n_targets = 0
|
|
31
|
+
self.n_dim = 1
|
|
32
|
+
self.init_bounds = jnp.array([0.1,0.1,0.1,0.1])
|
|
33
|
+
super().__init__(process_noise, obs_noise, self.n_var, self.n_control, self.n_dim, n_obs)
|
|
34
|
+
|
|
35
|
+
self.R = jnp.array([[0.01]])
|
|
36
|
+
|
|
37
|
+
def sample_init_states(self, batch_size, key):
|
|
38
|
+
init_key, target_key = jrandom.split(key)
|
|
39
|
+
x0 = jrandom.uniform(init_key, shape=(batch_size, self.n_var), minval= -self.init_bounds, maxval= self.init_bounds)
|
|
40
|
+
targets = jnp.zeros((batch_size, self.n_targets))
|
|
41
|
+
return x0, targets
|
|
42
|
+
|
|
43
|
+
def sample_params(self, batch_size, mode, ts, key):
|
|
44
|
+
l1 = l2 = m1 = m2 = jnp.ones((batch_size))
|
|
45
|
+
|
|
46
|
+
return l1, l2, m1, m2
|
|
47
|
+
|
|
48
|
+
def f_obs(self, key, t_x):
|
|
49
|
+
_, out = super().f_obs(key, t_x)
|
|
50
|
+
out = jnp.array([(out[0]+jnp.pi)%(2*jnp.pi)-jnp.pi, (out[1]+jnp.pi)%(2*jnp.pi)-jnp.pi, out[2], out[3]])[:self.n_obs]
|
|
51
|
+
return key, out
|
|
52
|
+
|
|
53
|
+
def initialize_parameters(self, params, ts):
|
|
54
|
+
l1, l2, m1, m2 = params
|
|
55
|
+
self.l1 = l1 # [m]
|
|
56
|
+
self.l2 = l2 # [m]
|
|
57
|
+
self.m1 = m1 #: [kg] mass of link 1
|
|
58
|
+
self.m2 = m2 #: [kg] mass of link 2
|
|
59
|
+
self.lc1 = 0.5*self.l1 #: [m] position of the center of mass of link 1
|
|
60
|
+
self.lc2 = 0.5*self.l2 #: [m] position of the center of mass of link 2
|
|
61
|
+
self.moi1 = self.moi2 = 1.0
|
|
62
|
+
self.g = 9.81
|
|
63
|
+
|
|
64
|
+
self.G = jnp.array([[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,1]])
|
|
65
|
+
self.V = self.process_noise*self.G
|
|
66
|
+
|
|
67
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
68
|
+
self.W = self.obs_noise*jnp.eye(self.n_obs)
|
|
69
|
+
|
|
70
|
+
def drift(self, t, state, args):
|
|
71
|
+
control = jnp.squeeze(args)
|
|
72
|
+
control = jnp.clip(control, -1, 1)
|
|
73
|
+
theta1, theta2, theta1_dot, theta2_dot = state
|
|
74
|
+
|
|
75
|
+
d1 = self.m1 * self.lc1**2 + self.m2 * (self.l1**2 + self.lc2**2 + 2 * self.l1 * self.lc2 * jnp.cos(theta2)) + self.moi1 + self.moi2
|
|
76
|
+
d2 = self.m2 * (self.lc2**2 + self.l1 * self.lc2 * jnp.cos(theta2)) + self.moi2
|
|
77
|
+
|
|
78
|
+
phi2 = self.m2 * self.lc2 * self.g * jnp.cos(theta1 + theta2 - jnp.pi/2)
|
|
79
|
+
phi1 = -self.m2 * self.l1 * self.lc2 * theta2_dot**2 * jnp.sin(theta2) - 2 * self.m2 * self.l1 * self.lc2 * theta1_dot * theta2_dot * jnp.sin(theta1) \
|
|
80
|
+
+ (self.m1 * self.lc1 + self.m2 * self.l1) * self.g * jnp.cos(theta1 - jnp.pi/2) + phi2
|
|
81
|
+
|
|
82
|
+
theta2_acc = (control + d2/d1 * phi1 - self.m2 * self.l1 * self.lc2 * theta1_dot**2 * jnp.sin(theta2) - phi2) \
|
|
83
|
+
/ (self.m2 * self.lc2**2 + self.moi2 - d2**2 / d1)
|
|
84
|
+
theta1_acc = -(d2 * theta2_acc + phi1)/d1
|
|
85
|
+
|
|
86
|
+
return jnp.array([
|
|
87
|
+
theta1_dot,
|
|
88
|
+
theta2_dot,
|
|
89
|
+
theta1_acc,
|
|
90
|
+
theta2_acc
|
|
91
|
+
])
|
|
92
|
+
|
|
93
|
+
def diffusion(self, t, state, args):
|
|
94
|
+
return self.V
|
|
95
|
+
|
|
96
|
+
def fitness_function(self, state, control, target, ts):
|
|
97
|
+
reached_threshold = jax.vmap(lambda theta1, theta2: -jnp.cos(theta1) - jnp.cos(theta1 + theta2) > 1.5)(state[:,0], state[:,1])
|
|
98
|
+
first_success = jnp.argmax(reached_threshold)
|
|
99
|
+
|
|
100
|
+
control_cost = jax.vmap(lambda _state, _u: _u@self.R@_u)(state, control)
|
|
101
|
+
costs = jnp.where((ts/(ts[1]-ts[0]))>first_success, jnp.zeros_like(control_cost), control_cost)
|
|
102
|
+
|
|
103
|
+
return first_success + (first_success == 0) * ts.shape[0] + jnp.sum(costs)
|
|
104
|
+
|
|
105
|
+
def cond_fn_nan(self, t, y, args, **kwargs):
|
|
106
|
+
return jnp.where((jnp.abs(y[2])>(8*jnp.pi)) | (jnp.abs(y[3])>(18*jnp.pi)) | jnp.any(jnp.isnan(y)) | jnp.any(jnp.isinf(y)), -1.0, 1.0)
|
|
107
|
+
|
|
108
|
+
class Acrobot2(EnvironmentBase):
|
|
109
|
+
def __init__(self, process_noise, obs_noise, n_obs = None):
|
|
110
|
+
self.n_var = 4
|
|
111
|
+
self.n_control = 2
|
|
112
|
+
self.n_targets = 0
|
|
113
|
+
self.n_dim = 1
|
|
114
|
+
self.init_bounds = jnp.array([0.1,0.1,0.1,0.1])
|
|
115
|
+
self.default_obs = 4
|
|
116
|
+
super().__init__(process_noise, obs_noise, self.n_var, self.n_control, self.n_dim, n_obs if n_obs else self.default_obs)
|
|
117
|
+
|
|
118
|
+
self.R = jnp.array(0.01)*jnp.eye(self.n_control)
|
|
119
|
+
|
|
120
|
+
def sample_init_states(self, batch_size, key):
|
|
121
|
+
init_key, target_key = jrandom.split(key)
|
|
122
|
+
x0 = jrandom.uniform(init_key, shape=(batch_size, self.n_var), minval= -self.init_bounds, maxval= self.init_bounds)
|
|
123
|
+
targets = jnp.zeros((batch_size, self.n_targets))
|
|
124
|
+
return x0, targets
|
|
125
|
+
|
|
126
|
+
def sample_params(self, batch_size, mode, ts, key):
|
|
127
|
+
l1_key, l2_key, m1_key, m2_key, args_key = jrandom.split(key, 5)
|
|
128
|
+
if mode == "Constant":
|
|
129
|
+
l1 = l2 = m1 = m2 = jnp.ones((batch_size))
|
|
130
|
+
elif mode == "Different":
|
|
131
|
+
l1 = jrandom.uniform(l1_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
132
|
+
l2 = jrandom.uniform(l2_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
133
|
+
m1 = jrandom.uniform(m1_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
134
|
+
m2 = jrandom.uniform(m2_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
135
|
+
elif mode == "Switch":
|
|
136
|
+
switch_times = jrandom.randint(args_key, shape=(batch_size,), minval=int(ts.shape[0]/4), maxval=int(3*ts.shape[0]/4))
|
|
137
|
+
l1 = jnp.zeros((batch_size, ts.shape[0]))
|
|
138
|
+
l2 = jnp.zeros((batch_size, ts.shape[0]))
|
|
139
|
+
m1 = jnp.zeros((batch_size, ts.shape[0]))
|
|
140
|
+
m2 = jnp.zeros((batch_size, ts.shape[0]))
|
|
141
|
+
for i in range(batch_size):
|
|
142
|
+
_key11, _key12 = jrandom.split(jrandom.fold_in(l1_key, i))
|
|
143
|
+
l1 = l1.at[i,:switch_times[i]].set(jrandom.uniform(_key11, shape=(), minval=0.75, maxval=1.25))
|
|
144
|
+
l1 = l1.at[i,switch_times[i]:].set(jrandom.uniform(_key12, shape=(), minval=0.75, maxval=1.25))
|
|
145
|
+
|
|
146
|
+
_key21, _key22 = jrandom.split(jrandom.fold_in(l2_key, i))
|
|
147
|
+
l2 = l2.at[i,:switch_times[i]].set(jrandom.uniform(_key21, shape=(), minval=0.75, maxval=1.25))
|
|
148
|
+
l2 = l2.at[i,switch_times[i]:].set(jrandom.uniform(_key22, shape=(), minval=0.75, maxval=1.25))
|
|
149
|
+
|
|
150
|
+
_key31, _key32 = jrandom.split(jrandom.fold_in(m1_key, i))
|
|
151
|
+
m1 = m1.at[i,:switch_times[i]].set(jrandom.uniform(_key31, shape=(), minval=0.75, maxval=1.25))
|
|
152
|
+
m1 = m1.at[i,switch_times[i]:].set(jrandom.uniform(_key32, shape=(), minval=0.75, maxval=1.25))
|
|
153
|
+
|
|
154
|
+
_key41, _key42 = jrandom.split(jrandom.fold_in(m2_key, i))
|
|
155
|
+
m2 = m2.at[i,:switch_times[i]].set(jrandom.uniform(_key41, shape=(), minval=0.75, maxval=1.25))
|
|
156
|
+
m2 = m2.at[i,switch_times[i]:].set(jrandom.uniform(_key42, shape=(), minval=0.75, maxval=1.25))
|
|
157
|
+
|
|
158
|
+
elif mode == "Decay":
|
|
159
|
+
decay_factors = jrandom.uniform(args_key, shape=(batch_size,2), minval=0.98, maxval=1.02)
|
|
160
|
+
init_l1 = jrandom.uniform(l1_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
161
|
+
init_l2 = jrandom.uniform(l2_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
162
|
+
init_m1 = jrandom.uniform(m1_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
163
|
+
init_m2 = jrandom.uniform(m2_key, shape=(batch_size,), minval=0.75, maxval=1.25)
|
|
164
|
+
|
|
165
|
+
l1 = jax.vmap(lambda l, d, t: l*(d**t), in_axes=[0, 0, None])(init_l1, decay_factors[:,0], ts)
|
|
166
|
+
l2 = jax.vmap(lambda l, d, t: l*(d**t), in_axes=[0, 0, None])(init_l2, decay_factors[:,1], ts)
|
|
167
|
+
m1 = jax.vmap(lambda m, d, t: m*(d**t), in_axes=[0, 0, None])(init_m1, decay_factors[:,0], ts)
|
|
168
|
+
m2 = jax.vmap(lambda m, d, t: m*(d**t), in_axes=[0, 0, None])(init_m2, decay_factors[:,1], ts)
|
|
169
|
+
|
|
170
|
+
return l1, l2, m1, m2
|
|
171
|
+
|
|
172
|
+
def f_obs(self, key, t_x):
|
|
173
|
+
_, out = super().f_obs(key, t_x)
|
|
174
|
+
out = jnp.array([(out[0]+jnp.pi)%(2*jnp.pi)-jnp.pi, (out[1]+jnp.pi)%(2*jnp.pi)-jnp.pi, out[2], out[3]])[:self.n_obs]
|
|
175
|
+
return key, out
|
|
176
|
+
|
|
177
|
+
def initialize_parameters(self, params, ts):
|
|
178
|
+
l1, l2, m1, m2 = params
|
|
179
|
+
self.l1 = l1 # [m]
|
|
180
|
+
self.l2 = l2 # [m]
|
|
181
|
+
self.m1 = m1 #: [kg] mass of link 1
|
|
182
|
+
self.m2 = m2 #: [kg] mass of link 2
|
|
183
|
+
self.lc1 = 0.5*self.l1 #: [m] position of the center of mass of link 1
|
|
184
|
+
self.lc2 = 0.5*self.l2 #: [m] position of the center of mass of link 2
|
|
185
|
+
self.moi1 = self.moi2 = 1.0
|
|
186
|
+
self.g = 9.81
|
|
187
|
+
|
|
188
|
+
self.G = jnp.array([[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,1]])
|
|
189
|
+
self.V = self.process_noise*self.G
|
|
190
|
+
|
|
191
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
192
|
+
self.W = self.obs_noise*jnp.eye(self.n_obs)*(jnp.array([1,1,1,1])[:self.n_obs])
|
|
193
|
+
|
|
194
|
+
def drift(self, t, state, args):
|
|
195
|
+
control = jnp.squeeze(args)
|
|
196
|
+
control = jnp.clip(control, -1, 1)
|
|
197
|
+
c1, c2 = control
|
|
198
|
+
theta1, theta2, theta1_dot, theta2_dot = state
|
|
199
|
+
|
|
200
|
+
d1 = self.m1 * self.lc1**2 + self.m2 * (self.l1**2 + self.lc2**2 + 2 * self.l1 * self.lc2 * jnp.cos(theta2)) + self.moi1 + self.moi2
|
|
201
|
+
d2 = self.m2 * (self.lc2**2 + self.l1 * self.lc2 * jnp.cos(theta2)) + self.moi2
|
|
202
|
+
|
|
203
|
+
phi2 = self.m2 * self.lc2 * self.g * jnp.cos(theta1 + theta2 - jnp.pi/2)
|
|
204
|
+
phi1 = -self.m2 * self.l1 * self.lc2 * theta2_dot**2 * jnp.sin(theta2) - 2 * self.m2 * self.l1 * self.lc2 * theta1_dot * theta2_dot * jnp.sin(theta1) \
|
|
205
|
+
+ (self.m1 * self.lc1 + self.m2 * self.l1) * self.g * jnp.cos(theta1 - jnp.pi/2) + phi2
|
|
206
|
+
|
|
207
|
+
theta2_acc = (c1 + d2/d1 * phi1 - self.m2 * self.l1 * self.lc2 * theta1_dot**2 * jnp.sin(theta2) - phi2) \
|
|
208
|
+
/ (self.m2 * self.lc2**2 + self.moi2 - d2**2 / d1)
|
|
209
|
+
theta1_acc = (c2 - d2 * theta2_acc - phi1)/d1
|
|
210
|
+
|
|
211
|
+
return jnp.array([
|
|
212
|
+
theta1_dot,
|
|
213
|
+
theta2_dot,
|
|
214
|
+
theta1_acc,
|
|
215
|
+
theta2_acc
|
|
216
|
+
])
|
|
217
|
+
|
|
218
|
+
def diffusion(self, t, state, args):
|
|
219
|
+
return self.V
|
|
220
|
+
|
|
221
|
+
def fitness_function(self, state, control, target, ts):
|
|
222
|
+
reached_threshold = jax.vmap(lambda theta1, theta2: -jnp.cos(theta1) - jnp.cos(theta1 + theta2) > 1.5)(state[:,0], state[:,1])
|
|
223
|
+
first_success = jnp.argmax(reached_threshold)
|
|
224
|
+
|
|
225
|
+
control_cost = jax.vmap(lambda _state, _u: _u@self.R@_u)(state, control)
|
|
226
|
+
costs = jnp.where((ts/(ts[1]-ts[0]))>first_success, jnp.zeros_like(control_cost), control_cost)
|
|
227
|
+
|
|
228
|
+
return first_success + (first_success == 0) * ts.shape[0] + jnp.sum(costs)
|
|
229
|
+
|
|
230
|
+
def terminate_event(self, state, **kwargs):
|
|
231
|
+
return (jnp.abs(state.y[2])>(8*jnp.pi)) | (jnp.abs(state.y[3])>(18*jnp.pi)) | jnp.any(jnp.isnan(state.y)) | jnp.any(jnp.isinf(state.y))
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kozax: Genetic programming framework in JAX
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 sdevries0
|
|
5
|
+
|
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
This program is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import jax
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
import jax.random as jrandom
|
|
23
|
+
|
|
24
|
+
from Kozax.environments.control_environments.control_environment_base import EnvironmentBase
|
|
25
|
+
|
|
26
|
+
class CartPole(EnvironmentBase):
|
|
27
|
+
def __init__(self, process_noise, obs_noise, n_obs = 4):
|
|
28
|
+
self.n_var = 4
|
|
29
|
+
self.n_control = 1
|
|
30
|
+
self.n_targets = 0
|
|
31
|
+
self.n_dim = 1
|
|
32
|
+
self.init_bounds = jnp.array([0.05,0.05,0.05,0.05])
|
|
33
|
+
super().__init__(process_noise, obs_noise, self.n_var, self.n_control, self.n_dim, n_obs)
|
|
34
|
+
|
|
35
|
+
self.Q = jnp.array(0)
|
|
36
|
+
self.R = jnp.array([[0.0]])
|
|
37
|
+
|
|
38
|
+
def sample_init_states(self, batch_size, key):
|
|
39
|
+
init_key, target_key = jrandom.split(key)
|
|
40
|
+
x0 = jrandom.uniform(init_key, shape=(batch_size, self.n_var), minval= -self.init_bounds, maxval= self.init_bounds)
|
|
41
|
+
targets = jnp.zeros((batch_size, self.n_targets))
|
|
42
|
+
return x0, targets
|
|
43
|
+
|
|
44
|
+
def sample_params(self, batch_size, mode, ts, key):
|
|
45
|
+
#g, mass, length
|
|
46
|
+
return jnp.zeros((batch_size))
|
|
47
|
+
|
|
48
|
+
def initialize_parameters(self, params, ts):
|
|
49
|
+
_ = params
|
|
50
|
+
self.g = 9.81
|
|
51
|
+
self.pole_mass = 0.1
|
|
52
|
+
self.pole_length = 0.5
|
|
53
|
+
self.cart_mass = 1
|
|
54
|
+
|
|
55
|
+
self.G = jnp.array([[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,0]])
|
|
56
|
+
self.V = self.process_noise*self.G
|
|
57
|
+
|
|
58
|
+
self.C = jnp.eye(self.n_var)[:self.n_obs]
|
|
59
|
+
self.W = self.obs_noise*jnp.eye(self.n_obs)
|
|
60
|
+
|
|
61
|
+
def drift(self, t, state, args):
|
|
62
|
+
control = jnp.squeeze(args)
|
|
63
|
+
control = jnp.clip(control, -1, 1)
|
|
64
|
+
x, theta, x_dot, theta_dot = state
|
|
65
|
+
|
|
66
|
+
cos_theta = jnp.cos(theta)
|
|
67
|
+
sin_theta = jnp.sin(theta)
|
|
68
|
+
|
|
69
|
+
theta_acc = (self.g * sin_theta - cos_theta * (
|
|
70
|
+
control + self.pole_mass * self.pole_length * theta_dot**2 * sin_theta
|
|
71
|
+
) / (self.cart_mass + self.pole_mass)) / (
|
|
72
|
+
self.pole_length * (4/3 - (self.pole_mass * cos_theta**2) / (self.cart_mass + self.pole_mass)))
|
|
73
|
+
|
|
74
|
+
x_acc = (control + self.pole_mass * self.pole_length * (theta_dot**2 * sin_theta - theta_acc * cos_theta)) / (self.cart_mass + self.pole_mass)
|
|
75
|
+
|
|
76
|
+
# x_acc = control
|
|
77
|
+
# theta_acc = (self.g/self.pole_length) * jnp.sin(theta)
|
|
78
|
+
|
|
79
|
+
return jnp.array([
|
|
80
|
+
x_dot,
|
|
81
|
+
theta_dot,
|
|
82
|
+
x_acc,
|
|
83
|
+
theta_acc
|
|
84
|
+
])
|
|
85
|
+
|
|
86
|
+
def diffusion(self, t, x, args):
|
|
87
|
+
return self.V
|
|
88
|
+
|
|
89
|
+
def fitness_function(self, state, control, target, ts):
|
|
90
|
+
invalid_points = jax.vmap(lambda _x, _u: jnp.any(jnp.isinf(_x)) + jnp.isnan(_u))(state, control[:,0])
|
|
91
|
+
punishment = jnp.ones_like(invalid_points)
|
|
92
|
+
# costs_control = jax.vmap(lambda _state, _u: _u@self.R@_u)(state, u)
|
|
93
|
+
|
|
94
|
+
costs = jnp.where(invalid_points, punishment, jnp.zeros_like(punishment))
|
|
95
|
+
|
|
96
|
+
return jnp.sum(costs)
|
|
97
|
+
|
|
98
|
+
def terminate_event(self, state, **kwargs):
|
|
99
|
+
return (jnp.abs(state.y[1])>0.2) | (jnp.abs(state.y[0])>4.8) | jnp.any(jnp.isnan(state.y)) | jnp.any(jnp.isinf(state.y))
|