optimizing iterated objective function - containing ∑- sum operator

3 ビュー (過去 30 日間)
Wycliff Dembe
Wycliff Dembe 2018 年 3 月 3 日
I am writing MATLAB code to minimize objective function f(x,y) - please see attached image. P and Q are readily available (known) arrays of equal size n. My major challenge is on how to deal with P and Q to formulate the objective function f(x,y) for input to the optimization algorithm (genetic algorithm). I tried using a loop to iterate through all P and Q before the optimization process but i could not understand the resulting objective function and how to apply it in the optimization algorithm. Same challenge exists with the first constraint, dealing with P and Q. Is there any possible way i can resolve this? (n,a,b,R,K and L are all known constants). Thanks in advance.

採用された回答

Wycliff Dembe
Wycliff Dembe 2018 年 8 月 23 日
I later figured it out. I had to apply loops and function handles and it worked.
  1 件のコメント
Kedar Karpe
Kedar Karpe 2019 年 2 月 4 日
Please share your solution. Thanks!

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

その他の回答 (2 件)

Wycliff Dembe
Wycliff Dembe 2019 年 4 月 9 日
編集済み: Wycliff Dembe 2019 年 10 月 24 日
Define a custom objective function as:
function f_sum = objectiveFun(inParam)
global N p q
x = inParam(1);
y = inParam(2);
for i=1:N
f_xy(i) = sqrt((x-p(i))^2+(y-q(i))^2);
end
f_sum = sum(f_xy);
end
Define a custom constraint function as:
function [ c, ceq ] = constraintsFun(inParam)
global R
a = rand; b = rand;
x = inParam(1);
y = inParam(2);
%inequality constraint. please note I didn't use the first constraint during implementation so I haven't written it here
c = (x-a)^2 + (y-b)^2 - R;
%equality constraint
ceq = []; % none
end
Invoke the matlab built-in ga function as:
clc
clear
global N p q R
R = randi(5);
N = randi(20);
p = rand(1,N);
q = rand(1,N);
nvars = 2; % x and y
min_bound = [-rand -rand]; % -K,-L
max_bound = [rand rand]; % K,L
[optim_x_y,f_min,exitflag] = ga(@objectiveFun,nvars,[],[],[],[],min_bound,max_bound,@constraintsFun)
Anything you might want to customise in ga can be passed via options using gaoptimset
  3 件のコメント
Wycliff Dembe
Wycliff Dembe 2019 年 10 月 24 日
The dimensions of the input parameter (inParam) are defined in nvars.
With nvars gives as 2 (because we have x and y), we define the first dimension of inParam as x and the second dimension of inParam as y. The two can be accessed as:
x = inParam(1)
y = inParam(2)
Shahrooz Pouryousef
Shahrooz Pouryousef 2022 年 5 月 11 日
Hi Wycliff, I need your help. I have almost the same objective function. But I have sum over three different sets in my objective function and also for each variable I need to call a function. Would you please help me on this? I can explain more if you are here.

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


Hanne Vanduffel
Hanne Vanduffel 2019 年 4 月 9 日
Could you please share your code?
Thanks in advance!

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by