AlenaCh
AlenaCh

Reputation: 21

Hello! I am trying to write a model to solve Min Cost Problem in AMPL, however I keep getting an error regarding exeprion on origin and dest

I am new to AMPL and trying to make a simple model of MC problem, but I have so many errors and I can't understand how to solve them. I have attached bellow my model with a data in it. Maybe someone can please help to understand what is wrong?

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


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

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

set NODES := 1 2 3 4 5 6;

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

param COMMODITIES: orgin dest quantity :=
        1           3    5      6
        2           1    6      4

I have tried to define constrains separately, but it wouldn't help.

Upvotes: 1

Views: 29

Answers (1)

fdabrandao
fdabrandao

Reputation: 121

One error that is affecting most of the model is:

test.run, line 4 (offset 97):
    COMMODITIES needs to be subscripted.
context:  param  >>> quantity{COMMODITIES} <<< ;

You declared set COMMODITIES {NODES}; which is an indexed set. In the model you seem to use it as a regular set, in which case you can just use set COMMODITIES;.

Then in line 25, you have subject to c2 {u in NODES exept {orgin, dest}, k in COMMODITIES}:. Instead you should have u in NODES diff {orgin, dest}, but you also need to replace orgin and dest by the nodes since they orgin and dest are both declared as parameters. If you want to use the origin[k] and dest[k] (for each commodity k), then you can do that as follows:

#Flow conservation constraints
subject to c2 {k in COMMODITIES, u in NODES diff {origin[k], dest[k]}}:
    sum{(v,u) in ARCS} x[v,u,k] - sum{(u,v) in ARCS} x[u,v,k] = 0;

Fixing this and some typos you get the following model:

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


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

# Objective Function
minimize Total_Cost:
   sum {(u,v) in ARCS, k in COMMODITIES}
      cost [u,v] *x[u,v,k];
      
# Capacity constraints
subject to c1 {(u,v) in ARCS}:
    sum{(u,v,k) in ARCS cross COMMODITIES} 
    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} x[v,u,k] - sum{(u,v) in ARCS} x[u,v,k] = 0;
    
    
# Source and sink constraints
subject to c5{k in COMMODITIES, v in NODES diff {origin[k],dest[k]}}:
    sum{(u,v) in ARCS} x[u,v,k] - sum{(v,u) in ARCS} x[v,u,k] =
        if v = origin[k] then +quantity[k] else if v = dest[k] then -quantity[k] else 0;  

# Non-negativity constraints
subject to c3 {(u,v,k) in ARCS cross COMMODITIES}:
    x[u,v,k] >= 0;
 
      
data;

set NODES := 1 2 3 4 5 6;

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

param: COMMODITIES: origin dest quantity :=
        1           3    5      6
        2           1    6      4;

Upvotes: 0

Related Questions