-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlgorithm.py
More file actions
111 lines (89 loc) · 4.7 KB
/
Copy pathAlgorithm.py
File metadata and controls
111 lines (89 loc) · 4.7 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import yaml
import pygmo as pg
import random
import torch
import numpy
import MORoverInterface
import MOBeachInterface
import Policy
import Individual
import Utils
import ExpUtils.DataLogger
class CentralisedAlgorithm:
def __init__(self, alg_config_filename, domain_name="rover", domain_config_filename=None, data_filename=None):
self.config_filename = alg_config_filename
self._read_config()
self.data_filename = data_filename
self.data_logger = ExpUtils.DataLogger.DataLogger(data_fields=['gen',
'id',
'fitness',
'trajectory'],
target_filename=self.data_filename)
if domain_name == "rover":
self.interface = MORoverInterface.MORoverInterface(domain_config_filename)
elif domain_name == "beach":
self.interface = MOBeachInterface.MOBeachInterface(domain_config_filename)
self.team_size = self.interface.get_team_size()
self.num_objs = self.interface.get_num_objs()
self.pop = []
self.glob_ind_counter = 0
# Create the initial population
for i in range(self.pop_size):
# Add new individual ot the population
self.pop.append(Individual.Individual(config_filename=self.config_filename,
num_agents=self.team_size,
input_size=self.interface.get_state_size(),
output_size=self.interface.get_action_size(),
id=i,
num_objs=self.num_objs))
# Increment the global id counter
self.glob_ind_counter += 1
# Evo utils
self.utils = Utils.Utils(num_objs=self.num_objs)
def _read_config(self):
"""Read and load NSGA-II configuration from the YAML file."""
with open(self.config_filename, 'r') as config_file:
self.config_data = yaml.safe_load(config_file)
print('[NSGA-II]: YAML config read.')
self._load_config()
def _load_config(self):
"""Load internal NSGA-II configuration."""
self.pop_size = self.config_data['Evolutionary']['pop_size']
self.num_gens = self.config_data['Evolutionary']['num_gens']
class CoevolutionaryAlgorithm:
def __init__(self, alg_config_filename, domain_name="rover", domain_config_filename=None, data_filename=None):
self.config_filename = alg_config_filename
self._read_config()
self.data_filename = data_filename
self.data_logger = ExpUtils.DataLogger.DataLogger(data_fields=['gen',
'id',
'fitness',
'trajectory'],
target_filename=self.data_filename)
if domain_name == "rover":
self.interface = MORoverInterface.MORoverInterface(domain_config_filename)
elif domain_name == "beach":
self.interface = MOBeachInterface.MOBeachInterface(domain_config_filename)
self.team_size = self.interface.get_team_size()
self.num_objs = self.interface.get_num_objs()
self.pop = [] # NOTE: A list of subpopulations
self.glob_eval_counter = 0
for _ in range(self.team_size):
# Create a subpopulation of policies
subpop = [Policy.Policy(self.config_filename,
self.interface.get_state_size(),
self.interface.get_action_size()) for _ in range(self.pop_size)]
# Add subpop to the population
self.pop.append(subpop)
# Evo utils
self.utils = Utils.Utils(num_objs=self.num_objs)
def _read_config(self):
"""Read and load NSGA-II configuration from the YAML file."""
with open(self.config_filename, 'r') as config_file:
self.config_data = yaml.safe_load(config_file)
print('[NSGA-II+D]: YAML config read.')
self._load_config()
def _load_config(self):
"""Load internal NSGA-II configuration."""
self.pop_size = self.config_data['Evolutionary']['pop_size']
self.num_gens = self.config_data['Evolutionary']['num_gens']