I have an optimization problem where i need to minimize the Total_Cost. I have the equations in AMPL format and i need to convert it to MATLAB. Can anybody help and correct my answer?
3 次查看(过去 30 天)
显示 更早的评论
This is the first equation in AMPL:
minimize Total_Cost:
sum {i in CONTROLLER} kappa_c[i] *
sum {j in LOCATION} tcn[i, j]
*tcn is a binary variable
This is after i tried convert it to MATLAB form:
% these values are given%
CONTROLLER = 9;
kappa_c = {1,2,3,4,5,6,7,8,9};
LOCATION = 9;
sum = 0;
for i = 1:CONTROLLER
sum = sum + kappa_c{i};
sum1 = 0;
for j = 1:LOCATION
sum1 = sum1 + tcn{i;j};
end
ans1 = sum1 * sum ;
end
display(ans1)
1 个评论
Walter Roberson
2017-11-15
Your AMPL definition uses j as the loop control variable twice, and uses the undefined variable i . Your translation of it into MATLAB would be for the slightly different
minimize Total_Cost:
sum {i in CONTROLLER} kappa_c[i] *
sum {j in LOCATION} tcn[i, j]
回答(1 个)
Yogananda Jeppu
2017-11-11
sum1 = sum1 + tcn{i;j}; The ';' should be perhaps a ','. It is easier if you can represent as a matrix with [] square brackets. Use the sum function in matlab and .* to multiply elements instead of matrices.
9 个评论
Walter Roberson
2017-11-15
tcn = randi([0 1], 9, 9);
If none of the entries are negative, then the tcn that minimizes will always be the all-zero tcn, which would give a cost of 0. With no negative entries it is not possible for the cost to be below 0, so all-zero minimizes.
The exception to this is if you have constraints such as "at least 2 locations must be turned on for each controller"
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!