zeeguu/core/word_scheduling/arts/tools/algo_parameter_approximator.py
def fit_parameters(self, variables_to_set, optimization_goals):
        """
        Fit the parameters of the algorithm to match optimization goals best
        :param variables_to_set: a list of algorithm_variables
         algorithm_variable: ['X', getattr(algorithm, 'X'), approximation_change]
         where:
          1. X is the variable/parameter in the algorithm
          2. The current/starting value for X in the first run
          3. approximation_change is the added approximation variable in the next run
        Notes about the approximation_change:
         The starting value gets increased by approximation_change in every run until no further improvement is made.
         Then: The new approximation_change is -(approximation_change/2) to run backward again closer to the optimal
         value until it overshoots and the approximation_change is again updated
        :param optimization_goals: An instance of OptimizationGoals
        :return: The variables_to_set where the second parameter is the found optimal value
        """
        if len(self.fancy.bookmarks) == 0:
            return None

        iteration_counter = 0
        tick_tock = 0  # ensure that when optimizing, it´s stopped the earliest after all variables have been considered

        # Init run
        result_new = self.fancy.calc_algorithm_stats(verbose=False)
        change = self.__diff_to_goal(optimization_goals, result_new)

        # Only leave optimization when the change limit is too small and
        #  the optimization was run on all parameters (tick_tock)
        while change > self.change_limit or tick_tock != 0:
            print('------------------------------------------------------------------------')
            print('Iteration {:3d} of the algorithm tickTock={}, variables={}'
                  .format(iteration_counter, tick_tock, variables_to_set))

            new_variable_value = math.fabs(variables_to_set[tick_tock][1] + variables_to_set[tick_tock][2])
            setattr(self.algorithm, variables_to_set[tick_tock][0], new_variable_value)
            print('Trying now with D={}, b={}, w={}'.format(self.algorithm.d, self.algorithm.b, self.algorithm.w))
            self.__update_algorithm_instance(self.algorithm)

            # run the algorithm
            result_new = self.fancy.calc_algorithm_stats(verbose=False)
            # difference to desired goal
            diff_to_goal = self.__diff_to_goal(optimization_goals, result_new)

            if diff_to_goal < change:
                print('Improvement found')

                # We just did better
                variables_to_set[tick_tock][1] = new_variable_value
                change = diff_to_goal
            else:
                print('No further improvement')

                # reset the variable
                setattr(self.algorithm, variables_to_set[tick_tock][0], variables_to_set[tick_tock][1])

                # Time to optimize on the other variable
                variables_to_set[tick_tock][2] *= -0.5

            tick_tock += 1
            tick_tock = divmod(tick_tock, len(variables_to_set))[1]

            iteration_counter = iteration_counter + 1
            if iteration_counter > self.max_iterations:
                print('Stopped due to max_iterations parameter')
                break

        print('')
        print('The variables should be set the following way:')
        for variable_to_set in variables_to_set:
            print('{}={}'.format(variable_to_set[0], variable_to_set[1]))

        return variables_to_set

        Fit the parameters of the algorithm to match optimization goals best
        :param variables_to_set: a list of algorithm_variables
         algorithm_variable: ['X', getattr(algorithm, 'X'), approximation_change]
         where:
          1. X is the variable/parameter in the algorithm
          2. The current/starting value for X in the first run
          3. approximation_change is the added approximation variable in the next run
        Notes about the approximation_change:
         The starting value gets increased by approximation_change in every run until no further improvement is made.
         Then: The new approximation_change is -(approximation_change/2) to run backward again closer to the optimal
         value until it overshoots and the approximation_change is again updated
        :param optimization_goals: An instance of OptimizationGoals
        :return: The variables_to_set where the second parameter is the found optimal value
        
Fit the parameters of the algorithm to match optimization goals best
Fit the parameters of the algorithm to match optimization goals best
def fit_parameters(self, variables_to_set, optimization_goals):
        if len(self.fancy.bookmarks) == 0:
            return None
        iteration_counter = 0
        tick_tock = 0  
        result_new = self.fancy.calc_algorithm_stats(verbose=False)
        change = self.__diff_to_goal(optimization_goals, result_new)
        while change > self.change_limit or tick_tock != 0:
            print('------------------------------------------------------------------------')
            print('Iteration {:3d} of the algorithm tickTock={}, variables={}'
                  .format(iteration_counter, tick_tock, variables_to_set))
            new_variable_value = math.fabs(variables_to_set[tick_tock][1] + variables_to_set[tick_tock][2])
            setattr(self.algorithm, variables_to_set[tick_tock][0], new_variable_value)
            print('Trying now with D={}, b={}, w={}'.format(self.algorithm.d, self.algorithm.b, self.algorithm.w))
            self.__update_algorithm_instance(self.algorithm)
            result_new = self.fancy.calc_algorithm_stats(verbose=False)
            diff_to_goal = self.__diff_to_goal(optimization_goals, result_new)
            if diff_to_goal < change:
                print('Improvement found')
                variables_to_set[tick_tock][1] = new_variable_value
                change = diff_to_goal
            else:
                print('No further improvement')
                setattr(self.algorithm, variables_to_set[tick_tock][0], variables_to_set[tick_tock][1])
                variables_to_set[tick_tock][2] *= -0.5
            tick_tock += 1
            tick_tock = divmod(tick_tock, len(variables_to_set))[1]
            iteration_counter = iteration_counter + 1
            if iteration_counter > self.max_iterations:
                print('Stopped due to max_iterations parameter')
                break
        print('')
        print('The variables should be set the following way:')
        for variable_to_set in variables_to_set:
            print('{}={}'.format(variable_to_set[0], variable_to_set[1]))
        return variables_to_set
def fit_parameters ( self , variables_to_set , optimization_goals ) : if len ( self . fancy . bookmarks ) == 0 : return None iteration_counter = 0 tick_tock = 0 result_new = self . fancy . calc_algorithm_stats ( verbose = False ) change = self . __diff_to_goal ( optimization_goals , result_new ) while change > self . change_limit or tick_tock != 0 : print ( '------------------------------------------------------------------------' ) print ( 'Iteration {:3d} of the algorithm tickTock={}, variables={}' . format ( iteration_counter , tick_tock , variables_to_set ) ) new_variable_value = math . fabs ( variables_to_set [ tick_tock ] [ 1 ] + variables_to_set [ tick_tock ] [ 2 ] ) setattr ( self . algorithm , variables_to_set [ tick_tock ] [ 0 ] , new_variable_value ) print ( 'Trying now with D={}, b={}, w={}' . format ( self . algorithm . d , self . algorithm . b , self . algorithm . w ) ) self . __update_algorithm_instance ( self . algorithm ) result_new = self . fancy . calc_algorithm_stats ( verbose = False ) diff_to_goal = self . __diff_to_goal ( optimization_goals , result_new ) if diff_to_goal < change : print ( 'Improvement found' ) variables_to_set [ tick_tock ] [ 1 ] = new_variable_value change = diff_to_goal else : print ( 'No further improvement' ) setattr ( self . algorithm , variables_to_set [ tick_tock ] [ 0 ] , variables_to_set [ tick_tock ] [ 1 ] ) variables_to_set [ tick_tock ] [ 2 ] *= - 0.5 tick_tock += 1 tick_tock = divmod ( tick_tock , len ( variables_to_set ) ) [ 1 ] iteration_counter = iteration_counter + 1 if iteration_counter > self . max_iterations : print ( 'Stopped due to max_iterations parameter' ) break print ( '' ) print ( 'The variables should be set the following way:' ) for variable_to_set in variables_to_set : print ( '{}={}' . format ( variable_to_set [ 0 ] , variable_to_set [ 1 ] ) ) return variables_to_set
Fit the parameters of the algorithm to match optimization goals best
Fit the parameters of the algorithm to match optimization goals best
"""
        Fit the parameters of the algorithm to match optimization goals best
        :param variables_to_set: a list of algorithm_variables
         algorithm_variable: ['X', getattr(algorithm, 'X'), approximation_change]
         where:
          1. X is the variable/parameter in the algorithm
          2. The current/starting value for X in the first run
          3. approximation_change is the added approximation variable in the next run
        Notes about the approximation_change:
         The starting value gets increased by approximation_change in every run until no further improvement is made.
         Then: The new approximation_change is -(approximation_change/2) to run backward again closer to the optimal
         value until it overshoots and the approximation_change is again updated
        :param optimization_goals: An instance of OptimizationGoals
        :return: The variables_to_set where the second parameter is the found optimal value
        """ # ensure that when optimizing, it´s stopped the earliest after all variables have been considered # Init run # Only leave optimization when the change limit is too small and #  the optimization was run on all parameters (tick_tock) # run the algorithm # difference to desired goal # We just did better # reset the variable # Time to optimize on the other variable
The variables_to_set where the second parameter is the found optimal value
The variables_to_set where the second parameter is the found optimal value
self
variables_to_set
a list of algorithm_variables
algorithm_variable: ['X', getattr(algorithm, 'X'), approximation_change]
where:
1. X is the variable/parameter in the algorithm
2. The current/starting value for X in the first run
3. approximation_change is the added approximation variable in the next run
Notes about the approximation_change:
The starting value gets increased by approximation_change in every run until no further improvement is made.
Then: The new approximation_change is -(approximation_change/2) to run backward again closer to the optimal
value until it overshoots and the approximation_change is again updated
a list of algorithm_variables algorithm_variable : [ ' X ' getattr ( algorithm ' X ' ) approximation_change ] where : 1 . X is the variable / parameter in the algorithm 2 . The current / starting value for X in the first run 3 . approximation_change is the added approximation variable in the next run Notes about the approximation_change : The starting value gets increased by approximation_change in every run until no further improvement is made . Then : The new approximation_change is - ( approximation_change / 2 ) to run backward again closer to the optimal value until it overshoots and the approximation_change is again updated
optimization_goals
An instance of OptimizationGoals
An instance of OptimizationGoals