python, iteration limit exceeded











up vote
0
down vote

favorite












It is an easy problem but I tried many times and couldn't find where there is a bug in my code.
Assume there is a Force to push the mass to a goal point. Use the Optimizer to find the best position, velocity, force.
Constraint 1 is used to describe a position relationship, contraint2 is used to describe a velocity relationship.
the code is as follows:



import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt

t=0.1
m=10
g=9.8
s_goal=3
n=80
e=0.01

A = np.zeros((n,n))
for i in range (n):
A[0,i]=0
A[i,i-1]=1
B = np.zeros((n,n))
for i in range (n):
B[0,i]=0
B[i,i-1]=t
C = np.zeros((n,1))
for i in range (n):
C[i,0]=t*g

x0=np.zeros((n,3))

def constraint1(x):
x=x.reshape(n,3)
s=x[:,0].reshape(n,1)
v=x[:,1].reshape(n,1)
a=s-np.dot(A,s)-np.dot(B,v)
a=a.reshape(n,)
return a
def constraint2(x):
x=x.reshape(n,3)
F=x[:,2].reshape(n,1)
v=x[:,1].reshape(n,1)
b=v-np.dot(A,v)-(t/m)*np.dot(A,F)+C
b=b.reshape(n,)
return b
def objective(x):
x=x.reshape(n,3)
s=x[:,0].reshape(n,1)
v=x[:,1].reshape(n,1)
#F=x[:,2].reshape(n,1)
sum_up=0
for i in range (n):
sum_up = sum_up + (s[i,0]-s_goal)**2 + e*(v[i,0])**2
#print(sum_up )
return sum_up
# optimize
con1 = {'type':'eq','fun':constraint1}
con2 = {'type':'eq','fun':constraint2}
cons = ([con1,con2])
solution = minimize(objective,x0,method='SLSQP',constraints=cons)
print(solution)
#print (np.shape(solution.x))
m=(solution.x).reshape(n,3)

position=m[:,0]
velocity=m[:,1]
Force=m[:,2]
#plot
tn=np.linspace(0,n*t,n)
plt.plot(tn,position)
plt.ylabel('position')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()


plt.plot(tn,velocity)
plt.ylabel('velocity')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()


plt.plot(tn,Force)
plt.ylabel('Force')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()


I think the constraints are clear, but the result is saying:
message: 'Iteration limit exceeded'
nfev: 24442
nit: 101
njev: 101
status: 9
success: False



Could you please tell me where did I do wrong. Thank you Very much!!!










share|improve this question







New contributor




Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    It is an easy problem but I tried many times and couldn't find where there is a bug in my code.
    Assume there is a Force to push the mass to a goal point. Use the Optimizer to find the best position, velocity, force.
    Constraint 1 is used to describe a position relationship, contraint2 is used to describe a velocity relationship.
    the code is as follows:



    import numpy as np
    from scipy.optimize import minimize
    import matplotlib.pyplot as plt

    t=0.1
    m=10
    g=9.8
    s_goal=3
    n=80
    e=0.01

    A = np.zeros((n,n))
    for i in range (n):
    A[0,i]=0
    A[i,i-1]=1
    B = np.zeros((n,n))
    for i in range (n):
    B[0,i]=0
    B[i,i-1]=t
    C = np.zeros((n,1))
    for i in range (n):
    C[i,0]=t*g

    x0=np.zeros((n,3))

    def constraint1(x):
    x=x.reshape(n,3)
    s=x[:,0].reshape(n,1)
    v=x[:,1].reshape(n,1)
    a=s-np.dot(A,s)-np.dot(B,v)
    a=a.reshape(n,)
    return a
    def constraint2(x):
    x=x.reshape(n,3)
    F=x[:,2].reshape(n,1)
    v=x[:,1].reshape(n,1)
    b=v-np.dot(A,v)-(t/m)*np.dot(A,F)+C
    b=b.reshape(n,)
    return b
    def objective(x):
    x=x.reshape(n,3)
    s=x[:,0].reshape(n,1)
    v=x[:,1].reshape(n,1)
    #F=x[:,2].reshape(n,1)
    sum_up=0
    for i in range (n):
    sum_up = sum_up + (s[i,0]-s_goal)**2 + e*(v[i,0])**2
    #print(sum_up )
    return sum_up
    # optimize
    con1 = {'type':'eq','fun':constraint1}
    con2 = {'type':'eq','fun':constraint2}
    cons = ([con1,con2])
    solution = minimize(objective,x0,method='SLSQP',constraints=cons)
    print(solution)
    #print (np.shape(solution.x))
    m=(solution.x).reshape(n,3)

    position=m[:,0]
    velocity=m[:,1]
    Force=m[:,2]
    #plot
    tn=np.linspace(0,n*t,n)
    plt.plot(tn,position)
    plt.ylabel('position')
    plt.xlabel('time')
    plt.legend(loc='best')
    plt.show()


    plt.plot(tn,velocity)
    plt.ylabel('velocity')
    plt.xlabel('time')
    plt.legend(loc='best')
    plt.show()


    plt.plot(tn,Force)
    plt.ylabel('Force')
    plt.xlabel('time')
    plt.legend(loc='best')
    plt.show()


    I think the constraints are clear, but the result is saying:
    message: 'Iteration limit exceeded'
    nfev: 24442
    nit: 101
    njev: 101
    status: 9
    success: False



    Could you please tell me where did I do wrong. Thank you Very much!!!










    share|improve this question







    New contributor




    Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      It is an easy problem but I tried many times and couldn't find where there is a bug in my code.
      Assume there is a Force to push the mass to a goal point. Use the Optimizer to find the best position, velocity, force.
      Constraint 1 is used to describe a position relationship, contraint2 is used to describe a velocity relationship.
      the code is as follows:



      import numpy as np
      from scipy.optimize import minimize
      import matplotlib.pyplot as plt

      t=0.1
      m=10
      g=9.8
      s_goal=3
      n=80
      e=0.01

      A = np.zeros((n,n))
      for i in range (n):
      A[0,i]=0
      A[i,i-1]=1
      B = np.zeros((n,n))
      for i in range (n):
      B[0,i]=0
      B[i,i-1]=t
      C = np.zeros((n,1))
      for i in range (n):
      C[i,0]=t*g

      x0=np.zeros((n,3))

      def constraint1(x):
      x=x.reshape(n,3)
      s=x[:,0].reshape(n,1)
      v=x[:,1].reshape(n,1)
      a=s-np.dot(A,s)-np.dot(B,v)
      a=a.reshape(n,)
      return a
      def constraint2(x):
      x=x.reshape(n,3)
      F=x[:,2].reshape(n,1)
      v=x[:,1].reshape(n,1)
      b=v-np.dot(A,v)-(t/m)*np.dot(A,F)+C
      b=b.reshape(n,)
      return b
      def objective(x):
      x=x.reshape(n,3)
      s=x[:,0].reshape(n,1)
      v=x[:,1].reshape(n,1)
      #F=x[:,2].reshape(n,1)
      sum_up=0
      for i in range (n):
      sum_up = sum_up + (s[i,0]-s_goal)**2 + e*(v[i,0])**2
      #print(sum_up )
      return sum_up
      # optimize
      con1 = {'type':'eq','fun':constraint1}
      con2 = {'type':'eq','fun':constraint2}
      cons = ([con1,con2])
      solution = minimize(objective,x0,method='SLSQP',constraints=cons)
      print(solution)
      #print (np.shape(solution.x))
      m=(solution.x).reshape(n,3)

      position=m[:,0]
      velocity=m[:,1]
      Force=m[:,2]
      #plot
      tn=np.linspace(0,n*t,n)
      plt.plot(tn,position)
      plt.ylabel('position')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      plt.plot(tn,velocity)
      plt.ylabel('velocity')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      plt.plot(tn,Force)
      plt.ylabel('Force')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      I think the constraints are clear, but the result is saying:
      message: 'Iteration limit exceeded'
      nfev: 24442
      nit: 101
      njev: 101
      status: 9
      success: False



      Could you please tell me where did I do wrong. Thank you Very much!!!










      share|improve this question







      New contributor




      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      It is an easy problem but I tried many times and couldn't find where there is a bug in my code.
      Assume there is a Force to push the mass to a goal point. Use the Optimizer to find the best position, velocity, force.
      Constraint 1 is used to describe a position relationship, contraint2 is used to describe a velocity relationship.
      the code is as follows:



      import numpy as np
      from scipy.optimize import minimize
      import matplotlib.pyplot as plt

      t=0.1
      m=10
      g=9.8
      s_goal=3
      n=80
      e=0.01

      A = np.zeros((n,n))
      for i in range (n):
      A[0,i]=0
      A[i,i-1]=1
      B = np.zeros((n,n))
      for i in range (n):
      B[0,i]=0
      B[i,i-1]=t
      C = np.zeros((n,1))
      for i in range (n):
      C[i,0]=t*g

      x0=np.zeros((n,3))

      def constraint1(x):
      x=x.reshape(n,3)
      s=x[:,0].reshape(n,1)
      v=x[:,1].reshape(n,1)
      a=s-np.dot(A,s)-np.dot(B,v)
      a=a.reshape(n,)
      return a
      def constraint2(x):
      x=x.reshape(n,3)
      F=x[:,2].reshape(n,1)
      v=x[:,1].reshape(n,1)
      b=v-np.dot(A,v)-(t/m)*np.dot(A,F)+C
      b=b.reshape(n,)
      return b
      def objective(x):
      x=x.reshape(n,3)
      s=x[:,0].reshape(n,1)
      v=x[:,1].reshape(n,1)
      #F=x[:,2].reshape(n,1)
      sum_up=0
      for i in range (n):
      sum_up = sum_up + (s[i,0]-s_goal)**2 + e*(v[i,0])**2
      #print(sum_up )
      return sum_up
      # optimize
      con1 = {'type':'eq','fun':constraint1}
      con2 = {'type':'eq','fun':constraint2}
      cons = ([con1,con2])
      solution = minimize(objective,x0,method='SLSQP',constraints=cons)
      print(solution)
      #print (np.shape(solution.x))
      m=(solution.x).reshape(n,3)

      position=m[:,0]
      velocity=m[:,1]
      Force=m[:,2]
      #plot
      tn=np.linspace(0,n*t,n)
      plt.plot(tn,position)
      plt.ylabel('position')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      plt.plot(tn,velocity)
      plt.ylabel('velocity')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      plt.plot(tn,Force)
      plt.ylabel('Force')
      plt.xlabel('time')
      plt.legend(loc='best')
      plt.show()


      I think the constraints are clear, but the result is saying:
      message: 'Iteration limit exceeded'
      nfev: 24442
      nit: 101
      njev: 101
      status: 9
      success: False



      Could you please tell me where did I do wrong. Thank you Very much!!!







      python-3.x






      share|improve this question







      New contributor




      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 12 at 17:30









      Katherine

      1




      1




      New contributor




      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Katherine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          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
          });


          }
          });






          Katherine is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267258%2fpython-iteration-limit-exceeded%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Katherine is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Katherine is a new contributor. Be nice, and check out our Code of Conduct.













          Katherine is a new contributor. Be nice, and check out our Code of Conduct.












          Katherine is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267258%2fpython-iteration-limit-exceeded%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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents