Skip to content

Commit

Permalink
dev: auto setup monitor to use opt_direction
Browse files Browse the repository at this point in the history
  • Loading branch information
BillHuang2001 committed Oct 17, 2023
1 parent 8f32ae7 commit 7a9459e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/evox/monitors/std_mo_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self, record_pf=False, record_fit_history=True):
self.current_population = None
self.pf_solutions = None
self.pf_fitness = None
self.opt_direction = 1 # default to min, so no transformation is needed

def set_opt_direction(self, opt_direction):
self.opt_direction = opt_direction

def record_pop(self, pop, tranform=None):
self.current_population = pop
Expand Down Expand Up @@ -57,16 +61,16 @@ def record_fit(self, fitness, metrics=None, tranform=None):
self.pf_solutions = self.pf_solutions[pf]

def get_last(self):
return self.fitness_history[-1]
return self.opt_direction * self.fitness_history[-1]

def get_pf_fitness(self):
return self.pf_fitness
return self.opt_direction * self.pf_fitness

def get_pf_solutions(self):
return self.pf_solutions

def get_history(self):
return self.fitness_history
return [self.opt_direction * fit for fit in self.fitness_history]

def flush(self):
hcb.barrier_wait()
Expand Down
12 changes: 8 additions & 4 deletions src/evox/monitors/std_so_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def __init__(self, record_topk=1, record_fit_history=True):
self.current_population = None
self.topk_solutions = None
self.topk_fitness = None
self.opt_direction = 1 # default to min, so no transformation is needed

def set_opt_direction(self, opt_direction):
self.opt_direction = opt_direction

def record_pop(self, pop, tranform=None):
self.current_population = pop
Expand Down Expand Up @@ -66,10 +70,10 @@ def record_fit(self, fitness, metrics=None, transform=None):
self.topk_fitness = self.topk_fitness[topk_rank]

def get_last(self):
return self.fitness_history[-1]
return self.opt_direction * self.fitness_history[-1]

def get_topk_fitness(self):
return self.topk_fitness
return self.opt_direction * self.topk_fitness

def get_topk_solutions(self):
return self.topk_solutions
Expand All @@ -78,7 +82,7 @@ def get_best_fitness(self):
if self.topk_fitness is None:
warnings.warn("trying to get info from a monitor with no recorded data")
return None
return self.topk_fitness[0]
return self.opt_direction * self.topk_fitness[0]

def get_best_solution(self):
if self.topk_solutions is None:
Expand All @@ -87,7 +91,7 @@ def get_best_solution(self):
return self.topk_solutions[0]

def get_history(self):
return self.fitness_history
return [self.opt_direction * fit for fit in self.fitness_history]

def flush(self):
hcb.barrier_wait()
Expand Down
1 change: 1 addition & 0 deletions src/evox/workflows/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def __init__(
self.async_dispatch_list = deque()
self.async_dispatch = async_dispatch
self.monitor = monitor
self.monitor.set_opt_direction(opt_direction)
self.record_pop = record_pop
self.record_time = record_time
self.metrics = metrics
Expand Down
1 change: 1 addition & 0 deletions src/evox/workflows/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
self.pop_transform = pop_transform
self.fitness_transform = fitness_transform
self.opt_direction = parse_opt_direction(opt_direction)
self.monitor.set_opt_direction(self.opt_direction)

def step(self, state):
if self.monitor and self.record_time:
Expand Down
1 change: 1 addition & 0 deletions src/evox/workflows/uni_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __init__(
self.num_objectives = num_objectives
self.distributed_step = False
self.opt_direction = parse_opt_direction(opt_direction)
self.monitor.set_opt_direction(self.opt_direction)
if jit_problem is False and self.num_objectives is None:
warnings.warn(
(
Expand Down

0 comments on commit 7a9459e

Please sign in to comment.