Genetic Algorithm with array as decision variable

9 ビュー (過去 30 日間)
Scott Lowrie
Scott Lowrie 2016 年 8 月 15 日
コメント済み: Scott Lowrie 2016 年 8 月 15 日
So I have this code. All I want to occur is the ga tool to change the elements of the x vector which starts out as [0,0,0,0,0] by changing each element to a one or leaving it a zero. When I run solver.m, it returns the same z value over and over so ga is not changing the decision variables like I want it to. I have been stuck on this and I cannot figure out what the issue is, I'm sure I'm just missing something small. Any help would be appreciated, thanks!
main_function.m
function z = main_function(x)
n = 5;
x = zeros(1,n);
%very long code that returns rank deficiency
z = rank_deficiency;
end
solver.m
clc
clear
FitnessFunction = @main_function;
n = 5;
LB = zeros(1,n);
UB = ones(1,n);
integers = linspace(1,n,n);
[x,fval] = ga(FitnessFunction,n,[],[],[],[],LB,UB,[],ints,[]);

採用された回答

Alan Weiss
Alan Weiss 2016 年 8 月 15 日
I see at least two errors in your code:
  1. You set x = zeros(1,n) within main_function. Don't do that! Allow your function to use the x that ga passes in.
  2. You set integers = 1:n but then you pass an argument called ints
Alan Weiss
MATLAB mathematical toolbox documentation
  1 件のコメント
Scott Lowrie
Scott Lowrie 2016 年 8 月 15 日
Yes, it works finally. Thanks Alan.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by