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!!!
python-3.x
New contributor
add a comment |
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!!!
python-3.x
New contributor
add a comment |
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!!!
python-3.x
New contributor
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
python-3.x
New contributor
New contributor
New contributor
asked Nov 12 at 17:30
Katherine
1
1
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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