-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTestScript.py
More file actions
58 lines (45 loc) · 1.69 KB
/
TestScript.py
File metadata and controls
58 lines (45 loc) · 1.69 KB
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
import os
import unittest
import gymnasium as gym
import numpy as np
from evorob.utils.filesys import get_project_root
from evorob.algorithms.ga import GA, GA_opts
ROOT_DIR = get_project_root()
class MyTestCase(unittest.TestCase):
def test_gym(self):
ENV_NAMES = ["HalfCheetah-v5"]
for ENV_NAME in ENV_NAMES:
env = gym.make(
ENV_NAME,
render_mode='human')
rewards = None
env.reset()
for step in range(100):
actions = np.random.uniform(low=-0.3, high=0.3, size=env.action_space.shape[0])
observations, rewards, terminated, truncated, info = env.step(actions)
if terminated:
break
env.close()
self.assertFalse(rewards is None, "Gym environment invalid")
def f_reversed_ackley(self, x, y):
return -1 * (
-20.0 * np.exp(-0.2 * np.sqrt(0.5 * (x ** 2 + y ** 2)))
- np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
+ np.e
+ 20
)
def test_functions(self):
pop_size = 50
n_params = 2
results_dir = os.path.join(ROOT_DIR, "results", "TEST")
ea = GA(pop_size, n_params, GA_opts, results_dir)
for _ in range(ea.n_gen):
pop = ea.ask()
fitnesses_gen = np.empty(ea.n_pop)
for index, individual in enumerate(pop):
fit_ind = self.f_reversed_ackley(*individual)
fitnesses_gen[index] = fit_ind
ea.tell(pop, fitnesses_gen)
self.assertLess(-0.1, ea.x_best_so_far.max())
if __name__ == '__main__':
unittest.main()