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?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Walter Roberson
el 15 de Nov. de 2017
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]
Respuestas (1)
Yogananda Jeppu
el 11 de Nov. de 2017
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 comentarios
Walter Roberson
el 15 de Nov. de 2017
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"
Ver también
Categorías
Más información sobre Multivariate Models en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!