[MATLAB] using genetic algorithm function to find the optimal solution of objective function

Source: Internet
Author: User

Recent exposure to genetic algorithms and the use of genetic algorithms to find the optimal solution, so the relevant contents of the collation of records.

Introduction to Genetic algorithms (excerpt from Wikipedia)

Genetic Algorithm (English: Genetic Algorithm (GA)) is a search algorithm used in computational mathematics to solve the optimization, which is a kind of evolutionary algorithm. Evolutionary algorithms were originally developed from some phenomena in evolutionary biology, including heredity, mutation, natural selection, and hybridization.

Algorithm
    • Select the initial Life population
    • Cycle
      • Assessing individual fitness in a population
      • The principle of proportionality (higher probability of picking in high scores) is chosen to produce the next population.
      • Change the population (crossover and mutation)
    • Until the condition of the stop loop satisfies
Applicable issues

Genetic algorithm is good at solving the problem is the global optimization problem.
Compared with the traditional mountain climbing algorithm, the genetic algorithm can get out of the local optimal and find the most advantage. Moreover, genetic algorithms allow the use of very complex fitness functions (or target functions) and limit the range of variables to be changed.

Ii. ga function in matlab
    1. X = GA (FITNESSFCN, Nvars)
      This is the simplest invocation method of GA function, where FITNESSFCN is the objective function, its parameters should be constant, and nvars is the dimension of the parameter vector.
      X is the parameter vector with the value of the target function being the hour.
    2. X = GA (FITNESSFCN, Nvars, A, B, Aeq, beq, LB, ub, Nonlcon, Options)
      These parameters are used to constrain x:
      • Ax <= B, aeqx = Beq (linear constraint)
      • LB <= X <= UB
      • Nonlcon: Definition C (x) <= 0, Ceq (x) = 0 (nonlinear constraint)
      • Options: Setting Parameters for GA
    3. [X,fval,exitflag,output] = GA (FITNESSFCN, ...)
      • FVal is the value when the parameter of the target function is X
      • Exitflag is the sign of ending genetic algorithm calculation
        -0 Maximum number of generations exceeded.
        -1 optimization terminated by the output or plot function.
        -2 No feasible point found.
        -4 Stall time limit exceeded.
        -5 time limit exceeded.
      • The output structure contains information such as the genetic algebra, the outputs of the population, etc.
    4. Options
      options = Gaoptimset (); options. generations=5000; % Iteration count options. populationsize=30; % of population
      There are many other options that you can set.
A city instances

Suppose the objective function is required
f = (339-0.01*x1-0.003*x2)*x1 + (399-0.004*x1-0.01*x2)*x2 - (400000+195*x1+225*x2);
The maximum value.

First write the objective function myfit.m :

function f = myfit (x)    f = (339-0.01*x (1) -0.003*x (2)) *x (1)        ... + (399-0.004*x (1) -0.01*x (2)) *x (2)        ... -(400000+195*x (1) +225*x (2));    f =-F; % because GA is looking for the minimum value, so in order to find the maximum value of this function, take the inverse number of f

Call the GA function:

X =ga (@myfit, 2)

The results show:Optimization terminated: maximum number of generations exceeded.
It is indicated that the maximum number of iterations has not yet obtained the optimal solution. So the following options increase the number of iterations:

options = Gaoptimset (); options. Generations = 2000; The maximum number of iterations is set to 2000 to call the GA function again [x,fval,exitflag,output] =ga (@myfit, 2, [], [],[],[],[],[],[],options);

The results show:Optimization terminated: average change in the fitness value less than options.TolFun.

At this time

X = 1.0e+03 *    4.7350 7.0429 fval = -5.5364e+05

This result is consistent with the results of the X1 and X2 of the objective function (x1=4735, x2=7043, y=553641), which indicates that the results are correct.

Iv. other methods for finding the optimal solution

MATLAB also has many other commonly used functions to find the best solution, such as fmincon (), Fminsearch (), Fminimax () and so on.

[MATLAB] using genetic algorithm function to find the optimal solution of objective function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.