1

I am new to AMPL and doing MC problem. I have included my code. I asked earlier to help with other errors, at this moment program is working, but optimal solution is 0 and giving a strange error. What can be a problem? Maybe my constraints are wrong?

Error:

Error executing "solve" command:
error processing constraint c2[1,1]:
invalid subscript flow[1,4,1]

Code:

set NODES;  #define set of nodes
set ARCS within {NODES, NODES};
set COMMODITIES;
param demand {COMMODITIES};
param origin {COMMODITIES};
param dest {COMMODITIES};
param cap{ARCS} >= 0;
param cost{ARCS} >= 0;


# Decision Variables
var flow{(u,v,k) in ARCS cross COMMODITIES};   #flow assos with commodity k
var x{(u,v,k) in ARCS cross COMMODITIES} binary;  #fraction of flow assos with commodity k

# Objective Function
minimize Total_Cost:
   sum {(u,v) in ARCS, k in COMMODITIES}
      cost [u,v] *(flow[u,v,k]*x[u,v,k]);
      
# Capacity constraints
subject to c1 {(u,v) in ARCS}:
    sum{(u,v,k) in ARCS cross COMMODITIES} 
    (flow[u,v,k]*x[u,v,k]) <= cap[u,v];
 
    
#Flow conservation constraints
subject to c2 {k in COMMODITIES, u in NODES diff {origin[k], dest[k]}}:
    sum{(v,u) in ARCS} (flow[u,v,k]*x[v,u,k]) - sum{(u,v) in ARCS} (flow[u,v,k]*x[u,v,k]) = 0;
    
     
# Source and sink constraints
subject to c3{k in COMMODITIES, u in NODES diff {origin[k],dest[k]}}:
    sum{(u,v) in ARCS} flow[u,v,k] - sum{(v,u) in ARCS} flow[v,u,k] =
        if u = origin[k] then demand[k] else if u = dest[k] then -demand[k] else 0;
            
 
# Commodity flow constraints
subject to c4 {(u,v,k) in ARCS cross COMMODITIES}:
    flow[u,v,k] <= x[u,v,k] * cap[u,v]; 
        
# Non-negativity constraints
subject to c5 {(u,v,k) in ARCS cross COMMODITIES}:
    x[u,v,k] >= 0;
 
      
data;

set NODES := 1 2 3 4 5 6;

param: ARCS:   cost  cap  :=
       1,2     3      15
       2,3     6      1
       4,1     8      6
       2,5     4      10
       2,4     4      10
       3,4     3      6
       3,6     8      7
       5,3     12     4
       4,6     10     8 ;
  
param: COMMODITIES: origin dest    demand :=
           1           3    5      6
           2           1    6      4;

Program doesn't have an errors, but optimal solution is 0. I don't understand where is the problem, which constrains are wrong.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AlenaCh
  • 21
  • 2

1 Answers1

0

In constraint c2 you have the following:

sum{(v,u) in ARCS} (flow[u,v,k]*x[v,u,k]) 

However, flow is indexed over ARCS. If there is an arc (v,u) in ARCS, there is not guarantee of that there is also an arc (u,v) (opposite direction) and in that case flow[u,v,k] will be an invalid subscript.

fdabrandao
  • 81
  • 4
  • Hello! Thank you so much for your answer! I understand that my constraints doesn't apply a mutual direction. Maybe you have a tip how better to write this constrain? Because when add another one (constrain) with opposite direction it's wouldn't work. – AlenaCh Apr 13 '23 at 07:12
  • You can filter for ARCS for which there is a reverse arc: `sum{(v,u) in ARCS: (u,v) in ARCS} (flow[u,v,k]*x[v,u,k])` – fdabrandao Apr 13 '23 at 10:15