Last updated: 2023-03-16.
tf_quant_finance.math.optimizer.differential_evolution_minimize#
Applies the Differential evolution algorithm to minimize a function.
tf_quant_finance.math.optimizer.differential_evolution_minimize(
objective_function, initial_population=None, initial_position=None,
population_size=50, population_stddev=1.0, max_iterations=100, func_tolerance=0,
position_tolerance=1e-08, differential_weight=0.5, crossover_prob=0.9,
seed=None, name=None
)
Differential Evolution is an evolutionary optimization algorithm which works
on a set of candidate solutions called the population. It iteratively
improves the population by applying genetic operators of mutation and
recombination. The objective function f supplies the fitness of each
candidate. A candidate s_1 is considered better than s_2 if
f(s_1) < f(s_2).
This method allows the user to either specify an initial population or a single candidate solution. If a single solution is specified, a population of the specified size is initialized by adding independent normal noise to the candidate solution.
The implementation also supports a multi-part specification of the state. For example, consider the objective function:
# x is a tensor of shape [n, m] while y is of shape [n].
def objective(x, y):
return tf.math.reduce_sum(x ** 2, axis=-1) + y ** 2
The state in this case is specified by two input tensors x and y. To
apply the algorithm to this objective function, one would need to specify
either an initial population as a list of two tensors of shapes
[population_size, k] and [population_size]. The following code shows the
complete example:
population_size = 40
# With an initial population and a multi-part state.
initial_population = (tf.random.normal([population_size]),
tf.random.normal([population_size]))
def easom_fn(x, y):
return -(tf.math.cos(x) * tf.math.cos(y) *
tf.math.exp(-(x-np.pi)**2 - (y-np.pi)**2))
optim_results = tfp.optimizer.differential_evolution_minimize(
easom_fn,
initial_population=initial_population,
seed=43210)
print(optim_results.converged)
print(optim_results.position) # Should be (close to) [pi, pi].
print(optim_results.objective_value) # Should be -1.
# With a single starting point
initial_position = (tf.constant(1.0), tf.constant(1.0))
optim_results = tfp.optimizer.differential_evolution_minimize(
easom_fn,
initial_position=initial_position,
population_size=40,
population_stddev=2.0,
seed=43210)
Args:#
objective_function: A Python callable that accepts a batch of possible solutions and returns the values of the objective function at those arguments as a rank 1 realTensor. This specifies the function to be minimized. The input to this callable may be either a singleTensoror a PythonlistofTensors. The signature must match the format of the argumentpopulation. (i.e. objective_function(*population) must return the value of the function to be minimized).initial_population: A realTensoror Python list ofTensors. If a list, eachTensormust be of rank at least 1 and with a common first dimension. The first dimension indexes into the candidate solutions while the rest of the dimensions (if any) index into an individual solution. The size of the population must be at least 4. This is a requirement of the DE algorithm.initial_position: A realTensorof any shape. The seed solution used to initialize the population of solutions. If this parameter is specified theninitial_populationmust not be specified.population_size: A positive scalar int32Tensorgreater than 4. The size of the population to evolve. This parameter is ignored ifinitial_populationis specified. Default value: 50.population_stddev: A positive scalar realTensorof the same dtype asinitial_position. This parameter is ignored ifinitial_populationis specified. Used to generate the population from theinitial_positionby adding random normal noise with zero mean and the specified standard deviation. Default value: 1.0max_iterations: Positive scalar int32Tensor. The maximum number of generations to evolve the population for. Default value: 100func_tolerance: ScalarTensorof the same dtype as the output of theobjective_function. The algorithm stops if the absolute difference between the largest and the smallest objective function value in the population is below this number. Default value: 0position_tolerance: ScalarTensorof the same real dtype asinitial_positionorinitial_population. The algorithm terminates if the largest absolute difference between the coordinates of the population members is below this threshold. Default value: 1e-8differential_weight: Real scalarTensor. Must be positive and less than 2.0. The parameter controlling the strength of mutation in the algorithm. Default value: 0.5crossover_prob: Real scalarTensor. Must be between 0 and 1. The probability of recombination per site. Default value: 0.9seed:intor None. The random seed for thisOp. IfNone, no seed is applied. Default value: None.name: (Optional) Python str. The name prefixed to the ops created by this function. If not supplied, the default name ‘differential_evolution_minimize’ is used. Default value: None
Returns:#
optimizer_results: An object containing the following attributes: converged: Scalar booleanTensorindicating whether the minimum was found within the specified tolerances. num_objective_evaluations: The total number of objective evaluations performed. position: ATensorcontaining the best point found during the search. If the search converged, then this value is the argmin of the objective function within the specified tolerances. objective_value: ATensorcontaining the value of the objective function at theposition. If the search converged, then this is the (local) minimum of the objective function. final_population: The final state of the population. final_objective_values: The objective function evaluated at the final population. initial_population: The starting population. initial_objective_values: The objective function evaluated at the initial population. num_iterations: The number of iterations of the main algorithm body.
Raises:#
ValueError: If neither the initial population, nor the initial position are specified or if both are specified.