-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (134 loc) · 4.1 KB
/
main.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import argparse
import copy
import numpy as np
from omegaconf import DictConfig
import pickle as pkl
import time
from lmao.factory import (
config_factory,
function_factory,
VALID_FUNCTIONS,
VALID_SOLVERS
)
from lmao.solver import BOSolver
DESCRIPTION = "Lava Multi-Agent Optimization (LMAO)"
def get_config() -> DictConfig:
"""
Get the configuration for the optimization process.
Returns:
DictConfig: The configuration as a `DictConfig` object.
"""
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
"--function",
type=str,
choices=VALID_FUNCTIONS,
default="ackley",
help="The function to optimize",
)
parser.add_argument(
"--return-lp",
action="store_true",
help="Return the Lava process wrapper for the function",
)
parser.add_argument(
"--max-iter",
type=int,
default=50,
dest="max_iterations",
help="The maximum number of iterations to run",
)
parser.add_argument(
"--num_initial_points",
type=int,
default=10,
help="The number of initial points to use for optimizer initialization",
)
parser.add_argument(
"--num_processes",
type=int,
default=1,
help="The number of processes to use for optimization",
)
parser.add_argument(
"--num_repeats",
type=int,
default=1,
help="The number of times to repeat the optimization at every iteration",
)
parser.add_argument(
"--optimizer_class",
type=str,
default="gp-cpu",
help="The class of optimizer to use",
choices=VALID_SOLVERS
)
parser.add_argument(
"--seed",
type=int,
default=0,
help="The random seed to use for the optimization process",
)
parser.add_argument(
"--run-idx",
type=int,
default=0,
help="The index of the run",
)
args = parser.parse_args()
config: dict = vars(args)
config: DictConfig = DictConfig(config)
return config
def print_intro(config: DictConfig, delimiter="-", delimiter_width=60):
"""
Print the introduction section of the program.
Args:
config (DictConfig): The configuration object containing program
settings.
delimiter (str, optional): The delimiter character to use for the
separator line. Defaults to "-".
delimiter_width (int, optional): The width of the separator line.
Defaults to 60.
"""
print()
print(delimiter * delimiter_width)
print(DESCRIPTION)
print(delimiter * delimiter_width)
print(f"Configuration:")
print(f" - Black Box Function: {config.function}")
print(f" - Return Lava Process: {config.return_lp}")
print(f" - Max Iterations: {config.max_iterations}")
print(f" - Number of IPs: {config.num_initial_points}")
print(f" - Number of Processes: {config.num_processes}")
print(f" - Number of Repeats: {config.num_repeats}")
print(f" - Optimizer Class: {config.optimizer_class}")
print(f" - Random Seed: {config.seed}")
print(f" - Run Index: {config.run_idx}")
print(delimiter * delimiter_width)
def main(config: DictConfig):
"""
Executes the main logic of the program.
Args:
config (DictConfig): The configuration object containing program
settings.
TODO Add Parameters
Returns:
None
"""
print_intro(config)
config.optimizer = config_factory(config)
function_process, search_space, minima = function_factory(config.function, return_lp=config.return_lp)
solver = BOSolver(config)
solve_start = time.time()
results = solver.solve(
ufunc=function_process,
use_lp=config.return_lp,
search_space=search_space
)
solve_end = time.time()
total_time = solve_end - solve_start
print(f"Total Time: {total_time}")
return total_time, results
if __name__ == "__main__":
config = get_config()
times, results = main(config)