Menu

dual variable and initial input

2021-12-16
2022-04-11
  • Hoomaan Moradi

    Hoomaan Moradi - 2021-12-16

    Hello, I would be truly grateful if someone lets me know how I can define dual variables as an output for MPC sfunction in Simulink.
    Additionally, please let me know whether it is possible to use userparam to define the initial u in each sampling time (u(0)=param[1]) in Simulink. Many thanks.

    Best regards,
    Hoomaan

     
  • Andreas Völz

    Andreas Völz - 2021-12-16

    Dear Hoomaan,

    what do you mean by dual variables? Are you interested in the adjoint states related to the dynamics or in the Lagrangian multipliers related to the constraints? In both cases, you have to modify grampc_run_Sfct.c. You can find the definition of the output ports in the function mdlInitializeSizes and the setting of the outputs in mdlOutputs.

    Regarding the second question, userparam is already passed to the S-function via a dynamically-sized input port. If you want to adjust the inital controls, you could do this in mdlOutputs after reading the userparams and before calling grampc_run, that is in line 339.

    Could you desribe your exact usecase a bit more detailed?

    Regards,
    Andreas Völz

     
  • Hoomaan Moradi

    Hoomaan Moradi - 2021-12-20

    Dear Andreas,

    Thank you so much for the reply. Actually, I am working on online tuning of MPC’s parameters (related to the cost function and model). To this end, for doing the related computations, I need to have Lagrangian multipliers related to the equality and inequality constraints (adj and mult, respectively) in each sampling time. Thanks to your guidance, I could define adjoint states and multipliers in grampcrunSfct.c by reading from grampc->rws->adj and grampc->rws->mult, respectively.
    In addition to MPC’s parameters (userparam), I have to also tune u[0] in each sampling time. My question is whether it is possible to define an equality constrain like u[0]=param[1], so that param[1] can be defined as one of the elements of userparam?

    Best regards,
    Hoomaan

     

    Last edit: Hoomaan Moradi 2021-12-20
  • Andreas Völz

    Andreas Völz - 2021-12-20

    Dear Hoomaan,

    if you want to fix the first control u[0], i.e. not optimize it, then the best approach is to set the gradient dfdu_vec to zero if t==0 and manually set grampc.rws.u[0] = userparam[0] to your desired value before the call to grampc_run(...).

    Regards,
    Andreas Völz

     
  • Hoomaan Moradi

    Hoomaan Moradi - 2022-04-08

    Dear Andreas,

    Thank you so much for your support. For fixing the first control, u[0], as you mentioned, I defined df/du as follows:

    if (t == (typeRNum) 0)
    out[0] = (typeRNum)0vec[0];
    else
    out[0] = (typeRNum)((param[0])/15.5179)vec[0];

    Additionally, in grampcreuSfct.c before grampcrun(grampc); I added this line:

    grampc->rws->u[0] = (typeRNum)duserparam[0];

    However, u[0] does not become userparam[0] in any sampling time. Could you please let me know whether there is another method for fixing u[0] to userparam[0] in all sampling times from the beginning to the end of the simulation?

    Best regards,
    Hoomaan

     
  • Andreas Völz

    Andreas Völz - 2022-04-11

    Dear Hoomaan,

    I'm sorry, but when I said "if t == 0" I did not consider that ffct, dfdx_vec, and dfdu_vec receive as argument the global time "grampc->param->t0 + t" instead of the prediction time "t" as in the other problem functions. This might be the reason why GRAMPC still updates the first element of the control vector. You could check this by not updating t0 in the S-function in line 327. Please report back if this works as intended.

    Best regards,
    Andreas Völz

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.