I am trying to solve below network problem in GAMS Cplex. I am unable to get the desired output as the GAMS is giving the arcs which doesnt exist as output. Could you please help me in correcting this.
Program:
Set
i supply nodes /1,2,3,4/;
Alias(i,j);
Set
arc(i,j) arcs from node i to j
/1 .2
2 .3
3 .4
1 .4
2 .4/;
Parameter b(i) number of units available or required at node i
/ 1 5
2 2
3 -4
4 -3/ ;
Table c(i,j) cost of shipping from node i to node j
1 2 3 4
1 0 3 0 1
2 0 0 6 5
3 0 0 0 0
4 0 0 2 0 ;
Positive variables
x(i,j) number of units shipped along arc from i to j;
Variable z;
Equations obj, cons(i);
obj.. z =E= sum(arc(i,j),c(arc)*x(arc));
cons(i).. sum(j,x(i,j)) - sum(j,x(j,i)) =E= b(i);
I think you need to use the set "arc" in your constraints "cons" like this:
cons(i).. sum(arc(i,j),x(i,j)) - sum(arc(j,i),x(j,i)) =E= b(i);
Hope that helps,
Lutz
Related
I'm trying to solve the network problem below in GAMS Cplex. I have a piecewise constraint that depend on the node situations (whether a node is an origin (o) node, in between node, and destination (d) node).
How do I write these piecewise constraints? Or is there any way to write this 'manually' for the equation 1 to 5?
In the program below, I've written:
eq1 represents node 1 as an origin node,
eq2 eq3 eq4 represents node 2,3, and 4 as in between nodes,
eq5 represents node 5 as a destination node.
Set
i nodes /1,2,3,4,5/;
Alias(i,j);
Set
arc(i,j) arcs from node i to j
/1 .2
2 .1
1 .3
3 .1
1 .4
4 .1
2 .3
3 .2
2 .5
5 .2
3 .5
5 .3
4 .5
5 .4/;
Table c(i,j) population exposed from node i to node j
1 2 3 4 5
1 0 105000 90000 65000 0
2 105000 0 100000 0 85000
3 90000 100000 0 0 80000
4 65000 0 0 0 55000
5 0 85000 80000 55000 0
;
Table l(i,j) distance from node i to node j
1 2 3 4 5
1 0 5 8 10 0
2 5 0 2 0 7
3 8 2 0 0 11
4 10 0 0 0 8
5 0 7 11 8 0
Binary Variables
x(i,j)
y(i,j);
Positive Variables
v(i,j)
lambda(i,j);
Free Variables
w(i) node i
w(j) node j
z optimization solution;
Scalar
R very large number;
R = 10000000000000000;
Equations
sol optimization solution
eq1(i,j) constraint 1
eq2(i,j) constraint 2
eq3(i,j) constraint 3
eq4(i,j) constraint 4
eq5(i,j) constraint 5
eq6(i,j) constraint 6
eq7(i,j) constraint 7
eq8(i,j) constraint 8
eq9(i,j) constraint 9;
sol.. z =e= sum(arc(i,j),c(arc)*x(arc));
eq1(i,j).. x(1,2) - x(2,1) + x(1,3) - x(3,1) + x(1,4) - x(4,1) =e= 1;
eq2(i,j).. - x(1,2) + x(2,1) + x(2,3) - x(3,2) + x(2,5) - x(5,2) =e= 0;
eq3(i,j).. - x(1,3) + x(3,1) - x(2,3) + x(3,2) + x(3,5) - x(5,3) =e= 0;
eq4(i,j).. - x(1,4) + x(4,1) + x(4,5) - x(5,4) =e= 0;
eq5(i,j).. - x(2,5) + x(5,2) - x(3,5) + x(5,3) - x(4,5) + x(5,4) =e= -1;
eq6(i,j).. - y(i,j) + x(i,j) =l= 0;
eq7(i,j).. l(i,j) - w(i) + w(j) - v(i,j) + lambda(i,j) =e= 0;
eq8(i,j).. v(i,j) - R * (1 - x(i,j)) =l= 0;
eq9(i,j).. lambda(i,j) - R * (1 - (y(i,j) - x(i,j))) =l= 0;
Model contohTMB /all/;
Solve contohTMB using MIP Minimizing z;
Display "Solution values:"
Display
x.l, z.l;
We make phones. We have selling price, production cost, profit.
The goal is to maximize profits.
The following components are required to assemble each phone.
Maximum quantity of components .
Orders (so many phones were ordered from us, we sold them) :
Here is my mod file:
set PHONE;
set COMPONENTS;
param price {PHONE} >= 0;
param cost {PHONE} >= 0;
param maxComponents {COMPONENTS} >= 0;
param ordered {PHONE} >= 0;
param matrix {COMPONENTS, PHONE}; #The amount of components needed to make a particular phone.
var x {PHONE} >= 0; # Number of manufactured telephones.
maximize profit: sum {i in PHONE} ( ordered[i] * price[i] - x[i] * cost[i] );
subject to min_manufacture {i in PHONE}:
x[i] >= ordered[i]; # We must produce a minimum of what is ordered
subject to component {i in COMPONENTS}:
sum {j in PHONE} matrix[i,j] * x[j] <= maxComponents[i]; # The number of components used must not exceed the maximum.
subject to min_quantity {i in COMPONENTS, l in PHONE}:
sum {j in PHONE} matrix[i,j] * x[j] >= matrix[i,l]; # Minimum quantity used per component if we manufacture at least one telephone. For example, a triple phone requires at least 2 of the five components.
and dat file:
set PHONE := 1 2 3 4 5;
set COMPONENTS:= 1 2 3 4 5 6 7;
param price :=
1 450
2 120
3 500
4 390
5 100;
param cost :=
1 370
2 90
3 400
4 320
5 70;
param maxComponents :=
1 28
2 20
3 8
4 30
5 47
6 27
7 15;
param ordered :=
1 3
2 5
3 5
4 0
5 10;
param matrix: 1 2 3 4 5 :=
1 1 1 0 0 0
2 1 1 0 0 0
3 1 0 0 0 0
4 1 0 1 1 0
5 0 0 2 1 1
6 0 0 2 1 0
7 0 0 1 1 0;
The problem is that if, for example, the maximum amount of sixth components is three, the maximum amount of seventh components is two , then 1.5 is produced from the triple phone which cannot be . And quantity used of the fourth, fifth, sixth, seventh components for the triple phone 1,5 3 3 1,5 which also cannot be.
How do I do it to just get a integer solution?
Because if I write to the variable x that it's an integer, I get zero for everything.
My run file:
model phone.mod;
data phone.dat;
option presolve 0;
option solver cplex;
solve;
display profit, x;
display {i in COMPONENTS, j in PHONE} matrix[i,j] * x[j];
You need to declare the relevant variables as integer, like so:
var x {PHONE} >= 0 integer;
Some solvers are not able to deal with integer constraints and may ignore that constraint (with a warning message) but CPLEX should be fine.
If there is a nested for loop inside while loop like this:
while(condition)
for(i=0; i<size; i++)
the size in the for loop is increasing every time the for loop is being executed, starting at 1,2,...,n-1
and the while loop runs n-1 times.
That means that the time complexity is O(n^3)?
If by every time the for loop is being executed you mean every time the while(condition) triggers a new run of the for loop, then the time complexity is .
That is, if you increment a counter inside the inner for loop, it will be incremented = n choose 2 times. But because constant factors and lower order terms are omitted in big O notation, we can just write .
For illustration, consider this Python 2.7 implementation (I converted the outer while loop + size increment into a for loop):
n = 10
counter = 0
for size in range(1, n):
for i in range(0, size):
counter += 1
print i,
print
print
print "counter =", counter
print "n choose 2 =", (n * (n - 1)) / 2
Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
counter = 45
n choose 2 = 45
Try running it with different values of n to see that the formula holds true.
I am using tetgen to generate meshes for my research.
My models have empty internal chambers inside them. For example, an empty box of size (5,5,5) inside a box of size (10, 10, 10). See image:
The problem is that tetgen generates tetrahedrons inside the empty chamber. Why? Is there a way to avoid it?
I tried using -YY, -q, -CC, -c, and their combinations, but all had the same problem, and did not give insight on the error. (http://wias-berlin.de/software/tetgen/1.5/doc/manual/manual005.html).
The way I solved it was to create a .poly file (http://wias-berlin.de/software/tetgen/fformats.poly.html). I created a .poly file from a .off file (https://en.wikipedia.org/wiki/OFF_(file_format)), which I could export from OpenScad.
.poly file has 4 parts, from which the 3rd specifies holes in the object. You need to tell TetGen where you have holes in the object.
The way to do it, is by specifying one point in the hole/chamber.
A possible .poly file would look like this:
part1 - vertices:
40 3 0 0
0 0.2 0 1
1 0.161803 0.117557 0
...
part2 - faces:
72 0
1
3 0 1 2
1
3 1 0 3
...
part3 - holes <============== the one I needed
1
1 0 0 0.5 <=== this is a point, which I know is inside my hole/chamber
So here is the file, without any breaks, just in case:
40 3 0 0
0 0.2 0 1
1 0.161803 0.117557 0
...
72 0
1
3 0 1 2
1
3 1 0 3
...
1
1 0 0 0.5
So, I know that Ada offers two remainder operators, rem and mod, but what exactly is the difference between them? I was able to find this, but I'm not sure I'm fully grasping the difference.
There's no difference between A mod B and A rem B if A is nonnegative and B is positive. If A is negative and B is positive, mod gives you the true mathematical modulo operation; thus, for example, if B is 5, here are the results of A mod 5 and A rem 5 for values of A:
A = -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
A mod 5 = 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3
A rem 5 = 0 -4 -3 -2 -1 0 -4 -3 -2 -1 0 1 2 3 4 0 1 2 3
Note the pattern in the A mod 5 results.
rem corresponds to the way the % operator works in C-style languages (but not Python or Ruby, apparently). It may be faster on some processors. If you have to deal with negative values for A, my hunch is that mod is much more likely to be useful, but there may be some uses for rem also. I don't think there's much use at all for mod or rem with a negative right-hand operand, so I wouldn't worry too much about the definition.
See also http://en.wikipedia.org/wiki/Modulo_operation.
According to the LRM, the difference is which operand's sign is associated with the result.
Integer division and remainder are defined by the relation
A = (A/B)*B + (A rem B)
where (A rem B) has the sign of A and an absolute value less than the absolute value of B. Integer division satisfies the identity
(-A)/B = -(A/B) = A/(-B)
The result of the modulus operation is such that (A mod B) has the sign of B and an absolute value less than the absolute value of B; in addition, for some integer value N, this result must satisfy the relation
A = B*N + (A mod B)