how to use arrays in gekko optimizer for python











up vote
0
down vote

favorite












I tried to convert an example from gekko python optimizer by using the list, array x instead of variables x1..x4. This is the code which gives the result, but I think it is not correct



from gekko import GEKKO
import numpy as np
# Initialize Model
m = GEKKO(remote=False)

#help(m)

#define parameter
eq = m.Param(value=40)

#initialize variables
x = [m.Var(value=1,lb=1,ub=5) for i in range(4)]
x[1].value=5
x[2].value=5

#Equations
m.Equation(np.prod([x[i] for i in range(0,4)])>=25)
m.Equation(np.sum([x[i]**2 for i in range(0,4)])==eq)

#Objective
m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])

#Set global options
m.options.IMODE = 3 #steady state optimization

#Solve simulation
m.solve() # solve on public server

#Results
print('')
print('Results')
print('x1: ' + str(x[0].value))
print('x2: ' + str(x[1].value))
print('x3: ' + str(x[2].value))
print('x4: ' + str(x[3].value))


Please anyone could help me out how to use list, array of variables in gekko. This seems to me less elegant and I was wondering is there is a way of using Array() function instead of Var(). I can not figure out how and when we can use Array() function.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I tried to convert an example from gekko python optimizer by using the list, array x instead of variables x1..x4. This is the code which gives the result, but I think it is not correct



    from gekko import GEKKO
    import numpy as np
    # Initialize Model
    m = GEKKO(remote=False)

    #help(m)

    #define parameter
    eq = m.Param(value=40)

    #initialize variables
    x = [m.Var(value=1,lb=1,ub=5) for i in range(4)]
    x[1].value=5
    x[2].value=5

    #Equations
    m.Equation(np.prod([x[i] for i in range(0,4)])>=25)
    m.Equation(np.sum([x[i]**2 for i in range(0,4)])==eq)

    #Objective
    m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])

    #Set global options
    m.options.IMODE = 3 #steady state optimization

    #Solve simulation
    m.solve() # solve on public server

    #Results
    print('')
    print('Results')
    print('x1: ' + str(x[0].value))
    print('x2: ' + str(x[1].value))
    print('x3: ' + str(x[2].value))
    print('x4: ' + str(x[3].value))


    Please anyone could help me out how to use list, array of variables in gekko. This seems to me less elegant and I was wondering is there is a way of using Array() function instead of Var(). I can not figure out how and when we can use Array() function.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I tried to convert an example from gekko python optimizer by using the list, array x instead of variables x1..x4. This is the code which gives the result, but I think it is not correct



      from gekko import GEKKO
      import numpy as np
      # Initialize Model
      m = GEKKO(remote=False)

      #help(m)

      #define parameter
      eq = m.Param(value=40)

      #initialize variables
      x = [m.Var(value=1,lb=1,ub=5) for i in range(4)]
      x[1].value=5
      x[2].value=5

      #Equations
      m.Equation(np.prod([x[i] for i in range(0,4)])>=25)
      m.Equation(np.sum([x[i]**2 for i in range(0,4)])==eq)

      #Objective
      m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])

      #Set global options
      m.options.IMODE = 3 #steady state optimization

      #Solve simulation
      m.solve() # solve on public server

      #Results
      print('')
      print('Results')
      print('x1: ' + str(x[0].value))
      print('x2: ' + str(x[1].value))
      print('x3: ' + str(x[2].value))
      print('x4: ' + str(x[3].value))


      Please anyone could help me out how to use list, array of variables in gekko. This seems to me less elegant and I was wondering is there is a way of using Array() function instead of Var(). I can not figure out how and when we can use Array() function.










      share|improve this question















      I tried to convert an example from gekko python optimizer by using the list, array x instead of variables x1..x4. This is the code which gives the result, but I think it is not correct



      from gekko import GEKKO
      import numpy as np
      # Initialize Model
      m = GEKKO(remote=False)

      #help(m)

      #define parameter
      eq = m.Param(value=40)

      #initialize variables
      x = [m.Var(value=1,lb=1,ub=5) for i in range(4)]
      x[1].value=5
      x[2].value=5

      #Equations
      m.Equation(np.prod([x[i] for i in range(0,4)])>=25)
      m.Equation(np.sum([x[i]**2 for i in range(0,4)])==eq)

      #Objective
      m.Obj(x[0]*x[3]*(x[0]+x[1]+x[2])+x[2])

      #Set global options
      m.options.IMODE = 3 #steady state optimization

      #Solve simulation
      m.solve() # solve on public server

      #Results
      print('')
      print('Results')
      print('x1: ' + str(x[0].value))
      print('x2: ' + str(x[1].value))
      print('x3: ' + str(x[2].value))
      print('x4: ' + str(x[3].value))


      Please anyone could help me out how to use list, array of variables in gekko. This seems to me less elegant and I was wondering is there is a way of using Array() function instead of Var(). I can not figure out how and when we can use Array() function.







      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 21:40

























      asked Oct 23 at 8:54









      Radovan Omorjan

      215




      215
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          This one will work as well.



          #Equations
          m.Equation(np.prod(np.asarray(x))>=25)
          m.Equation(np.sum(np.asarray(x)**2)==eq)





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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',
            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
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52944970%2fhow-to-use-arrays-in-gekko-optimizer-for-python%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








            up vote
            1
            down vote













            This one will work as well.



            #Equations
            m.Equation(np.prod(np.asarray(x))>=25)
            m.Equation(np.sum(np.asarray(x)**2)==eq)





            share|improve this answer



























              up vote
              1
              down vote













              This one will work as well.



              #Equations
              m.Equation(np.prod(np.asarray(x))>=25)
              m.Equation(np.sum(np.asarray(x)**2)==eq)





              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                This one will work as well.



                #Equations
                m.Equation(np.prod(np.asarray(x))>=25)
                m.Equation(np.sum(np.asarray(x)**2)==eq)





                share|improve this answer














                This one will work as well.



                #Equations
                m.Equation(np.prod(np.asarray(x))>=25)
                m.Equation(np.sum(np.asarray(x)**2)==eq)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 21:41

























                answered Nov 10 at 20:07









                Radovan Omorjan

                215




                215






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52944970%2fhow-to-use-arrays-in-gekko-optimizer-for-python%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

                    Bressuire

                    Vorschmack

                    Quarantine