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:

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.)