how do i write a constraint and the sum of the z value?
Show older comments
i have the code and i try to make x as constraint but when i run i always get an error said that Undefined function or variable 'x'. below is the code and how can i get the sum of the z value?
clc;
clear;
%sum sum sum sum(fik*djq*xij*xkq)
%i,k= facilities
%j,q= location
%f(i,k)= flow between facilities i and k
%d(j,q)= distance between locations j and q
%xij = 1 if facility i is assigned to location j and if otherwise, xij = 0
% Flow matrix: flow assigning facility i (column) to facility k (row)
f = [0 5 7 9;
5 0 4 6;
7 4 0 3;
9 6 3 0];
%Distance matrix: distance assigning location j (column) to location q (row)
d = [0 6 8 9;
6 0 5 1;
8 5 0 2;
9 1 2 0];
z= 0;
nf= 4;%no of facilities
nd= 4;%no of locations
for i=1:nf
for j=1:nf
for k=1:nd
for q=1:nd
z = min('z','sum(sum(f(i,k)*d(j,q)*x(i,j)*x(k,q)))');
%if x(i,j)==1;
%else x(k,q)==0;
end
end
end
end
%Constraints %x as binary 0 1, %x(i,j) = 1 if facility i is assigned to location j 0r otherwise, x(i,j) = 0
Constraints.constr1 = (x(i,j))==1;
%The first set of constraints requires that each facility gets exactly one %location, that is for each facility, the sum of the location values %corresponding to that facility is exactly one
Constraints.constr2 = sum(x,2) == 1;
%The second set of constraints are inequalities. These constraints specify %that each office has no more than one facility in it.
Constraints.constr3 = sum(x,1) == 1;
disp (z);
are the constraint i code is wrong? and how can i get the sum of z value?
6 Comments
Image Analyst
on 9 Jun 2018
Why do you think that x should be anything when you never assigned anything to it before you tried to use it? It doesn't just magically have some value.
John D'Errico
on 9 Jun 2018
Are you hoping that somehow, MATLAB will know that you are apparently wanting to do some sort of optimization here? MATLAB cannot read your mind.
I would guess that you want to do some optimization, but for that to happen, you would need to use an optimization tool, perhaps linprog might be right. You would also need to define an objective.
And, no matter what, this has NO chance of doing what you think it will do:
min('z','sum(sum(f(i,k)*d(j,q)*x(i,j)*x(k,q)))')
So it looks like you need to first learn MATLAB, then learn how to formulate this as an optimization for a proper solver.
sharifah shuthairah syed abdullah
on 9 Jun 2018
Walter Roberson
on 9 Jun 2018
Constraints are inputs, not outputs. If you want your x matrix to be a constraint, then you need to know it ahead of time, or at least randomly generate it before you do your optimization.
sharifah shuthairah syed abdullah
on 10 Jun 2018
Walter Roberson
on 10 Jun 2018
If you tried to do that before your for i loop, then i would not have been defined, and would have the default value of sqrt(-1)
Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!