예제 #1
0
    def load(self):
        """
        Load backup lines as population and set algorithm state (population and pareto front) at this backup
        """
        if os.path.exists(self._filepath):

            logging.info('Load best solution from last checkpoint')
            with open(self._filepath) as f:

                # read data for each line
                for i, line in enumerate(f.readlines()):

                    data = line.replace(';\n', '').split(';')

                    nObjectives = len(self.algo.evaluator)
                    scores = [float(s) for s in data[0:nObjectives]]

                    # get best solution data information
                    current_data = list(map(int, data[-1].split(' ')))

                    self.algo.result[i].data = np.array(current_data)
                    self.algo.result[i].scores = scores

            macop_text(
                self.algo,
                f'Load of available pareto front backup from `{ self._filepath}`'
            )
        else:
            macop_text(
                self.algo,
                'No pareto front found... Start running algorithm with new pareto front population.'
            )
            logging.info("No pareto front backup used...")

        macop_line(self.algo)
예제 #2
0
    def load(self):
        """
        Load backup lines as rewards and occurrences for UCB
        """
        if os.path.exists(self._filepath):

            logging.info('Load UCB data')
            with open(self._filepath) as f:

                lines = f.readlines()
                # read data for each line
                rewardsLine = lines[0].replace('\n', '')
                occurrencesLine = lines[1].replace('\n', '')

                self.algo.policy.rewards = [
                    float(f) for f in rewardsLine.split(';')
                ]
                self.algo.policy.occurences = [
                    float(f) for f in occurrencesLine.split(';')
                ]

            macop_text(
                self.algo,
                f'Load of available UCB policy data from `{self._filepath}`')
        else:
            macop_text(self.algo, 'No UCB data found, use default UCB policy')
            logging.info("No UCB data found...")

        macop_line(self.algo)
예제 #3
0
파일: base.py 프로젝트: stsievert/macop
    def end(self):
        """Display end message into `run` method
        """

        macop_text(
            self,
            f'({type(self).__name__}) Found after {self._numberOfEvaluations} evaluations \n   - {self._bestSolution}'
        )
        macop_line(self)
예제 #4
0
    def load(self):
        """
        Load backup lines as population and set algorithm state (population and pareto front) at this backup
        """
        if os.path.exists(self._filepath):

            logging.info('Load best solution from last checkpoint')
            with open(self._filepath) as f:

                # read data for each line
                for i, line in enumerate(f.readlines()):

                    data = line.replace(';\n', '').split(';')

                    # only the first time
                    if i == 0:
                        # get evaluation  information
                        globalEvaluation = int(data[0])

                        if self.algo.getParent() is not None:
                            self.algo.getParent(
                            )._numberOfEvaluations = globalEvaluation
                        else:
                            self.algo._numberOfEvaluations = globalEvaluation

                    nObjectives = len(self.algo.evaluator)
                    scores = [float(s) for s in data[1:nObjectives + 1]]

                    # get best solution data information
                    current_data = list(map(int, data[-1].split(' ')))

                    # initialise and fill with data
                    self.algo.population[i] = self.algo.initialiser()
                    self.algo.population[i].data = np.array(current_data)
                    self.algo.population[i].scores = scores

                    self.algo.result.append(self.algo.population[i])

            macop_line(self.algo)
            macop_text(
                self.algo,
                f'Load of available population from `{self._filepath}`')
            macop_text(
                self.algo,
                f'Restart algorithm from evaluation {self.algo._numberOfEvaluations}.'
            )
        else:
            macop_text(
                self.algo,
                'No backup found... Start running algorithm from evaluation 0.'
            )
            logging.info(
                "Can't load backup... Backup filepath not valid in Checkpoint")

        macop_line(self.algo)
예제 #5
0
    def end(self):
        """Display end message into `run` method
        """

        macop_text(
            self,
            f'({type(self).__name__}) Found after {self._numberOfEvaluations} evaluations'
        )

        for i, solution in enumerate(self._pfPop):
            macop_text(self, f'  - [{i}] {solution.scores} : {solution}')

        macop_line(self)
예제 #6
0
    def load(self):
        """
        Load last backup line of solution and set algorithm state (best solution and evaluations) at this backup
        """
        if os.path.exists(self._filepath):

            logging.info('Load best solution from last checkpoint')
            with open(self._filepath) as f:

                # get last line and read data
                lastline = f.readlines()[-1]
                data = lastline.split(';')

                # get evaluation  information
                globalEvaluation = int(data[0])

                if self.algo.getParent() is not None:
                    self.algo.getParent().setEvaluation(globalEvaluation)
                else:
                    self.algo.setEvaluation(globalEvaluation)

                # get best solution data information
                solution_data = list(map(int, data[1].split(' ')))

                if self.algo.result is None:
                    self.algo.result = self.algo.initialiser()

                self.algo.result.data = np.array(solution_data)
                self.algo.result.fitness = float(data[2])

            macop_line(self.algo)
            macop_text(self.algo,
                       f'Checkpoint found from `{self._filepath}` file.')
            macop_text(
                self.algo,
                f'Restart algorithm from evaluation {self.algo.getEvaluation()}.'
            )
        else:
            macop_text(
                self.algo,
                'No backup found... Start running algorithm from evaluation 0.'
            )
            logging.info(
                "Can't load backup... Backup filepath not valid in Checkpoint")

        macop_line(self.algo)