示例#1
0
def load_objectives_from_gdm_file(p: DTLZ1P, obj_path: str):
    with open(obj_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]
    _bag = []
    for element in content:
        line = clean_line(element)
        solution_ = p.generate_solution()
        solution_.objectives = [float(_var) for _var in line[:p.number_of_objectives]]
        _bag.append(solution_)
        print(solution_.objectives)
    print(len(_bag))
    preference = ITHDMPreferences(p.objectives_type, p.instance_.attributes['models'][0])
    best_compromise = p.generate_existing_solution(p.instance_.attributes['best_compromise'][0], True)

    max_net_score = 0

    # Make ROI
    print('best compromise', best_compromise.objectives)
    for x in _bag:
        preference.compare(x, best_compromise)
        x.attributes['net_score'] = preference.sigmaXY
        if max_net_score < preference.sigmaXY:
            max_net_score = preference.sigmaXY
            best_compromise = x

    roi = list(filter(lambda x: x.attributes['net_score'] >= preference.preference_model.beta, _bag))
    print('Best compromise')
    print(best_compromise.objectives, best_compromise.attributes['net_score'])
    print('ROI', len(roi))
    for x in roi:
        print(x.objectives, x.attributes['net_score'])
示例#2
0
 def _is_high_dis(self, x: Solution, dm: int) -> bool:
     """
         The DM is strongly dissatisfied with x if for each w in R1 we have wP(Beta, Lambda)x.
     """
     preferences = ITHDMPreferences(self.problem.objectives_type,
                                    self.problem.get_preference_model(dm))
     r1 = self.problem.instance_.attributes['r1'][dm]
     for idx in range(len(r1)):
         self.w.objectives = r1[idx]
         if preferences.compare(self.w, x) > -1:
             return False
     return True
示例#3
0
 def _is_high_sat(self, x: Solution, dm: int) -> bool:
     """
        The DM is highly satisfied with a satisfactory x if for each w in R2 we have xPr(Beta,Lambda)w
     """
     preferences = ITHDMPreferences(self.problem.objectives_type,
                                    self.problem.get_preference_model(dm))
     r2 = self.problem.instance_.attributes['r2'][dm]
     for idx in range(len(r2)):
         self.w.objectives = r2[idx]
         if preferences.compare(x, self.w) > -1:
             return False
     return True
示例#4
0
 def __init__(self,
              problem: GDProblem,
              sample_size=1000,
              dm: int = 0,
              k: int = 100000):
     self.problem = problem
     self.sample_size = sample_size
     self.dm = dm
     self.k = k
     self.preference = ITHDMPreferences(
         problem.objectives_type,
         problem.instance_.attributes['models'][dm])
     self.ranking = ITHDMRanking(self.preference, [-1], [-1])
示例#5
0
class BestCompromise:
    """
    Looking for a best compromise in a solution sample using the dm preferences
    """
    def __init__(self,
                 problem: GDProblem,
                 sample_size=1000,
                 dm: int = 0,
                 k: int = 100000):
        self.problem = problem
        self.sample_size = sample_size
        self.dm = dm
        self.k = k
        self.preference = ITHDMPreferences(
            problem.objectives_type,
            problem.instance_.attributes['models'][dm])
        self.ranking = ITHDMRanking(self.preference, [-1], [-1])

    def make(self) -> Tuple[Solution, List[Solution]]:
        """returns candidate solutions
        Generates a sample of feasible solutions and compares them looking for an xPy
        or xSy relationship, finally orders the candidate solutions by crowding distance
        """
        bag = []
        while len(bag) < self.k:
            print('Check xPy inner :', len(bag))
            sample = [
                self.problem.generate_solution()
                for _ in range(self.sample_size)
            ]
            candidates = self.ranking.compute_ranking(sample)
            if len(candidates) != 0:
                bag += candidates[0]
        print('Candidates size: ', len(bag))
        max_net_score = 0
        best_compromise = None
        for s in bag:
            if max_net_score < s.attributes['net_score']:
                max_net_score = s.attributes['net_score']
                best_compromise = s
        bag.remove(best_compromise)
        # Make ROI
        for x in bag:
            self.preference.compare(x, best_compromise)
            x.attributes['net_score'] = self.preference.sigmaXY

        roi = list(
            filter(
                lambda p: p.attributes['net_score'] >= self.preference.
                preference_model.beta, bag))
        return best_compromise, [best_compromise] + roi
示例#6
0
    def _desc_rule(self, x: Solution, dm: int) -> int:
        preferences = ITHDMPreferences(self.problem.objectives_type,
                                       self.problem.get_preference_model(dm))
        category = -1
        r1 = self.problem.instance_.attributes['r1'][dm]
        for idx in range(len(r1)):
            self.w.objectives = r1[idx]
            if preferences.compare(x, self.w) <= -1:
                category = idx
                break

        if r1 != -1:
            category += len(r1)
        r2 = self.problem.instance_.attributes['r2'][dm]
        for idx in range(len(r2)):
            self.w.objectives = r2[idx]
            if preferences.compare(x, self.w) <= -1:
                category = idx
                break

        return category
示例#7
0
    def _asc_rule(self, x: Solution, dm: int) -> int:
        preferences = ITHDMPreferences(self.problem.objectives_type,
                                       self.problem.get_preference_model(dm))
        r2 = self.problem.instance_.attributes['r2'][dm]
        category = -1
        for idx in range(len(r2)):
            self.w.objectives = r2[idx]
            v = preferences.compare(self.w, x)
            if v <= -1 or v == 2:
                category = idx
                break

        if category != -1:
            return category
        r1 = self.problem.instance_.attributes['r1'][dm]
        for idx in range(len(r1)):
            self.w.objectives = r1[idx]
            if preferences.compare(self.w, x) <= -1:
                category = idx
                break
        if category == -1:
            return category
        return category + len(r2)
示例#8
0
    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(']', ''))