Constraint addition to fminconn
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I am trying to add a budget connstraint to this code but my approach is not working. the constraint would be that the summation from i= 1 to 12 of beta(i) * x(i) <= budget. budget would be a number.
my thought process was using this loop in my code,
d= 0
for i = 1: n
d = d + x(i) + (beta(i))
end
________________________________________________________________________________________________________________________________________
d would then serve as my summation. The challenge here is that I do not know how to create the relationship between d, A or b(the agument in fmincon) and my budget.
I need some directions please, and if there is a simpler and a more "matlab" way, kindly feel free to answer. Thank you
%% Optimization Code
clc; clear;
n = 12; % Number of variables
% maximum values and beta values are defined here, the are defined in a
% vector form. Both vector are same lenght as the number of variables.
% Maximum value vector:
maxVals = [5 4 0.5 10 9 15 0.5 8.5 6 3 3.5 11];
% Beta coefficients
beta = randi([1,20],1,n);
% Defining initial guess. Let's assume a zero initial guess
x0 = zeros(1,n);
% This is the objective function that needs to be minizimed for different
% values of x.
Z = @(x) (exp(-x(1))*maxVals(1) + beta(1)*x(1)) + ...
(exp(-x(2))*maxVals(2) + beta(2)*x(2)) + ...
(exp(-x(3))*maxVals(3) + beta(3)*x(3)) + ...
(exp(-x(4))*maxVals(4) + beta(4)*x(4)) + ...
(exp(-x(5))*maxVals(5) + beta(5)*x(5)) + ...
(exp(-x(6))*maxVals(6) + beta(6)*x(6)) + ...
(exp(-x(7))*maxVals(7) + beta(7)*x(7)) + ...
(exp(-x(8))*maxVals(8) + beta(8)*x(8)) + ...
(exp(-x(9))*maxVals(9) + beta(9)*x(9)) + ...
(exp(-x(10))*maxVals(10) + beta(10)*x(10)) + ...
(exp(-x(11))*maxVals(11) + beta(11)*x(11)) + ...
(exp(-x(12))*maxVals(12) + beta(12)*x(12));
% Defining lower bound, meaning that the lowest values that the function
% can take
lb = zeros(n,1);
% Applying constraints
A = ones(1,n);
b = 1000;
% Calling fmincon, the function that will calculate the variables
opts = optimoptions('fmincon','display','none');
[xMin, val] = fmincon(Z,x0,[],[],[],[],lb,[],[],opts);
% Displaying result
fprintf('The value of Z that has been minimized is %0.4f.\n',val)
fprintf('x Values are: \n')
for i = 1:n
fprintf('\t\tx_%d:\t %0.3f\n',i,xMin(i))
end
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Nonlinear Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!