Hi Yu
IF you know what you want to count, you should study your loops and then you will find where to put the line with loopnr = loopnr + 1…
Renger
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com]
Sent: Montag, 25. Januar 2016 16:11
To: gamsworld@googlegroups.com
Subject: Re: how to code the position of last element in a set
hi Renger,
i added it in my code, but it returns wrong number, could you look it for me, thank you!Thank you!
Yu
Parameter loopnr /0/
Parameter lastloopnr ;
Option optcr=0;
Option minlp = baron;
// Heuristik
d = orig_d ;
while(not end,
Loopnr = loopnr + 1;
lambda(i)=0 ;
sigma=sigma_hat ;
h(i,l)=lz*p(i,l) ;
converged = 0 ;
loop(iteration$(not converged), // Subgradientenverfahren
solve relaxation using minlp minimizing Lagrange ;
display q.l ;
sigma=sigma_hat/ord(iteration) ;
lambda(i)$flag(i)=max(0, lambda(i)+sigma*(sum(l, d*q.l(i,l))-QM(i)*sq.l)) ;
display lambda;
loop(i$flag(i),
if (lambda(i)0)) = QM(i)/orig_d ;
// letzte Iteration: Lösung der Lagrange-Relaxation ist zulässig, d.h. lambda(i)=0 für alle i
if (sum(i$flag(i), lambda(i)) = 0,
end = 1;
loop(i$flag(i),
if (sum(l, q.l(i,l))>1e-4,//warum 1e-4
alpha(i)=1-sum(ii$selected(ii), alpha(ii));
selected(i)=1;
);
);
);
display alpha ;
selected(i)$(flag(i) and (lambda(i)>0)) = 1 ;
flag(i)(flag(i) and (lambda(i)>0)) = 0 ;
\
\
\
d = d - sum(i(lambda(i)>0), qm(i)) ;
if (d = 0, end = 1) ;
display d ;
);
Lastloopnr = loopnr;
display lastloopnr;
在 2016å¹´1月25日,15:44,Renger van Nieuwkoop 写é“:
If you want to know when your loop ends, you have to define a parameter and store this number in this parameter
Parameter
loopnr /0/,
lastloopnr ;
While (……,
Loopnr = loopnr + 1
My code
);
Lastloopnr = loopnr;
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com]
Sent: Montag, 25. Januar 2016 13:45
To: gamsworld@googlegroups.com
Subject: Re: how to code the position of last element in a set
hi Renger,
I attach my Codes, i have already put the Option in my codes, but it does’t work.
I want to count the total iteration for model Relaxation within the iteration what i defined( 50iterations).
Cound you help me?
best regards
Yu Li
*Subgradientenverfahren für Stufenrabatten
$eolcom //
sets
i Lieferanten /L1*L4/
l Rabattstufen /R1*R4/ ;
alias(i,ii);
Table P(i,l) Preis
R1 R2 R3 R4
L1 50 48 45 40
L2 48 45 43 40
L3 49 45 44 41
L4 47 44 40 37;
Table QU(i,l) Untergrenzen von Rabattstufe
R1 R2 R3 R4
L1 30 60 100 200
L2 40 80 120 220
L3 30 70 130 210
L4 20 40 60 150;
Table QO(i,l) Obergrenzen von Rabattstufe
R1 R2 R3 R4
L1 60 100 200 10000
L2 80 120 220 10000
L3 70 130 210 10000
L4 40 60 150 10000;
Parameters
QM(i) maximale Lieferraten
/L1 28
L2 20
L3 15
L4 30/
K(i) fixe Lieferkosten
/L1 120
L2 110
L3 115
L4 110/
lambda(i) Lagrange-Multiplikatoren
h(i,l) Lagerungskostensätze ;
Scalars d Rest-Bedarf pro Periode
orig_d ursprünglicher Bedarf pro Periode /35/
lz Lagerzinssatz /0.03/;
Variables
Lagrange Wert der Lagrange-Funktion
q(i,l) Bestellmenge
sq gesamte Bestellmenge
y(i,l) binaere Indikatorvariable fuer q>0 ;
sq.lo = 1; // zur Vermeidung numerischer Instabilität
Positive variable q;
Binary variable y;
Parameter flag(i) 1 wenn Lieferant i noch untersucht wird ;
flag(i)=1;
Equations
LR Definition Lagrange-Funktion
NB1 Bedarfdsrestriktion
NB2(i,l) Untergrenze von Bestellmenge
NB3(i,l) Obergrenze von Bestellmenge
NB4(i) Eindeutigkeit ;
LR… Lagrange =e= sum((i,l)$flag(i), p(i,l)*q(i,l)d/sq + h(i,l) q(i,l)/2 + K(i)*y(i,l)*d/sq);
NB1… sq =e= sum((i,l)$flag(i), q(i,l));
NB2(i,l)$flag(i)… q(i,l) =g= QU(i,l)*y(i,l) ;
NB3(i,l)$flag(i)… q(i,l) =l= QO(i,l)*y(i,l) ;
NB4(i)$flag(i)… sum(l, y(i,l)) =l= 1 ;
Model relaxation /LR, NB1, NB2, NB3, NB4/ ;
Set Iteration /iter1*iter50/;
Scalars end zeigt an ob Heuristik beendet ist /0/
converged zeigt an ob Subgradientenverfahren konvergiert ist
epsilon Toleranz für Abbruch des Subgradientenverfahrens /1/
sigma Schrittweite
sigma_hat Anfangswert der Schrittweite /1/ ;
Parameter alpha(i) Liefermengenanteile der Lieferanten i ;
Parameter selected(i) zeigt an ob bei Lieferant i eingekauft wird ;
selected(i)=0;
Option optcr=0;
Option minlp = baron;
// Heuristik
d = orig_d ;
while(not end,
lambda(i)=0 ;
sigma=sigma_hat ;
h(i,l)=lz*p(i,l) ;
converged = 0 ;
loop(iteration$(not converged), // Subgradientenverfahren
solve relaxation using minlp minimizing Lagrange ;
display q.l ;
sigma=sigma_hat/ord(iteration) ;
lambda(i)$flag(i)=max(0, lambda(i)+sigma*(sum(l, d*q.l(i,l))-QM(i)*sq.l)) ;
display lambda;
loop(i$flag(i),
if (lambda(i)0)) = QM(i)/orig_d ;
// letzte Iteration: Lösung der Lagrange-Relaxation ist zulässig, d.h. lambda(i)=0 für alle i
if (sum(i$flag(i), lambda(i)) = 0,
end = 1;
loop(i$flag(i),
if (sum(l, q.l(i,l))>1e-4,//warum 1e-4
alpha(i)=1-sum(ii$selected(ii), alpha(ii));
selected(i)=1;
);
);
);
display alpha ;
selected(i)$(flag(i) and (lambda(i)>0)) = 1 ;
flag(i)(flag(i) and (lambda(i)>0)) = 0 ;
\
\
\
d = d - sum(i(lambda(i)>0), qm(i)) ;
if (d = 0, end = 1) ;
display d ;
);
Parameter An Anzahl der Iteration für Modell Relaxation;
An=Relaxation.iterusd ;
display An;
Variables
qq(i,l) Bestellmenge
z(i,l) Binaere Indikatorvariable fuer q>0
sqq gesamte Bestellmenge
total_cost Gesamtkosten ;
sqq.lo=1;
Positive variable qq;
Binary variable z;
Equations
cost Gesamtkosten
NB6(i) Eindeutigkeit
NB7(i,l) Untergrenze von Bestellmenge
NB8(i,l) Obergrenze von Bestellmenge
NB9(i) Maximale Liefermenge;
cost… total_cost =e= sum(i$selected(i), (alpha(i)*orig_d + alpha(i)lz/2sqq)*sum(l, p(i,l)*z(i,l)) + K(i)*orig_d/sqq) ;
NB6(i)$(selected(i))… sum(l, z(i,l)) =l= 1 ;
NB7(i,l)$(selected(i))… qq(i,l) =g= QU(i,l)*z(i,l) ;
NB8(i,l)$(selected(i))… qq(i,l) =l= QO(i,l)*z(i,l) ;
NB9(i)$(selected(i))… sum(l, qq(i,l)) =e= alpha(i)*sqq ;
Model opt_q /cost, NB6, NB7, NB8, NB9/;
sqq.lo = smax(i$selected(i), QU(i,“R1”)/alpha(i)) ;
Solve opt_q using minlp minimizing total_cost ;
Display qq.l, sqq.l, z.l, total_cost.l ;
在 2016å¹´1月25日,12:33,Renger van Nieuwkoop 写é“:
Hi Yu
You can the iterations used to solve a model by storing yourmodelname.iterusd in a parameter.
e.g.
model test /all/;
solve test…
parameter iterationsused;
iterationsused = test.iterusd;
Cheers
Renger
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com]
Sent: Freitag, 22. Januar 2016 18:59
To: gamsworld@googlegroups.com
Subject: Re: how to code the position of last element in a set
hi Renger,
can you please help me again, how can i write the code for output the number of Iteration for some certain model, is there an option for this or some easy way?
Thank you!
yu
在 2016å¹´1月20日,10:56,Renger van Nieuwkoop 写é“:
Yes, it should be.
Cheers
Renger
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com]
Sent: Mittwoch, 20. Januar 2016 09:49
To: gamsworld@googlegroups.com
Subject: Re: how to code the position of last element in a set
hi Renger,
thank you for you help, but i don’t unterstand , why hier is ord(i) 写é“:
NB5(i,l)(ord(l)>1 and ord(i)
\
\
\
my code:
\
\
\
NB5(i,l)(ord(l)>1 and ord(l)1 and ord(l) is the order number for the last one.
some advice. Thank you!
Best regards
Yu
在 2016å¹´1月19日,15:58,Renger van Nieuwkoop 写é“:
Hi Yu
Use card(i) which will give you the cardinality of the set (the number of elements)
Cheers
Renger
From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com] On Behalf Of Yu Li
Sent: Dienstag, 19. Januar 2016 15:14
To: gamsworld
Subject: how to code the position of last element in a set
Hallo,
the position for the first element in a set we can code like this: ord(i)=1, how to code the position for the last one?
Thanks in advance.
Yu
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
–
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.