필터 지우기
필터 지우기

Minimise a fitness function using GA toolbox having variable number of variables

조회 수: 1 (최근 30일)
I have an error function which I wish to minimise using GA. The error function can be a function of 1,2, 3 or many variables depending on the input given by the user. How should I write a fitness function for the GA toolbox? For example, if we have to minimise y which is a function of x, we create a separate matlab file with the command function y = abc(x) and use it in the GA toolbox. But in my case, the number of variables itself is not fixed. So how should I write such a fitness function ?

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 11일
ga() always minimizes a function of one variable -- but the variable is permitted to be a vector.
For example,
F = @(x) (sum( (-x).^(0:length(x)-1) ) - 10000).^2
nvar = input('Number of variables?');
opts = optimoptions('ga', 'PlotFcn', @gaplotbestf, 'MaxGenerations', 1800);
[best_x, fval] = ga(F, nvar, [], [], [], [], [], [], [], opts)
(2 is the most interesting number of variables for the above)
  댓글 수: 2
Rakesh Jain
Rakesh Jain 2017년 2월 11일
Getting following error on running the above code Error using optimoptions (line 114) Invalid solver specified. Provide a solver name or handle (such as 'fmincon' or @fminunc). Type DOC OPTIMOPTIONS for a list of solvers.
Error in abcd (line 3) opts = optimoptions('ga', 'PlotFcn', @gaplotbestf, 'MaxGenerations', 1800);
>>
Walter Roberson
Walter Roberson 2017년 2월 11일
You could try
opts = optimoptions(@ga, 'PlotFcn', @gaplotbestf, 'MaxGenerations', 1800);
but I suspect that you might not have the Global Optimization Toolbox installed (or licensed perhaps) and so do not have ga. Try
which ga
to see.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by