Solved

# Loop in Main Execution to change a parameter at each iteration

• 5 replies
• 102 views • Explorer
• 5 replies

Hello,

I want to solve an optimization problem with a minimization of costs. I would like to create a loop that would help me solving the same model but changing only one parameter of my objective function at each iteration.

The logic is the following :

While (Parameter <= Bound ):

Solve Objective_Function(Parameter);

Parameter = Parameter + 1;

where “Parameter” is the parameter I want to change at each iteration of the loop.

where “Bound” is simply a bound to stop my while-loop.

where Objective_function is simply the objective function of my problem and depends on the parameter that should change at each iteration.

Could you help me?

Thank you.

icon

Best answer by gdiepen 17 March 2022, 10:00

View original

### 5 replies I tried with this loop but it’s not working …

for (n in iteration) do

! Assign a different value to fuelEff at each iteration

! At each iteration the fuel efficiency improves by 10%, (f_0 decreases by 10%)

fuelEff := f_0*Power(1-0.1,n);

! Solve the problem

solve minCost;

! Save the required outputs

TotalCost_n(n) := ObjFunc;

Procurement_Cost_n(n) := ProcurementCost;

Transport_Cost_n(n) := TransportCost;

DistanceTraveled_n(n) := TotalDistance;

endfor;

Userlevel 4 +7

You mention it does not work, could you maybe elaborate a bit more on this? Do you get any errors? Have you tried executing the procedure containing the code with the Debugger to go through it step by step? Here is the file with our AIMMS Project. The loop is in the MainExecution section.

As you can see, it is a Traveling Purchaser Problem with multi-vehicles. We want to create a loop that vary the fuel efficiency parameter, minimize the costs for that iteration and then store the results in different variables. The results should be saved in the Procurement_Cost_n, Total_Cost_n, Transport_Cost_n and Distance_Traveled_n variables.

We tried several ways to write the line aiming at changing the fuel efficiency variable :

• fuelEff := f_0*Power(1-0.1,n);

where f_0 is just a parameter with the first value we want to test for the fuel efficiency.

With the line above, the error is “The second argument is of type element while expecting an argument of type numeric”.

We tried also more simple ways to change the value (without the Power() function). For instance :

• fuelEff := f_0*2*n;

The error in that case is “The RHS expression is of type string while expecting an expression of type numeric”.

I didn’t know the existence of the Debugger. I am now trying to do it step-by-step using it.

Thank you in advance!

Userlevel 4 +7

Quickly took a look at the source of the model and the problem is in the definition of your set iteration.

As the definition for this set, you have ElementRange(0,9) and my guess is that you assume that this set therefore are numbers. Unfortunately, this is not the case ;) They are string-based elements (that look like numbers).

You have multiple options, you can either make the set iteration a subset of Integers (and set the property of this set to “elements are numeric”). This way, when you loop over it with index n, every iteration will be a numerical value instead of the current string-based label.

Alternatively, you don’t need to explicitly create the iteration set. According to the documentation of the for statement you can define it dynamically in the statement itself:

https://documentation.aimms.com/language-reference/procedural-language-components/execution-statements/flow-control-statements.html#the-for-statement Thank you very much! It works now!!