Search code examples
optimizationampl

AMPL code compilation problem: is already defined


I have short task in AMPL.

There was similar question but it don't bring me the solution

It's my .mod file:


set MOTORS;
set FABRICS;

param work {FABRICS,MOTORS}>= 0;
param power {FABRICS}>= 0;
param price {MOTORS}>= 0;

var prod {MOTORS}>= 0;
var use {o in FABRICS} = sum {k in MOTORS} work [o,k] * prod[k];
var free {o in FABRICS} >= 0;

var income = sum {k in MOTORS} price[k]*prod[k];

maximize income;

s.t. Ogrfreepower {o in FABRICS}:  free[o]= power[o]- use[o];

It's my .dat file:

set MOTORS:= GL SL ;
set FABRICS:= montage paintshop;

    param work:  GL  SL:=
    montage   2   5 
    paintshop   3   3
        ;

    param:  power :=
    montage   150
    paintshop   180
    ;

    param:  price :=
    GL  2
    SL  3
    ;

It's my .run file:

#RESET THE AMPL ENVIROMENT

reset;

model example1.mod;

data example1.dat;

#LOAD THE MODEL

option solver './cplex';

#SOLVE
solve;

#SHOW RESULTS
display income;

when i try to use i console:

ampl: include example1.run;

it brings me:

example1.mod, line 19 (offset 314):
    income is already defined
context:  maximize  >>> income; <<< 

what should I change to compile it correctly?


Solution

  • Try changing

    maximize income;
    

    to:

    maximize objective_function: income;
    

    AFAIK, the issue here is that AMPL treats variables and objective functions as separate entities. maximize income is interpreted as declaring an objective function named "income" when you already have a variable of that name, hence the "already defined" error.