writing for loop

  • 25 September 2019
  • 8 replies
  • 549 views

Dear,
I want to do a cycle for. I have two boundary values and I want to consider values between these boundaries. Then for these considered values I want to solve the Min_Total_Costs and have the values of Total Costs for the considered values. How can I write this cycle for?
How can define set, ect?

For now I wrote this code

for e| (epsilon_e(e))>epsilonl and (epsilon_e(e))
CO2_ec:=epsilon_e(e);
solve Min_Total_Costs;
Total_Costs_e(e):=Total_Costs;
endfor;

with e the index of a set ep and epsilon_e(e) an integer variable.
But it seems that it is not working.
Can you help me?
Thanks.

8 replies

Userlevel 5
Badge +2
If you have two boundary integer values { P_Lower = 1, P_Upper = 10 }, you could define a set:

code:
S_Cycles
SubsetOf: Integers
Index: CYCLE
Definition: { (P_Lower+1) .. (P_Upper-1) }


and you could use this set to loop in:

code:
for CYCLE do
solve Min_Total_Costs;
P_TotalCost(CYCLE) := Min_Total_Costs.Objective;
endfor;


I dind't understand the relation between your integer variable and this CYCLE, but you can add a constraint (variable = cycle) or you can try to use the variable attribute "range" instead.

Hope it helps!
Dear @mateusarakawa ,
thanks for your response.
Can you explain better this:

I dind't understand the relation between your integer variable and this CYCLE, but you can add a constraint (variable = cycle) or you can try to use the variable attribute "range" instead.

Best
GL
Userlevel 5
Badge +2
Hi, @graleo! I didn't understand the purpose of the loop.

For instance, for each iteration what should change within your mathematical model? A variable value?
Hi @mateusarakawa ,
I want to change the value of cycle so this value is used to Min Total Cost.
I want to have and plot the values of Total Costs.
How can I do?
Best
GL
Hi @mateusarakawa
Is it necessary to write something in the MainTermination and PreMainTermination section?
Thanks
GL
Userlevel 5
Badge +2
@graleo

Looks like you want to vary the value of CO2_ec between the two boundary values and solve the math model. The number of cycles in this loop depends on the difference between the boundary values and by how much you want to increment in each step. Using a while loop will give you more flexibility here. What you will need to do is create a set a below

code:
S_Cycles
Index: CYCLE
Parameter: ep_Cycle


Declare parameters - epsilon, increment, p_TotalCost(CYCLE)

Now write your procedure.

code:
empty s_Cycles;
epsilon := boundary_low;
!initializing epsilon at the lower boundary value

while epsilon <= boundary_high do

SetElementAdd(
Setname : S_Cycles ,
Elempar : ep_Cycle ,
Newname : FormatString("Cycle-%n", loopcount));
!Using predeclared function SetElementAdd to create cycles as we go. This removes need to calculate number of cycles based on difference and increment

CO2_ec := epsilon;
solve Min_Total_Costs;
P_TotalCost(ep_Cycle) := Min_Total_Costs.Objective;
!you can also use P_TotalCost(ep_Cycle) := Total_Costs;

epsilon += increment;
!increasing the epsilon value
endwhile;


You do not need to write anything in MainTermination or PreMainTermination.

Hope this helps.
dear @mohansx ,
thanks for your response. I solved it also with the cycle for

for e do
epsilon_e(e):=epsilonlow+e*(epsilonhigh-epsilonlow)/10;
solve Min_Total_Costs;
Total_Costs_e(e):=Total_Costs;
endfor;

where e is a set (integer number from 1 to 10). What do you think?

In your code what is the meaning of

code:
SetElementAdd(
Setname : S_Cycles ,
Elempar : ep_Cycle ,
Newname : FormatString("Cycle-%n", loopcount));


Best
Userlevel 5
Badge +2
@graleo , your approach is also correct. Just make sure that your math model Min_Total_Costs uses the proper value of epsilon_e(e) when solving.

Using SetElementAdd, I am dynamically creating the elements in the set S_Cycles. In your approach, you decided that you will have 10 cycles (as e is a set from 1 to 10). In my approach, instead of deciding on the 10 cycles, I decided on how much should epsilon be increased in each iteration, and by using SetElementAdd, I am creating those elements as I go.

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator