-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem.py
57 lines (50 loc) · 1.98 KB
/
problem.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
import numpy as np
class Problem:
def __init__(self, best_sol = None, worst_sol = None, instance_name = None,
best_fitness = None, worst_fitness=None):
self.best_sol = best_sol
self.worst_sol = worst_sol
self.instance_name = instance_name
if best_sol is None:
self.best_fitness = best_fitness
else:
self.best_fitness = self.fitness_nosave(best_sol)
if best_fitness != None:
assert self.best_fitness == best_fitness
if worst_sol is None:
self.worst_fitness = None
else:
self.worst_fitness = self.fitness_nosave(worst_sol)
if worst_fitness != None:
assert self.worst_fitness == worst_fitness
self.reset()
def check_permutation(self, x):
# Assumes numpy array
return ((x >= 0) & (x < self.n)).all() and np.unique(x).shape[0] == x.shape[0]
def reset(self):
self.evaluations = []
self.solutions = []
def fitness_nosave(self, x):
raise NotImplementedError("virtual method")
def fitness(self, x):
f = self.fitness_nosave(x)
self.solutions.append(x)
self.evaluations.append(f)
return f
def distance_to_best(self, perm, distance):
if self.best_sol is None:
return np.nan
# MANUEL: Why divide by n * (n -1) / 2 ?
# if kendall: return kendallTau(perm, self.best_sol) / (self.n * (self.n - 1) * 0.5)
# MANUEL: Why distance to identity?
# return self.n - (perm==np.arange(self.n)).sum()
return distance(perm, self.best_sol)
# # Returns a closure function that can be called from R.
# # WARNING: this function minimizes for CEGO
# def make_r_fitness(self):
# @ri.rternalize
# def r_fitness(x):
# xpy = np.asarray(x) - 1 # R vectors are 1-indexed
# y = self.fitness(xpy)
# return FloatVector(np.asarray(y))
# return r_fitness