Declaring variables in diferent ways leads to diferent results

31 views
Skip to first unread message

Leandro O.

unread,
May 15, 2023, 9:03:04 AM5/15/23
to AMPL Modeling Language
I want to know if the syntaxes bellow are diferent 

METHOD 1:

var AREA_PAST_Q_OUT, >= 0.00;
var AREA_PAST_Q_NOV, >= 0.00;
var AREA_PAST_Q_DEZ, >= 0.00;
var AREA_PAST_Q_JAN, >= 0.00;
var AREA_PAST_Q_FEV, >= 0.00;
var AREA_PAST_Q_MAR, >= 0.00;
var AREA_PAST_Q_ABR, >= 0.00;
var AREA_PAST_Q_MAI, = 0.00;
var AREA_PAST_Q_JUN, = 0.00;
var AREA_PAST_Q_JUL, = 0.00;
var AREA_PAST_Q_AGO, = 0.00;
var AREA_PAST_Q_SET, = 0.00;

var A_PQ_P, >= 0;  # COMMOM VARIABLE

s.t. PASTAGEM_QUENTE_OUT: AREA_PAST_Q_OUT = A_PQ_P;
s.t. PASTAGEM_QUENTE_NOV: AREA_PAST_Q_NOV = A_PQ_P;
s.t. PASTAGEM_QUENTE_DEZ: AREA_PAST_Q_DEZ = A_PQ_P;
s.t. PASTAGEM_QUENTE_JAN: AREA_PAST_Q_JAN = A_PQ_P;
s.t. PASTAGEM_QUENTE_FEV: AREA_PAST_Q_FEV = A_PQ_P;
s.t. PASTAGEM_QUENTE_MAR: AREA_PAST_Q_MAR = A_PQ_P;
s.t. PASTAGEM_QUENTE_ABR: AREA_PAST_Q_ABR = A_PQ_P;

METHOD 2:
set MESES := 1..12;

var AREA_SOJA{MESES};

var A_SJ_P, >= 0.00; # COMMOM VARIABLE

s.t. SOJA_PLANTADA{m in MESES}:
AREA_SOJA[m] = if m in {11,12,1,2,3,4} then A_SJ_P else 0.00;

P.S.: I just changed the month word representation to number representation.
In the fist method the variables are inside a set (I guess) and the the second don't. I had a problem printing using set before, i had to use a small increment for the set values to print the variable value because leads to problem duplicated value in set (see bellow):

set JANEIRO := {
"1", "JANEIRO", "QUENTE",
AREA_SOJA_JAN+0.000000001, AREA_TRIGO_JAN+0.000000002,     AREA_PAST_Q_JAN+0.000000003, AREA_PAST_F_JAN+0.000000004, AREA_SILAGEM_JAN+0.000000005,
RACAO_JAN+0.000000006,     VACAS_LACTACAO_JAN+0.000000007, VACAS_SECAS_JAN+0.000000008, NOVILHAS_JAN+0.000000009,    TERNEIRAS_JAN+0.00000000001
};

AMPL Google Group

unread,
May 16, 2023, 6:33:07 PM5/16/23
to AMPL Modeling Language
These formulations do appear to be the same, except that in the constraint you should say "if m in {10,11,12,1,2,3,4}". But I think you could express the model even more clearly like this:

set MESES;
set MESES_A_SJ_P within MESES;

var AREA_SOJA {MESES};
var A_SJ_P >= 0;

s.t. SOJA_PLANTADA {m in MESES}:
   AREA_SOJA[m] = if m in MESES_A_SJ_P then A_SJ_P else 0;

Then in a data file for this model, you could specify the members of the two sets with the following statements:

set MESES := OUT NOV DEZ JAN FEV MAR ABR MAI JUN JUL AGO SET ;
set MESES_A_SJ_P := OUT NOV DEZ JAN FEV MAR ABR ;

I am not sure why you need to have the set JANEIRO. Is there some other constraint in the model where you need the months to be given by numbers?


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2243099660-117002#}

Leandro O.

unread,
Nov 30, 2023, 8:42:33 AM11/30/23
to AMPL Modeling Language
Hi,  thank you so much for your fast answer. Gmail threw my email in another folder and here I'm 5 months later haha.

I did something in compact mode to print the results:

```
# SAÍDAS
set ROTULOS := {
# MÊS
"MES",
# ATIVIDADES EXTRAS
"AREA_SOJA",
"AREA_TRIGO",
# ALIMENTOS
"AREA_PAST_Q",
"AREA_PAST_F",
"AREA_SILAGEM",
"RAÇÃO",
# ANIMAIS
"VACAS_LEITEIRAS",
"VACAS_SECAS",
"NOVILHAS",
"TERNEIRAS"
};

printf "\n";

# ARMAZENAR A SAÍDA EM ARQUIVO CSV
printf {rot in ROTULOS} "%s;", rot >> "resultados_V4.csv";
printf {m in MESES}
"\n%s;%.6f;%.6f;%.6f;%.6f;%.6f;%.6f;%.6f;%.6f;%.6f;%.6f;",
# MÊS
m,
# ATIVIDADES EXTRAS
AREA_SOJA_[m],      # DEMANDA DE ÁREA PELA SOJA
AREA_TRIGO_[m],     # DEMANDA DE ÁREA PELO TRIGO
# ALIMENTOS
AREA_PAST_Q_[m],    # DEMANDA DE ÁREA DA PASTAGEM QUENTE
AREA_PAST_F_[m],    # DEMANDA DE ÁREA DA PASTAGEM FRIA
AREA_SILAGEM_[m],   # DEMANDA DE ÁREA PELA SILAGEM
RACAO_[m],     # DEMANDA DE RAÇÃO
# ANIMAIS
VACAS_LACTACAO_[m], # QUANTIDADE DE VACAS EM LACTAÇÃO
VACAS_SECAS_[m], # QUANTIDADE DE VACAS SECAS/DESCARTE
NOVILHAS_[m], # QUANTIDADE DE NOVILHAS
TERNEIRAS_[m] # QUANTIDADE DE TERNEIRAS
>> "resultados_V4.csv"; 
```

Reply all
Reply to author
Forward
0 new messages