def _discordance_ij(self, x: List[Interval], y: List[Interval], criteria: int) -> float: veto = self.preference_model.veto[criteria] if self.objectives_type[criteria]: return Interval(y[criteria]).poss_greater_than_or_eq(x[criteria] + veto) return Interval(y[criteria]).poss_smaller_than_or_eq(x[criteria] - veto)
def dm_generator(number_of_objectives: int, number_of_variables: int, max_objectives: List[Interval]): print(number_of_variables, number_of_objectives, max_objectives) generator = DMGenerator(number_of_variables=number_of_variables, number_of_objectives=number_of_objectives, max_objectives=max_objectives) w, veto = generator.make() print(number_of_objectives) print(number_of_variables) print(', '.join(map(str, w))) print(', '.join(map(str, veto))) print(Interval(0.51, 0.75)) print(Interval(0.51, 0.67))
def compare_(self, x: List, y: List) -> int: """ -1 if xPy, 0 if x~y, 1 otherwise """ if self.dominance_comparator.dominance_test_(x, y) == -1: return -1 ux = Interval(0) uy = Interval(0) for idx in range(len(x)): ux += self.preference_model.weights[idx] * x[idx] uy += self.preference_model.weights[idx] * y[idx] if ux >= uy: return -1 if ux < uy: return 1 return 0
def compare(self, x: Solution, y: Solution) -> int: """ -1 if xPy, 0 if x~y, 1 otherwise """ if self.dominance_comparator.compare(x, y) == -1: return -1 ux = Interval(0) uy = Interval(0) for idx in range(x.number_of_objectives): ux += self.preference_model.weights[idx] * x.objectives[idx] uy += self.preference_model.weights[idx] * y.objectives[idx] if ux >= uy: return -1 if ux < uy: return 1 return 0
def evaluate(self, solution: BinarySolution) -> BinarySolution: current_budget = Interval(0) objectives = self.number_of_objectives * [Interval(0)] for index, bits in enumerate(solution.variables[0]): if bits: current_budget += self.instance_.projects[index][0] for obj in range(0, self.number_of_objectives): objectives[obj] += self.instance_.projects[index][obj + 1] poss = self.budget.poss_greater_than_or_eq(current_budget) if poss < self.get_preference_model(0).chi: solution.constraints = [self.budget - current_budget] else: solution.constraints = [0] solution.budget = current_budget solution.objectives = objectives return solution
def dominance_test_(self, solution1: List, solution2: List) -> float: best_is_one = 0 best_is_two = 0 value1_strictly_greater = False value2_strictly_greater = False for i in range(len(solution1)): value1 = Interval(solution1[i]) value2 = Interval(solution2[i]) if self.objectives_type[i]: poss = value1.possibility(value2) else: poss = value2.possibility(value1) if poss >= self.alpha: if not value1_strictly_greater and poss > 0.5: value1_strictly_greater = True best_is_one += 1 if self.objectives_type[i]: poss = value2.possibility(value1) else: poss = value1.possibility(value2) if poss >= self.alpha: if not value2_strictly_greater and poss > 0.5: value2_strictly_greater = True best_is_two += 1 s1_dominates_s2 = False s2_dominates_s1 = False if value1_strictly_greater and best_is_one == len(solution1): s1_dominates_s2 = True if value2_strictly_greater and best_is_two == len(solution1): s2_dominates_s1 = True if s2_dominates_s1 and s1_dominates_s2: return 0 if s1_dominates_s2: return -1 if s2_dominates_s1: return 1 if not s2_dominates_s1 and not s1_dominates_s2 and best_is_one != len( solution1) and best_is_two != len(solution1): if self.objectives_type[0]: # Max if best_is_one < best_is_two: return -1 if best_is_two < best_is_one: return 1 else: if best_is_one > best_is_two: return -1 if best_is_two > best_is_one: return 1 return 0
def create_solution(self) -> BinarySolution: new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives) new_solution.variables[0] = [] budget = Interval(0) random.shuffle(self.positions) new_solution.variables[0] = self.number_of_bits * [False] for v in self.positions: tmp = budget + self.instance_.projects[v][0] poss = self.budget.poss_greater_than_or_eq(tmp) if poss >= self.get_preference_model(0).chi: new_solution.variables[0][v] = True budget = tmp return new_solution
def _generate_weights(self): weight = [] valid = False while not valid: valid = True weight = self._butler_weight() if sum(weight) == 1.0: if self.numberOfObjectives == 3: for _ in range(self.numberOfObjectives - 1): if weight[_] > 0.5 * (1 - weight[_]): valid = False break if weight[self.numberOfObjectives - 1] > 0.6 * ( 1 - weight[self.numberOfObjectives - 1]): valid = False else: for v in weight: if v > 0.5 * (1 - v): valid = False break else: valid = False return [Interval(w) for w in weight]
def _generate_veto(self, weights: List[Interval]): v = [] min_, max_ = min(weights), max(weights) weights_norm = [(v_ - min_) / (max_ - min_) for v_ in weights] idx = 0 while idx < self.numberOfObjectives: midp = self.maxObjectives[idx].midpoint() width = self.maxObjectives[idx].width() r1 = random.uniform(0, 1) vl = midp - r1 * (width / 10.0) r2 = random.uniform(0, 1) vu = midp + r2 * (width / 10.0) v.append(Interval(vl, vu)) valid = False for jdx in range(idx): if weights_norm[jdx] >= weights_norm[idx] and v[jdx] >= v[idx]: valid = True if not valid: idx += 1 # print("before re-normalize:", v) # return [(value + min_) * (max_ - min_) for value in v] return v
def _concordance_index(self, gamma: float, omegas: List) -> Interval: cl = 0 cu = 0 dl = 0 du = 0 i_weights = self.preference_model.weights for idx in range(len(i_weights)): if omegas[idx] >= gamma: self.coalition[idx] = 1 cl += i_weights[idx].lower cu += i_weights[idx].upper else: self.coalition[idx] = 0 dl += i_weights[idx].lower du += i_weights[idx].upper if (cl + du) >= 1: lower = cl else: lower = 1 - du if (cu + dl) <= 1: upper = cu else: upper = 1 - dl return Interval(lower, upper)
def compute(self, is_objective: bool = False): dms = self.instance_.attributes[ 'dms'] if 'dms' else 1 in self.instance_.attributes.keys() frontiers_objectives = self.instance_.attributes['frontiers'] for dm in range(dms): best_compromise = self.problem_.generate_existing_solution( self.instance_.attributes['best_compromise'][dm], is_objective) print('best compromise', best_compromise.objectives) dif_ideal_front = [] r2 = [[]] r1 = [[]] for v in frontiers_objectives: print(v) for idx in range(self.problem_.number_of_objectives): v = Interval(best_compromise.objectives[idx] - frontiers_objectives[dm][0][idx]) dif_ideal_front.append(abs(v.midpoint())) print('DIF ', dif_ideal_front) dominance = ITHDMDominanceComparator( self.problem_.objectives_type, self.problem_.get_preference_model(dm).alpha) if not self.problem_.get_preference_model( dm).supports_utility_function: # Step 1: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[0].append( Interval(best_compromise.objectives[idx] - dif_ideal_front[idx] / 4.0)) else: r2[0].append( Interval(best_compromise.objectives[idx] + dif_ideal_front[idx] / 4.0)) print('6 // OUTRANKING: R2(3) + R1(3)') print(str(r2[0]).replace('[', '').replace(']', '')) while dominance.dominance_test_(best_compromise.objectives, r2[0]) != -1: val = dominance.dominance_test_(best_compromise.objectives, r2[0]) for idx, tmp in enumerate(r2[0]): if self.problem_.objectives_type[idx]: r2[0][idx] = tmp - dif_ideal_front[idx] / 4 else: r2[0][idx] = tmp + dif_ideal_front[idx] / 4 # Step 2: Creando r11 a partir de la frontera for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[0].append((frontiers_objectives[dm][0][idx] - dif_ideal_front[idx] / 4)) else: r1[0].append((frontiers_objectives[dm][0][idx] + dif_ideal_front[idx] / 4)) while dominance.dominance_test_(r2[0], r1[0]) != -1: for idx, tmp in enumerate(r1[0]): if self.problem_.objectives_type[idx]: r1[0][idx] = tmp - dif_ideal_front[idx] / 4 else: r1[0][idx] = tmp + dif_ideal_front[idx] / 4 # Step 3: disminuir dif_r2_r1 = [ abs((r2[0][idx] - r1[0][idx]).midpoint()) for idx in range(self.problem_.number_of_objectives) ] r2 = r2 + [[ v - dif_r2_r1[idx] / 4 if self.problem_.objectives_type[idx] else v + dif_r2_r1[idx] / 4 for idx, v in enumerate(r2[0]) ]] pref = ITHDMPreferences(self.problem_.objectives_type, self.problem_.get_preference_model(dm)) while dominance.dominance_test_( best_compromise.objectives, r2[1]) != -1 or not self.check_assumption44( r2[1], r2, pref, 1): print( dominance.dominance_test_(best_compromise.objectives, r2[1]), self.check_assumption44(r2[1], r2, pref, 1)) for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[1][idx] = r2[1][idx] - dif_r2_r1[idx] / 4 else: r2[1][idx] = r2[1][idx] + dif_r2_r1[idx] / 4 print(str(r2[1]).replace('[', '').replace(']', '')) # Step 3 r23 -> r22, r23[i] = r21 - r11/3 r2 = r2 + [[ v - dif_r2_r1[idx] / 4 if self.problem_.objectives_type[idx] else v + dif_r2_r1[idx] / 4 for idx, v in enumerate(r2[1]) ]] jdx = 0 while dominance.dominance_test_( best_compromise.objectives, r2[2]) != -1 or not self.check_assumption44( r2[2], r2, pref, 2): for idx in range(jdx, self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[2][idx] = r2[2][idx] - dif_r2_r1[idx] / 4 else: r2[2][idx] = r2[2][idx] + dif_r2_r1[idx] / 4 jdx = jdx + 2 if jdx < self.problem_.number_of_objectives else 0 print(str(r2[2]).replace('[', '').replace(']', '')) print(str(r1[0]).replace('[', '').replace(']', '')) dif_r2_r1 = [ abs((r2[2][idx] - r1[0][idx]).midpoint()) for idx in range(self.problem_.number_of_objectives) ] r1 = r1 + [[ v - dif_r2_r1[idx] / 4 if self.problem_.objectives_type[idx] else v + dif_r2_r1[idx] / 4 for idx, v in enumerate(r1[0]) ]] jdx = 0 while dominance.dominance_test_( best_compromise.objectives, r1[1]) != -1 or not self.check_assumption44( r1[1], r1, pref, 1): for idx in range(jdx, self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[1][idx] = r1[1][idx] - dif_r2_r1[idx] / 4 else: r1[1][idx] = r1[1][idx] + dif_r2_r1[idx] / 4 jdx = jdx + 2 if jdx < self.problem_.number_of_objectives else 0 print(str(r1[1]).replace('[', '').replace(']', '')) r1 = r1 + [[ v - dif_r2_r1[idx] / 4 if self.problem_.objectives_type[idx] else v + dif_r2_r1[idx] / 4 for idx, v in enumerate(r1[1]) ]] jdx = 0 while dominance.dominance_test_( best_compromise.objectives, r1[2]) != -1 or not self.check_assumption44( r1[2], r1, pref, 2): for idx in range(jdx, self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[2][idx] = r1[2][idx] - dif_r2_r1[idx] / 4 else: r1[2][idx] = r1[2][idx] + dif_r2_r1[idx] / 4 jdx = jdx + 2 if jdx < self.problem_.number_of_objectives else 0 print(str(r1[2]).replace('[', '').replace(']', '')) else: # Step 1: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[0].append( Interval(best_compromise.objectives[idx] - dif_ideal_front[idx] / 2.0)) else: r2[0].append( Interval(best_compromise.objectives[idx] + dif_ideal_front[idx] / 2.0)) pref = ITHDMPreferenceUF( self.problem_.objectives_type, self.problem_.get_preference_model(dm)) while dominance.dominance_test_(best_compromise.objectives, r2[0]) != -1: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[0][idx] -= dif_ideal_front[idx] / 3 else: r2[0][idx] += dif_ideal_front[idx] / 3 print('6 // UF: R2(3) + R1(3)') print(str(r2[0]).replace('[', '').replace(']', '')) r2 += [[ v - dif_ideal_front[idx] / 3 if self.problem_.objectives_type[idx] else v + dif_ideal_front[idx] / 3 for idx, v in enumerate(r2[0]) ]] while dominance.dominance_test_(best_compromise.objectives, r2[0]) != -1 and pref.compare_( r2[1], r2[0]) != 0: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[1][idx] -= dif_ideal_front[idx] / 3 else: r2[1][idx] += dif_ideal_front[idx] / 3 print(str(r2[1]).replace('[', '').replace(']', '')) r2 += [[ v - dif_ideal_front[idx] / 3 if self.problem_.objectives_type[idx] else v + dif_ideal_front[idx] / 3 for idx, v in enumerate(r2[1]) ]] while dominance.dominance_test_(best_compromise.objectives, r2[1]) != -1 and \ pref.compare_(r2[2], r2[0]) != 0 and pref.compare_(r2[2], r2[1]) != 0: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r2[1][idx] -= dif_ideal_front[idx] / 3 else: r2[1][idx] += dif_ideal_front[idx] / 3 print(str(r2[2]).replace('[', '').replace(']', '')) r1[0] = [ v - dif_ideal_front[idx] if self.problem_.objectives_type[idx] else v + dif_ideal_front[idx] for idx, v in enumerate(r2[2]) ] while not self.check_assumption_74(r1[0], r2, pref): for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[0] -= dif_ideal_front[idx] / 3 else: r1[0] += dif_ideal_front[idx] / 3 print(str(r1[0]).replace('[', '').replace(']', '')) r1 += [[ v - dif_ideal_front[idx] / 3 if self.problem_.objectives_type[idx] else v + dif_ideal_front[idx] / 3 for idx, v in enumerate(r1[0]) ]] while not self.check_assumption_74( r1[1], r2, pref) and pref.compare_(r1[0], r1[1]) != 0: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[1] -= dif_ideal_front[idx] / 3 else: r1[1] += dif_ideal_front[idx] / 3 print(str(r1[1]).replace('[', '').replace(']', '')) r1 += [[ v - dif_ideal_front[idx] / 3 if self.problem_.objectives_type[idx] else v + dif_ideal_front[idx] / 3 for idx, v in enumerate(r1[1]) ]] while not self.check_assumption_74(r1[2], r2, pref) and pref.compare_(r1[0], r1[2]) != 0 and \ pref.compare_(r1[1], r1[2]) != 0: for idx in range(self.problem_.number_of_objectives): if self.problem_.objectives_type[idx]: r1[2] -= dif_ideal_front[idx] / 3 else: r1[2] += dif_ideal_front[idx] / 3 print(str(r1[2]).replace('[', '').replace(']', ''))
def _alpha_ij(self, x: List[Interval], y: List[Interval], criteria: int) -> float: if self.objectives_type[criteria]: return Interval(x[criteria]).poss_greater_than_or_eq(y[criteria]) return Interval(x[criteria]).poss_smaller_than_or_eq(y[criteria])
def read_(self, absolute_path: str) -> DTLZInstance: with open(absolute_path) as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] index = 0 self.n_obj = int(content[index].split()[0]) index += 1 self.n_var = int(content[index].split()[0]) index += 1 self.n_constraints = 0 self.dms = int(content[index].split()[0]) self.upper_bound = self.n_var * [0.0] self.lower_bound = self.n_var * [1.0] weight = [] for dm in range(self.dms): index += 1 line_split = clean_line(content[index]) idx = 0 w = [] while idx < self.n_obj * 2: lower = float(line_split[idx]) idx += 1 upper = float(line_split[idx].replace(',', '')) idx += 1 w.append(Interval(lower, upper)) weight.append(w) veto = [] for dm in range(self.dms): index += 1 line_split = clean_line(content[index]) idx = 0 v = [] while idx < self.n_obj * 2: lower = float(line_split[idx]) idx += 1 upper = float(line_split[idx].replace(',', '')) idx += 1 v.append(Interval(lower, upper)) veto.append(v) beta = [] for i in range(self.dms): index += 1 line_split = content[index].split() beta.append( Interval(float(line_split[0]), float(line_split[1].replace(',', '')))) lambdas = [] for i in range(self.dms): index += 1 line_split = content[index].split() lambdas.append( Interval(float(line_split[0]), float(line_split[1].replace(',', '')))) index += 1 line = clean_line(content[index]) if line[0] == 'TRUE': read_objectives = False if len(line) >= 2: read_objectives = line[1] == "TRUE" index += 1 n = int(content[index].split()[0]) best_compromise = [] for i in range(n): index += 1 line_split = content[index].split() if read_objectives: best_compromise.append([ float(line_split[i].replace(',', '')) for i in range(0, self.n_obj) ]) else: best_compromise.append([ float(line_split[i].replace(',', '')) for i in range(0, self.n_var) ]) self.attributes['best_compromise'] = best_compromise index += 1 line = clean_line(content[index]) if line[0] == "TRUE": is_interval = True if len(line) >= 2 and line[1] == "FALSE": is_interval = False r2_set = [] r1_set = [] frontiers = [] for dm in range(self.dms): index += 1 line = clean_line(content[index]) n_size = int(int(line[0]) / 2) r2 = [] r1 = [] for n_row in range(n_size): r2_ = [] index += 1 line = clean_line(content[index]) idx = 0 while idx < self.n_obj * (2 if is_interval else 1): if is_interval: r2_.append(Interval(line[idx], line[idx + 1])) idx += 2 else: r2_.append(Interval(line[idx], line[idx])) idx += 1 r2.append(r2_) for n_row in range(n_size): r1_ = [] index += 1 line = clean_line(content[index]) idx = 0 while idx < self.n_obj * (2 if is_interval else 1): if is_interval: r1_.append(Interval(line[idx], line[idx + 1])) idx += 2 else: r1_.append(Interval(line[idx], line[idx])) idx += 1 r1.append(r1_) r2_set.append(r2) r1_set.append(r1) frontiers.append(r2 + r1) self.attributes['frontiers'] = frontiers self.attributes['r2'] = r2_set self.attributes['r1'] = r1_set index += 1 if content[index].split()[0] == 'TRUE': index += 1 n = int(content[index].split()[0]) self.initial_solutions = [] for i in range(n): index += 1 line_split = content[index].split() solution = FloatSolution(lower_bound=self.lower_bound, upper_bound=self.upper_bound, number_of_objectives=self.n_obj) solution.variables = [ float(line_split[i].replace(',', '')) for i in range(0, self.n_var) ] self.initial_solutions.append(solution) self.attributes['dms'] = self.dms models = [] for dm in range(self.dms): model = OutrankingModel(weight[dm], veto[dm], 1.0, beta[dm], lambdas[dm], 1) models.append(model) self.attributes['models'] = models return self
def read_(self, absolute_path: str) -> PspIntervalInstance: with open(absolute_path) as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] index = 0 line = clean_line(content[index]) self.budget = Interval(line[0], line[1]) index += 1 line = clean_line(content[index]) self.n_obj = int(line[0]) index += 1 line = clean_line(content[index]) self.attributes['dms'] = int(line[0]) dm_type = [] for idx in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) dm_type.append(int(line[0])) index += 1 line = clean_line(content[index]) self.n_constraints = int(line.__getitem__(0)) for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) idx = 0 w = [] while idx < self.n_obj * 2: w.append(Interval(line[idx], line[idx + 1])) idx += 2 self.weights.append(w) for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) idx = 0 v = [] while idx < self.n_obj * 2: v.append(Interval(line[idx], line[idx + 1])) idx += 2 self.veto_threshold.append(v) beta = [] for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) beta.append(Interval(line[0], line[1])) chi = [] for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) chi.append(float(line[0])) alpha = [] for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) alpha.append(float(line[0])) lambdas = [] for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) lambdas.append(Interval(line[0], line[1])) models = [] for dm in range(self.attributes['dms']): models.append( OutrankingModel(self.weights[dm], self.veto_threshold[dm], alpha[dm], beta[dm], lambdas[dm], chi[dm], dm_type[dm] == 1)) self.attributes['models'] = models index += 1 line = clean_line(content[index]) self.n_var = int(line[0]) self.projects = [] for p in range(self.n_var): idx = 0 index += 1 line = clean_line(content[index]) project = [] while idx < self.n_obj * 2 + 1: project.append(Interval(line[idx], line[idx + 1])) idx += 2 self.projects.append(project) index += 1 line = clean_line(content[index]) if line[0] == "TRUE": best_compromise = [] for dm in range(self.attributes['dms']): v = '' index += 1 line = clean_line(content[index]) for idx in range(self.n_var): v += line[idx] best_compromise.append(v) self.attributes['best_compromise'] = best_compromise index += 1 line = clean_line(content[index]) if line[0] == "TRUE": notInterval = False if len(line) >= 2 and line[1] == "FALSE": notInterval = True r2_set = [] r1_set = [] frontiers = [] for dm in range(self.attributes['dms']): index += 1 line = clean_line(content[index]) n_size = int(int(line[0]) / 2) r2 = [] r1 = [] for n_row in range(n_size): r2_ = [] index += 1 line = clean_line(content[index]) idx = 0 while idx < self.n_obj * (1 if notInterval else 2): r2_.append(Interval(line[idx], line[idx + 1])) idx += 2 r2.append(r2_) for n_row in range(n_size): r1_ = [] index += 1 line = clean_line(content[index]) idx = 0 while idx < self.n_obj * (1 if notInterval else 2): r1_.append(Interval(line[idx], line[idx + 1])) idx += 2 r1.append(r1_) r2_set.append(r2) r1_set.append(r1) frontiers.append(r2 + r1) self.attributes['frontiers'] = frontiers self.attributes['r2'] = r2_set self.attributes['r1'] = r1_set return self