'float' object is not subscriptable error whehn shifting odeint to solve_ivp solver - ode

When I successfully ran the simulation by using odeint solver. But when I tried to use solve_ivp solver, it gives me an error as "'float' object is not subscriptable". Can someone help me to solve with this issue?. Highly appreciate if you can show the corrected codings to solve the problem as I am bit new to advance coding.
def tank(h,t):
c1=0.13
c2=0.20
Ac=2.0
qin=0.5
h0=h[0]
h1=h[1]
qout1=c1*h0**0.5
qout2=c2*h1**0.5
dhdt1=(qin-qout1)/Ac
dhdt2=(qout1-qout2)/Ac
dhdt=[dhdt1,dhdt2]
return dhdt
t0=0
tfinal=100
tspan=np.arange(0,tfinal+1)
y0=np.array([0,0])
# t=np.linspace(0,10,21)
# y=odeint(tank,y0,t,)
y=solve_ivp(tank,(t0,tfinal),y0,t_eval=tspan)
print(y)
plt.plot(t,y[:,0],'b-')
plt.plot(t,y[:,1],'r--')
plt.show()

Related

How to set iteration limit for subsolver ipopt in mindtpy using pyomo?

i am new with using pyomo and mindtpy solver, and have a very basic question.
how can i set the iteration limit for a subsolver in mindtpy? I want to set the iteration limit of ipopt, but it seems like it is not possible.
I treid the following code:
result = opt.solve(model, strategy='OA', init_strategy='FP', iteration_limit=100000, time_limit=36000, constraint_tolerance=10e-5, fp_iteration_limit=10000, fp_mipgap=10e-3, mip_solver='cplex', heuristic_nonconvex=True, mip_solver_args={'timelimit': 36000, 'warmstart': True}, mip_solver_mipgap=0.001, nlp_solver='ipopt', nlp_solver_args={'timelimit': 36000, 'maxiter': 10000}, tee=True, mip_solver_tee=True, nlp_solver_tee=True)
Setting the timelimit works and iteration limit not! I also tried it with iteration_limit instead of maxiter or max_iter.
I always get the error: ValueError: ProblemWriter_nl passed unrecognized io_options: maxiter = 10000
Thank you very much!
Kind regards

AttributeError: 'Image' object has no attribute 'save'

I am following the basic tutorial for RDKit.
p = Chem.MolFromSmiles('[nH]1cnc2cncnc21')
subms = [x for x in ms if x.HasSubstructMatch(p)]
len(subms)
AllChem.Compute2DCoords(p)
for m in subms: AllChem.GenerateDepictionMatching2DStructure(m,p)
img=Draw.MolsToGridImage(subms,molsPerRow=4,subImgSize=(200,200),legends=[x.GetProp("_Name") for x in subms])
img.save('images/cdk2_molgrid.aligned.o.png')
My version looks like this:
NP=pd.read_excel(r'C:\Users\BajMic\NPPics.xlsx', header=0, index_col=False, keep_default_na=True)
NP['mol']=NP.smiles.apply(getMol)
ms= [i for i in NP['mol'] if i is not None]
img=Draw.MolsToGridImage(ms, molsPerRow=10)
img.save('Pic.png')
In both cases, the tutorial and my own code, I get the same error:
AttributeError: 'Image' object has no attribute 'save'
Now, this is confusing, because I was just following a simple tutorial. I looked at other similar topic, but I think my case is much more trivial. What am I missing here?
Tutorial: https://www.rdkit.org/docs/GettingStartedInPython.html
Although in MolsToGridImage() default is returnPNG=False, I have to set it explicit in the function and it works for me.
You should try: img=Draw.MolsToGridImage(ms, molsPerRow=10, returnPNG=False)

GridSearch with XGBoost producing Depreciation error on infinite loop

I am trying to do a hyperparameter tuning using GridSearchCV on XGBoost.But, I'm getting the following error.
/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.if diff:
This keeps on running forever. Given below is the code.
classifier = xgb.XGBClassifier()
from sklearn.grid_search import GridSearchCV
n_estimators=[10,50,100,150,200,250,300]
max_depth=[2,3,4,5,6,7,8,9,10]
learning_rate=[0.1,0.01,0.09,0.08,0.07,0.001]
colsample_bytree=[0.5,0.6,0.7,0.8,0.9]
min_child_weight=[1,2,3,4,5,6,7,8,9,10]
gamma=[0.001,0.01,0.1,0.2,0.3,0.4,0.5,1]
subsample=[0.5,0.6,0.7,0.8,0.9]
param_grid=dict(n_estimators=n_estimators,max_depth=max_depth,learning_rate=learning_rate,colsample_bytree=colsample_bytree,min_child_weight=min_child_weight,gamma=gamma,subsample=subsample)
grid = GridSearchCV(classifier, param_grid, cv=10, scoring='accuracy')
grid.fit(X, Y)
grid.grid_scores_
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
# Predicting the Test set results
Y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, Y_pred)
I am using python3.5, XGBOOT and gridsearch library has already been preloaded. I am running this on google collaboratory.
Please suggest what is going wrong ?

Python ode first order, how to solve this using Sympy

When I try to solve this first ode by using Sympy as it shows below:
import sympy
y = sympy.Function('y')
t = sympy.Symbol('t')
ode = sympy.Eq(y(t).diff(t),(1/y(t))*sympy.sin(t))
sol = sympy.dsolve(ode,y(t))
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
ode_sol= sol.subs([(csol.rhs,csol.lhs)])
print(sympy.pprint(ode_sol))
It gives me this error:
Traceback (most recent call last):
File "C:/Users/Mohammed Alotaibi/AppData/Local/Programs/Python/Python35/ODE2.py", line 26, in <module>
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
AttributeError: 'list' object has no attribute 'subs'
Your problem is that this ODE does not have a unique solution. Thus it returns a list of solution, which you can find out from the error message and by printing sol.
Do the evaluation in a loop,
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
ode_sol = psol.subs([(csol.rhs,csol.lhs)]);
print(sympy.pprint(ode_sol))
to find the next error, that substituting does not solve for the constant. What works is to define C1=sympy.Symbol("C1") and using
ode_sol= psol.subs([(C1, sympy.solve(csol)[0])]);
but this still feels hacky. Or better to avoid error messages for the unsolvability of the second case:
C1=sympy.Symbol("C1");
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
for cc1 in sympy.solve(csol):
ode_sol= psol.subs([(C1, cc1)]);
print(sympy.pprint(ode_sol))

prolog get syntax error when increase stack size

Trying to solve the puzzle task with prolog and got some problems.
1002 Stack Overflow. Re-configure with Setup if necessary.
So, I've tried to increase stack size in setup and run program again.
But it causes the other error: Syntax error on line...
The error line is line with operator "not" in predicate.
Here is my code:
domains
age = integer
childname,ffood,fear = string
child = child(childname,age,ffood,fear)
children = child*
predicates
solve
name(child,childname)
fear(child,fear)
age(child,age)
ffood(child,ffood)
keys(children)
solution(children)
elder(child)
member(children,child)
structure(children)
clauses
member([X|_],X).
member([_|SP],X):-member(SP,X).
name(child(A,_,_,_),A).
age(child(_,A,_,_),A).
ffood(child(_,_,A,_),A).
fear(child(_,_,_,A),A).
structure([child("Dima",_,_,_),child("Kate",_,_,_),child("Misha",_,_,_),child("Sveta",_,_,_),child("Ura",_,_,_)]).
elder(child(_,A,_,_)):-A=7;A=8.
solve:-structure(Children),keys(Children),solution(Children).
keys(Struct):-
member(Struct,child(_,4,_,_)),
member(Struct,child(_,5,_,_)),
member(Struct,child(_,6,_,_),
member(Struct,child(_,7,_,_),
member(Struct,child(_,8,_,_)),
member(Struct,child(_,_,"Banana",_),
member(Struct,child(_,_,"Icecream",_),
member(Struct,child(_,_,"Pizza",_),
member(Struct,child(_,_,"Pasta",_),
member(Struct,child(_,_,"Chocolate",_),
member(Struct,child(_,_,_,"Thunderstorm"),
member(Struct,child(_,_,_,"Spiders"),
member(Struct,child(_,_,_,"Ghosts"),
member(Struct,child(_,_,_,"Dogs"),
member(Struct,child(_,_,_,"Darkness"),
member(Struct,Child1),
name(Child1,"Kate"),
elder(Child1),
not(fear(Child1,"Darkness")),
not(ffood(Child1,"Chocolate")),
member(Struct,Child2),
name(Child2,"Sveta"),
elder(Child2),
not(fear(Child2,"Darkness")),
not(ffood(Child2,"Chocolate")),
ffood(Child2,"Pizza"),
not(fear(Child2,"Spiders")),
member(Struct,Child3),
age(Child3,5),
fear(Child3,"Ghosts"),
member(Struct,Child4),
age(Child4,6),
fear(Child4,"Thunderstorm"),
not(ffood(Child4,"Chocolate")),
not(ffood(Child4,"Pasta")),
member(Struct,Child5),
age(Child5,4),
ffood(Child5,"Banana"),
member(Struct,Child6),
age(Child6,8),
not(fear(Child6,"Dogs")),
member(Struct,Child7),
name(Child7,"Dima"),
not(age(Child7,5)),
not(fear(Child7,"Darkness")),
not(fear(Child7,"Spiders")),
not(ffood(Child7,"Banana")),
member(Struct,Child8),
name(Child8,"Misha"),
not(fear(Child8,"Darkness")),
not(fear(Child8,"Spiders")),
not(ffood(Child8,"Banana")).
solution (Children):-
write ("Solve:"), write (Children).
goal
solve.
Found strange this prolog behavior... maybe somebody had the same problem?
I tried to reformat your code with SWI-Prolog:
keys(Struct):-
member(Struct,child(_,4,_,_)),
member(Struct,child(_,5,_,_)),
member(Struct,child(_,6,_,_),
member(Struct,child(_,7,_,_),
member(Struct,child(_,8,_,_)),
member(Struct,child(_,_,"Banana",_),
...
seems you're missing some parenthesis...
after the obvious correction, I get
?- solve.
Solve:[child(Dima,6,Icecream,Thunderstorm),child(Kate,8,Pasta,Spiders),child(Misha,5,Chocolate,Ghosts),child(Sveta,7,Pizza,Dogs),child(Ura,4,Banana,Darkness)]
true .

Resources