A simple quadratic optimizer for only constraints on input












0














I'm going to implement an quadratic optimizer with C for embedded systems. I will do that because I need speed.



But I have some trouble to find a quadratic optimizer for C that works with embedded systems. I'm using 32 bit CPU.



This is not a question about embedded nor C. This is a question of it's possible to simulate for example an input between 0 to 255, 8-bit resolution, for 50 times each, which will be 256*50 = 12800 times.



I have an idea of simulating a model with constant signal, then compare the signal with the reference value.



$$J(u_k) = abs(R-sum(y))$$



Where $u$ is the index variable of the current input. Then another input values is used $u = {0,1,2,3,4,5,...,u_k}$ for the constant simulation, and we get another $J$



Then we find which $J$ values is the smallest and compute its index. The value we are going to get will be between 0 and 255.



The procedure can be viewed like this:




  1. Simulate with $u = 0$ in 50 loops with Euler-forward

  2. Sum the output vector $y$

  3. Compare the absolut value of output vector with the reference vector $R$

  4. Repeat with $u = 1$ for 50 loops with Euler-forward

  5. .....

  6. ....


Will that work? Or is there a better way? Like compute the unconstrained Model Predictive Controller with saturation of the input e.g 255. I only need constraints on the input. Not the states.










share|cite|improve this question





























    0














    I'm going to implement an quadratic optimizer with C for embedded systems. I will do that because I need speed.



    But I have some trouble to find a quadratic optimizer for C that works with embedded systems. I'm using 32 bit CPU.



    This is not a question about embedded nor C. This is a question of it's possible to simulate for example an input between 0 to 255, 8-bit resolution, for 50 times each, which will be 256*50 = 12800 times.



    I have an idea of simulating a model with constant signal, then compare the signal with the reference value.



    $$J(u_k) = abs(R-sum(y))$$



    Where $u$ is the index variable of the current input. Then another input values is used $u = {0,1,2,3,4,5,...,u_k}$ for the constant simulation, and we get another $J$



    Then we find which $J$ values is the smallest and compute its index. The value we are going to get will be between 0 and 255.



    The procedure can be viewed like this:




    1. Simulate with $u = 0$ in 50 loops with Euler-forward

    2. Sum the output vector $y$

    3. Compare the absolut value of output vector with the reference vector $R$

    4. Repeat with $u = 1$ for 50 loops with Euler-forward

    5. .....

    6. ....


    Will that work? Or is there a better way? Like compute the unconstrained Model Predictive Controller with saturation of the input e.g 255. I only need constraints on the input. Not the states.










    share|cite|improve this question



























      0












      0








      0







      I'm going to implement an quadratic optimizer with C for embedded systems. I will do that because I need speed.



      But I have some trouble to find a quadratic optimizer for C that works with embedded systems. I'm using 32 bit CPU.



      This is not a question about embedded nor C. This is a question of it's possible to simulate for example an input between 0 to 255, 8-bit resolution, for 50 times each, which will be 256*50 = 12800 times.



      I have an idea of simulating a model with constant signal, then compare the signal with the reference value.



      $$J(u_k) = abs(R-sum(y))$$



      Where $u$ is the index variable of the current input. Then another input values is used $u = {0,1,2,3,4,5,...,u_k}$ for the constant simulation, and we get another $J$



      Then we find which $J$ values is the smallest and compute its index. The value we are going to get will be between 0 and 255.



      The procedure can be viewed like this:




      1. Simulate with $u = 0$ in 50 loops with Euler-forward

      2. Sum the output vector $y$

      3. Compare the absolut value of output vector with the reference vector $R$

      4. Repeat with $u = 1$ for 50 loops with Euler-forward

      5. .....

      6. ....


      Will that work? Or is there a better way? Like compute the unconstrained Model Predictive Controller with saturation of the input e.g 255. I only need constraints on the input. Not the states.










      share|cite|improve this question















      I'm going to implement an quadratic optimizer with C for embedded systems. I will do that because I need speed.



      But I have some trouble to find a quadratic optimizer for C that works with embedded systems. I'm using 32 bit CPU.



      This is not a question about embedded nor C. This is a question of it's possible to simulate for example an input between 0 to 255, 8-bit resolution, for 50 times each, which will be 256*50 = 12800 times.



      I have an idea of simulating a model with constant signal, then compare the signal with the reference value.



      $$J(u_k) = abs(R-sum(y))$$



      Where $u$ is the index variable of the current input. Then another input values is used $u = {0,1,2,3,4,5,...,u_k}$ for the constant simulation, and we get another $J$



      Then we find which $J$ values is the smallest and compute its index. The value we are going to get will be between 0 and 255.



      The procedure can be viewed like this:




      1. Simulate with $u = 0$ in 50 loops with Euler-forward

      2. Sum the output vector $y$

      3. Compare the absolut value of output vector with the reference vector $R$

      4. Repeat with $u = 1$ for 50 loops with Euler-forward

      5. .....

      6. ....


      Will that work? Or is there a better way? Like compute the unconstrained Model Predictive Controller with saturation of the input e.g 255. I only need constraints on the input. Not the states.







      optimization control-theory optimal-control linear-control






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Jan 6 at 17:25







      Daniel Mårtensson

















      asked Jan 6 at 17:09









      Daniel MårtenssonDaniel Mårtensson

      904316




      904316






















          1 Answer
          1






          active

          oldest

          votes


















          1














          First, it sounds like a pretty poor MPC implementation to use a control horizon of only 1.



          Besides that, finding that optimal input using brute-force enumeration on a discretized grid is both unnecessarily complicated and approximate. The solution to a scalar QP can be computed analytically by simply computing the unconstrained optimal solution (which is a linear map from the current state), and if it violates the constraints, the optimal solution is to saturate it.






          share|cite|improve this answer























          • The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
            – Daniel Mårtensson
            Jan 7 at 15:09










          • I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
            – Johan Löfberg
            Jan 7 at 16:15










          • I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
            – Daniel Mårtensson
            Jan 7 at 16:28










          • With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
            – Johan Löfberg
            Jan 7 at 16:49










          • Yes. But without knowing the states of the black box model, I cannot set constraints on them.
            – Daniel Mårtensson
            Jan 7 at 17:01











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3064123%2fa-simple-quadratic-optimizer-for-only-constraints-on-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          First, it sounds like a pretty poor MPC implementation to use a control horizon of only 1.



          Besides that, finding that optimal input using brute-force enumeration on a discretized grid is both unnecessarily complicated and approximate. The solution to a scalar QP can be computed analytically by simply computing the unconstrained optimal solution (which is a linear map from the current state), and if it violates the constraints, the optimal solution is to saturate it.






          share|cite|improve this answer























          • The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
            – Daniel Mårtensson
            Jan 7 at 15:09










          • I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
            – Johan Löfberg
            Jan 7 at 16:15










          • I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
            – Daniel Mårtensson
            Jan 7 at 16:28










          • With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
            – Johan Löfberg
            Jan 7 at 16:49










          • Yes. But without knowing the states of the black box model, I cannot set constraints on them.
            – Daniel Mårtensson
            Jan 7 at 17:01
















          1














          First, it sounds like a pretty poor MPC implementation to use a control horizon of only 1.



          Besides that, finding that optimal input using brute-force enumeration on a discretized grid is both unnecessarily complicated and approximate. The solution to a scalar QP can be computed analytically by simply computing the unconstrained optimal solution (which is a linear map from the current state), and if it violates the constraints, the optimal solution is to saturate it.






          share|cite|improve this answer























          • The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
            – Daniel Mårtensson
            Jan 7 at 15:09










          • I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
            – Johan Löfberg
            Jan 7 at 16:15










          • I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
            – Daniel Mårtensson
            Jan 7 at 16:28










          • With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
            – Johan Löfberg
            Jan 7 at 16:49










          • Yes. But without knowing the states of the black box model, I cannot set constraints on them.
            – Daniel Mårtensson
            Jan 7 at 17:01














          1












          1








          1






          First, it sounds like a pretty poor MPC implementation to use a control horizon of only 1.



          Besides that, finding that optimal input using brute-force enumeration on a discretized grid is both unnecessarily complicated and approximate. The solution to a scalar QP can be computed analytically by simply computing the unconstrained optimal solution (which is a linear map from the current state), and if it violates the constraints, the optimal solution is to saturate it.






          share|cite|improve this answer














          First, it sounds like a pretty poor MPC implementation to use a control horizon of only 1.



          Besides that, finding that optimal input using brute-force enumeration on a discretized grid is both unnecessarily complicated and approximate. The solution to a scalar QP can be computed analytically by simply computing the unconstrained optimal solution (which is a linear map from the current state), and if it violates the constraints, the optimal solution is to saturate it.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Jan 7 at 13:18

























          answered Jan 7 at 8:56









          Johan LöfbergJohan Löfberg

          5,1701811




          5,1701811












          • The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
            – Daniel Mårtensson
            Jan 7 at 15:09










          • I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
            – Johan Löfberg
            Jan 7 at 16:15










          • I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
            – Daniel Mårtensson
            Jan 7 at 16:28










          • With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
            – Johan Löfberg
            Jan 7 at 16:49










          • Yes. But without knowing the states of the black box model, I cannot set constraints on them.
            – Daniel Mårtensson
            Jan 7 at 17:01


















          • The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
            – Daniel Mårtensson
            Jan 7 at 15:09










          • I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
            – Johan Löfberg
            Jan 7 at 16:15










          • I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
            – Daniel Mårtensson
            Jan 7 at 16:28










          • With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
            – Johan Löfberg
            Jan 7 at 16:49










          • Yes. But without knowing the states of the black box model, I cannot set constraints on them.
            – Daniel Mårtensson
            Jan 7 at 17:01
















          The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
          – Daniel Mårtensson
          Jan 7 at 15:09




          The reason why I can't set some constraints on the state vector $x$ is because the state space model has been identified by subspaceidentification method. The state vector is very unknow, even if I got the state vector. Much unknow as the identified A-matrix.
          – Daniel Mårtensson
          Jan 7 at 15:09












          I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
          – Johan Löfberg
          Jan 7 at 16:15




          I'm not talking about the lack of state constraints, so I don't see what that comment refers to.
          – Johan Löfberg
          Jan 7 at 16:15












          I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
          – Daniel Mårtensson
          Jan 7 at 16:28




          I just want to explain why I'm cannot use constraints on the states, and that's the reason why I had to choose between QP + saturation on input or constrainted QP on input or that long simulation for-loop, which was quite bad suggestion from me.
          – Daniel Mårtensson
          Jan 7 at 16:28












          With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
          – Johan Löfberg
          Jan 7 at 16:49




          With or without state constraints, MPC leads to QP, and whether you can solve that on your machine has nothing to do with the lack of state constraints.
          – Johan Löfberg
          Jan 7 at 16:49












          Yes. But without knowing the states of the black box model, I cannot set constraints on them.
          – Daniel Mårtensson
          Jan 7 at 17:01




          Yes. But without knowing the states of the black box model, I cannot set constraints on them.
          – Daniel Mårtensson
          Jan 7 at 17:01


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3064123%2fa-simple-quadratic-optimizer-for-only-constraints-on-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mario Kart Wii

          The Binding of Isaac: Rebirth/Afterbirth

          What does “Dominus providebit” mean?