コード例 #1
0
    def _cm2(self, scenario1, scenario2, score_table):
        scenario = self._and_solution(scenario1, scenario2)
        solution = scenario.get_solution()

        safe_solution = self._make_valid(deepcopy(solution))

        # loop through the true vars and try to turn some of them to false
        valid = True
        while valid:
            solution = deepcopy(safe_solution)
            candidates = [x for x in solution if x['value']]

            entry = random.choice(candidates)
            entry['value'] = False

            test_scenario = from_solution(solution, self._data)
            valid = test_scenario.valid(process=self._process)
            if valid:
                # save the solution if it is still viable
                safe_solution = deepcopy(solution)

        final_solution = from_solution(safe_solution, self._data)

        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution
コード例 #2
0
    def _cm1(self, scenario1, scenario2, score_table):
        scenario = self._and_solution(scenario1, scenario2)
        solution = scenario.get_solution()

        safe_solution = self._make_valid(deepcopy(solution))

        # loop through the true vars and try to turn some of them to false
        for entry in [x for x in solution if x['value']]:
            t = next(x for x in entry.keys() if x != 'value')
            match = next(x for x in score_table
                         if x['type'] == t and
                         x['name'] == entry[t] and
                         x['value'] == entry['value'])
            prob = 1 - match['score']

            if random.random() <= prob:
                entry['value'] = False
                test_scenario = from_solution(solution, self._data)
                if test_scenario.valid(process=self._process):
                    # save the solution if it is still viable
                    safe_solution = deepcopy(solution)
                else:
                    break

        final_solution = from_solution(safe_solution, self._data)

        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution
コード例 #3
0
    def _make_valid(self, solution):
        max_iterations = 50
        i = 0
        while not from_solution(solution,
                                self._data).valid(process=self._process):
            # handle max iteration stuff
            if i >= max_iterations:
                raise NoValidSolutionException()

            if i == 25:
                logging.info("Solution taking a while to validate")

            i += 1

            months = len([x for x in solution
                         if 'month' in x.keys() and x['value']])
            variables = len([x for x in solution
                             if 'variable' in x.keys() and x['value']])
            changed = False
            if months < 3:
                solution = self._flip_random(solution, 'month', False)
                changed = True

            if variables < 3:
                solution = self._flip_random(solution, 'variable', False)
                changed = True

            if not changed:
                new_solution = deepcopy(solution)
                candidate_months = [x for x in new_solution
                                    if 'month' in x.keys()
                                    and not x['value']]

                # check if switching any particular month makes the
                # solution viable
                for month in candidate_months:
                    month['value'] = True
                    if from_solution(new_solution,
                                     self._data).valid(process=self._process):
                        changed = True
                    else:
                        month['value'] = False

                # just pick one and go around the loop again
                if not changed and len(candidate_months) > 0:
                    month = random.choice(candidate_months)
                    month['value'] = True
                else:
                    # no candidate months - definitely try to change
                    # a variable to True
                    solution = self._flip_random(solution, 'variable', False)
                    changed = True

                solution = new_solution

        return solution
コード例 #4
0
    def load_file(self, fname, data):
        rows = CSVFileReader(fname).get_content()
        scenarios = []
        for row in rows:
            scenario = []
            keys = [
                x for x in row.keys() if x not in ['utility', 'rmse', 'cost']
            ]
            for key in keys:
                entry = dict()
                if key in months:
                    entry['month'] = key
                    entry['value'] = (row[key] == "True")
                else:
                    entry['variable'] = key
                    entry['value'] = (row[key] == "True")

                scenario.append(entry)

            final_scenario = from_solution(scenario, data)
            final_scenario.set_rmse(row['rmse'])
            util = float(row['utility'])

            final_scenario.set_utility(util)
            scenarios.append(final_scenario)

        self._population = scenarios
コード例 #5
0
    def _and_solution(self, scenario1, scenario2):
        new_solution = []
        for var1, var2 in zip(scenario1.get_solution(),
                              scenario2.get_solution()):
            new_var = deepcopy(var1)
            new_var['value'] = var1['value'] and var2['value']
            new_solution.append(new_var)

        return from_solution(new_solution, self._data)
コード例 #6
0
    def _cm5(self, scenario1, scenario2, score_table):
        new_solution = self._and_solution(scenario1, scenario2).get_solution()

        for var in [x for x in new_solution if not x['value']]:
            if random.random() < 0.5:
                var['value'] = True

        new_solution = self._make_valid(new_solution)
        final_solution = from_solution(new_solution, self._data)
        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution
コード例 #7
0
    def _cm6(self, scenario1, scenario2, score_table):
        new_scenario = self._build_g_solution(score_table, False)
        new_solution = new_scenario.get_solution()

        for new_var, var1, var2 in zip(new_solution,
                                       scenario1.get_solution(),
                                       scenario2.get_solution()):
            if not (var1['value'] or var2['value']):
                new_var['value'] = False

        new_solution = self._make_valid(new_solution)
        final_solution = from_solution(new_solution, self._data)
        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution
コード例 #8
0
    def _cm4(self, scenario1, scenario2, score_table):
        new_solution = self._and_solution(scenario1, scenario2).get_solution()

        for var in [x for x in new_solution if not x['value']]:
            index = new_solution.index(var)
            var1 = scenario1.get_solution()[index]
            var2 = scenario2.get_solution()[index]

            weight = self._var_weight(scenario1, scenario2, var1, var2)

            if random.random() < weight:
                var['value'] = True

        new_solution = self._make_valid(new_solution)
        final_solution = from_solution(new_solution, self._data)
        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution
コード例 #9
0
    def _cm3(self, scenario1, scenario2, score_table):
        new_solution = []

        for var1, var2 in zip(scenario1.get_solution(),
                              scenario2.get_solution()):
            entry = dict()
            t = next(x for x in var1.keys() if x != 'value')
            entry[t] = var1[t]

            weight = self._var_weight(scenario1, scenario2, var1, var2)

            if random.random() <= weight:
                entry['value'] = True
            else:
                entry['value'] = False

            new_solution.append(entry)

        new_solution = self._make_valid(new_solution)
        final_solution = from_solution(new_solution, self._data)
        if not final_solution.valid(process=self._process):
            raise Exception("Solution not valid")

        return final_solution