|
| 1 | +import numpy as np |
| 2 | +from moead_framework.problem.problem import Problem |
| 3 | +from moead_framework.solution.one_dimension_solution import OneDimensionSolution |
| 4 | + |
| 5 | + |
| 6 | +class Mubqp(Problem): |
| 7 | + |
| 8 | + def __init__(self, instance_file): |
| 9 | + file = open(instance_file, 'r') |
| 10 | + file_content = list(map(str.strip, file.readlines())) |
| 11 | + file.close() |
| 12 | + |
| 13 | + file_content = file_content[6:] |
| 14 | + |
| 15 | + definition = file_content[0].split(" ") |
| 16 | + self.rho = float(definition[2]) |
| 17 | + self.m = int(definition[3]) |
| 18 | + self.n = int(definition[4]) |
| 19 | + self.d = float(definition[5]) |
| 20 | + file_content = file_content[2:] |
| 21 | + |
| 22 | + super().__init__(objective_number=self.m) |
| 23 | + |
| 24 | + self.qs = np.zeros((self.m, self.n, self.n)) |
| 25 | + self.load_qs(file_content) |
| 26 | + |
| 27 | + def f(self, function_id, solution): |
| 28 | + fit = 0 |
| 29 | + |
| 30 | + for i in range(self.n): |
| 31 | + if solution[i] == 1: |
| 32 | + for j in range(i+1): |
| 33 | + if solution[j] == 1: |
| 34 | + fit += self.qs[function_id][i][j] |
| 35 | + |
| 36 | + return fit |
| 37 | + |
| 38 | + def generate_random_solution(self, evaluate=True): |
| 39 | + return self.generate_solution(array=np.random.randint(0, 2, self.n).tolist()[:], evaluate=evaluate) |
| 40 | + |
| 41 | + def generate_solution(self, array, evaluate=True): |
| 42 | + x = OneDimensionSolution(np.array(array, dtype=int)) |
| 43 | + |
| 44 | + for j in range(self.function_numbers): |
| 45 | + if evaluate: |
| 46 | + x.F.append(self.f(j, x.solution)) |
| 47 | + else: |
| 48 | + x.F.append(None) |
| 49 | + |
| 50 | + return x |
| 51 | + |
| 52 | + def load_qs(self, array): |
| 53 | + line = 0 |
| 54 | + for i in range(self.n): |
| 55 | + for j in range(self.n): |
| 56 | + s = array[line].split(" ") |
| 57 | + line += 1 |
| 58 | + for n in range(self.m): |
| 59 | + self.qs[n][i][j] = int(s[n]) |
| 60 | + pass |
| 61 | + |
0 commit comments