フィルターのクリア

maximization with fmincon which of the two codes provided do the correct job

2 ビュー (過去 30 日間)
Az.Sa
Az.Sa 2023 年 6 月 18 日
コメント済み: Az.Sa 2023 年 6 月 19 日
I have the following maximization problem , solve for a_j:
s.t
k_j = 1x6, R_t= 6x1 , u = @(a) 1/(1-gamma)*a.^(1-gamma); gamma=2 ,
in the code W is 6x6 matrix , where each row correspond to k_j
code 1 : I have implemented as
clear all
W = csvread('optw.csv'); % Assuming W.csv contains a 6x6 matrix , where each row correspond to k_j
R = csvread('rt.csv'); % Assuming R.csv contains a 6x249 matrix
% Define the objective function
gamma = 2;
n = 6; % Number of variables (a_j)
u = @(x) 1/(1-gamma) * x.^(1-gamma);
obj = @(a) -sum(u(sum(a.* (W' * R), 1)));
% Set up optimization problem
Aeq = ones(1, n); % Equality constraint: sum_{j=1}^{j=6} a_j = 1
beq = 1; % Equality constraint value
lb = zeros(n, 1); % Lower bounds for variables a_j
ub = ones(n, 1); % Upper bounds for variables a_j
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'iter'); % Set additional options if needed
[a_opt, fval] = fmincon(obj, ones(n, 1) / n, [], [], Aeq, beq, lb, ub, [], options);
% Display the optimal solution and objective value
disp("Optimal a_j:");
disp(a_opt');
disp("Objective value:");
disp(-fval);
however another objective function I implemented provide different results :
W = csvread('optw.csv'); % Assuming W.csv contains a 6x6 matrix
R = csvread('rt.csv'); % Assuming R.csv contains a 6x249 matrix
% Define the objective function
gamma = 2;
n = 6; % Number of variables (a_j)
u = @(x) 1/(1-gamma) * x.^(1-gamma);
obj = @(a) -sum(u(sum(a .* (W* R), 1)));
% Set up optimization problem
Aeq = ones(1, n); % Equality constraint: sum_{j=1}^{j=6} a_j = 1
beq = 1; % Equality constraint value
lb = zeros(n, 1); % Lower bounds for variables a_j
ub = ones(n, 1); % Upper bounds for variables a_j
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'iter'); % Set additional options if needed
[a_opt, fval] = fmincon(obj, ones(n, 1) / n, [], [], Aeq, beq, lb, ub, [], options);
% Display the optimal solution and objective value
disp("Optimal a_j:");
disp(a_opt');
disp("Objective value:");
disp(-fval);
Can you please help me which objective function is doing the correct calculation based on the the problem I described former.
Thanks in advance
  4 件のコメント
Torsten
Torsten 2023 年 6 月 18 日
編集済み: Torsten 2023 年 6 月 18 日
If R_t is 6x1, the k_j must be 6x1, too, for that k_j^T*R_t can be evaluated.
Az.Sa
Az.Sa 2023 年 6 月 18 日
編集済み: Az.Sa 2023 年 6 月 18 日
R at observation t : is 6x1 , R does not depend on j
the dimention of k_j: is 1x6 ( you are correct , I editted the question by removing the tranpose sympole on k_j and make the k_j: is 1x6 )
we sum the objective function evaluated at each t and maximize the sum over the vector of m_j

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

採用された回答

Torsten
Torsten 2023 年 6 月 18 日
編集済み: Torsten 2023 年 6 月 18 日
I suggest using a function instead of a function handle.
I compared it with your implementation and random matrices for W and R. The second code gives the same result.
function value = obj(a,W,R)
value_gamma = 2;
u = @(x) 1/(1-value_gamma) * x.^(1-value_gamma);
vec = a.'*W;
value = 0;
for i = 1:size(R,2)
value = value + u(vec*R(:,i));
end
value = -value;
end
  3 件のコメント
Torsten
Torsten 2023 年 6 月 19 日
Play a little:
syms a [2,1] real
syms W [2,2] real
syms R [2,4] real
s1 = sum(a.* (W' * R))
s1 = 
size(s1)
ans = 1×2
1 4
s2 = sum(a.'* (W * R))
s2 = 
size(s2)
ans = 1×2
1 1
Az.Sa
Az.Sa 2023 年 6 月 19 日
that is so great! Thank you very much for your help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by