# solve ‘opti’ in a parallel computing way utilizing ‘parfor’

321 views
Skip to first unread message

qi wang

unread,
Aug 13, 2022, 12:03:32 AM8/13/22
to CasADi

1. Programming tools are Matlab 2019b, Casadi V3.5.5 (latest).

2. We constructed 6 optimization problems by applying the same mathematical model. For the convenience of understanding, let's take an example, we divide the 24h into 6 segments, each segment is 4h.

1) In our model, the segment is defined as variable obj.param.Period, i.e. obj.param.Period = 6.

2) The variables, constraints, objectives in our model all are formulated by a 6*1 cell, as shown in the following figures:

 微信图片_20220813115933.jpg

 3) The obj is defined as a custom function class, i.e. grid, as follows: (Refer to subsection 6.2.2. at https://web.casadi.org/docs/#document-opti)

**********************************

classdef grid < handle

    properties

        param       

        var                      

        constraints

        objective   

        opti        

        sol               

    end

    methods

        function obj = grid()

            obj.param.Period = 6;

            obj.opti = casadi.Opti();

            obj.opti.solver('ipopt');

            ...

        end

    end

end

**********************************

We are now solving the problems in a serial way, as shown in the following code section, it works correctly. In order to improve the computational efficiency, we want to use parallel computing instead, such as ‘parfor’ in Matlab.

However, Matlab throws an error:

The variable 'obj' in the body of the parfor loop cannot be classified. For more information, see The parallel for loop in MATLAB "solves the variable classification problem in the parfor loop".

**********************************

for i=1:obj.param.Period

    obj.param.Seg = i;

    obj.opti.subject_to();

    Cons = [obj.constraints{obj.param.Seg,1}];

    obj.opti.subject_to(Cons);

    OBJ = obj.objective{obj.param.Seg,1};

    obj.opti.minimize(OBJ);

    [~,obj.sol] = evalc('obj.opti.solve();');

end

**********************************

 [Questions] So my question is, can I solve ‘opti’ in a parallel computing way utilizing ‘parfor’ in MATLAB based on the model I have built? If it is, how can I implement it?  

If not, is there any other way to implement parallel computing for my optimization problem without changing the model I have built? 

Or, the worst case for me, I have to change the way I model this problem and realize parallel computing in other ways. (If it is, what is the specific method?)

(It would be great if you could provide a piece of code for me to refer to.)

qi wang

unread,
Aug 13, 2022, 10:35:09 PM8/13/22
to CasADi
Hi, this problem has puzzled me for a long time, and I have also searched for various related information on the Internet, but I still have not been able to tackle this problem.
Looking forward to your prompt reply, thanks in advance!

Bruno M L

unread,
Aug 16, 2022, 8:14:49 AM8/16/22
to CasADi
Hi! Have you seen this blog post? I'm not familiar with parfor but maybe this post helps.

Charles Khazoom

unread,
Aug 27, 2022, 10:11:02 PM8/27/22
to CasADi
Instead of using Parfor, you can convert your opti object as a casadi Function using :

f = opti.to_function('fname',{initial guess, param1,  param2, ... },{output1, output2, ...})

Then, you can use f.map() which creates another casadi function that can be evaluated in parallel for different arguments (see documentation https://web.casadi.org/docs/ for more info on map().
Reply all
Reply to author
Forward
0 new messages