Beispiel #1
0
    def _update(self, improvement_char, dimension=None, value=None):
        '''calculate the next value from the model and update state as
        necessary'''
        # check for invalid input
        if value is not None and dimension is None:
            err = 'cannot call _update with specified value but no dimension'
            raise ValueError(err)

        if dimension is None:
            dimension = base.random_index(self._current.xs)

        if value is None:
            # get random value if no value input
            value = self.model.xs[dimension]()

        updated = False
        while not updated:
            new_xs = tuple_replace(self._current.xs, dimension, value)
            try:
                self._current = self.model(new_xs, io=True)
                updated = True
            except ModelInputException:
                value = self.model.xs[dimension]()

        self._evals += 1
        self._current_era += self._current.energy

        # compare to previous best and update as necessary
        if self._current.energy < self._best.energy:
            self._best = self._current
            self._report += improvement_char
        else:
            self._report += '.'

        # end-of-era bookkeeping
        if self._evals % self.spec.era_length == 0:
            self._end_era()
Beispiel #2
0
    def _update(self, improvement_char, dimension=None, value=None):
        '''calculate the next value from the model and update state as
        necessary'''
        # check for invalid input
        if value is not None and dimension is None:
            err = 'cannot call _update with specified value but no dimension'
            raise ValueError(err)

        if dimension is None:
            dimension = base.random_index(self._current.xs)

        if value is None:
            # get random value if no value input
            value = self.model.xs[dimension]()

        updated = False
        while not updated:
            new_xs = tuple_replace(self._current.xs, dimension, value)
            try:
                self._current = self.model(new_xs, io=True)
                updated = True
            except ModelInputException:
                value = self.model.xs[dimension]()

        self._evals += 1
        self._current_era += self._current.energy

        # compare to previous best and update as necessary
        if self._current.energy < self._best.energy:
            self._best = self._current
            self._report += improvement_char
        else:
            self._report += '.'

        # end-of-era bookkeeping
        if self._evals % self.spec.era_length == 0:
            self._end_era()
Beispiel #3
0
 def _mutate(self, child):
     i = base.random_index(child)
     return base.tuple_replace(child, i, self.model.xs[i]())