How to handle infinite derivatives in scipy odeint? - python-3.x

I have a system of coupled ODEs that I'm trying to solve as an initial value problem using scipy.integrate.odeint. However, every time I run odeint, I get the error
lsoda-- at t(=r1) and step size h(=r2), the error
test failed repeatedly or with abs(h) = hmin
in above, r1 = 0.2351321589734D+00 r2 = 0.1000000000000D-05
ODEintWarning: Repeated error test failures (internal error). Run with full_output = 1 to get quantitative information.
warnings.warn(warning_msg, ODEintWarning)
I'm pretty sure I know the reason for this. The value of one of my derivatives goes to infinity at the point where the error is coming about. However, this is unavoidable in the problem I'm trying to solve.
I am wondering if there is a way to make odeint behave in a more civilised way when it gets to this infinity rather than just refusing to work and producing a nonsense output (shown in the picture) scipy getting upset.
Cheers in advance!

Related

Ortools setting constraints while solving

I am using CP-SAT solver from ortools
https://developers.google.com/optimization/cp/cp_solver
I am executing the solver with a callback object
solver = cp_model.CpSolver()
solution_agg = SolutionCollector(data, self.variables, self.products, self.vehicles)
status = solver.SearchForAllSolutions(self.model, callback=solution_agg)
Solution agg is supposed to filter out all solutions that have some wrong assignments, I could not model these as linear inequalities.
What I know is that generated solutions can be converged faster and the "hits" on the verifier can be made less. If I can add constraints on-the-go inside the callback.
I tried to do this inside the callback, adding a constraint to look for solutions in lesser volume than minimum volume till now.
self.__model.Add(volume_expression <= min_found_yet)
This doesn't give error, but the number of times the verifier rejected a solution is still same.
Is it possible to form constraints while solving? If not in Ortools, then any other solver which provides?
Not directly. The solver is stateless and read the cp_model once at the start of the solve.
What you are describing seems just a minimization property. Why don't you just minimize volume_expression?

Pyomo Optimization of minimum cost using binary variables

I have an optimization problem where I want to minimize for the total cost of a system, so I write an objective function that is the sum of my different costs. The problem includes using one of three machines each one with different cost at a different threshold of usage. I define each machine (model.Machine#) as a binary variable and declare the parameters of each machine cost model.Cost#). I am trying to get the cost to be able to minimize it but when I write the constraint:
model.Cost1*model.Machine1 + model.Cost2*model.Machine2 + model.Cost3*model.Machine3 == model.MachineCost
Where I also write:
model.Machine1 + model.Machine2 + model.Machine3 == 1
Gurobi is telling me that it can't handle an quadratic function referring to the first constraint mentioned above. However it is parameters multiplied by binary variables there isn't anything quadratic.
I know the question is vague and part of a larger problem but I hope you can understand what I am referring to and help me!
Thank you so much for your assistance!
What is model.MachineCost? Is it an Expression component with some kind of quadratic expression stored inside of it?
If not, can you start commenting out things in your model until you get down to a minimal working example (that causes this error) and post that? Otherwise, we can't be sure that there are not other quadratic pieces of the model that you are not showing.

Force Gurobi MIP solve to be deterministic

I am passing a MIP problem to Gurobi using PULP, with no special params other than gap 0.1 and it looks like the solution path is different every solve. I tried to set a seed, and use that same seed for each run, and that did not help. It still took a different journey. I know it's taking a dif path, because I am doing row generation (getting a solution, checking if a constraint is broken, adding if so) and its finding a feasible optimal one run without needing, for example, constraint x - but then the very next run with the exact same input, it breaks constraint x and requires it be added as a row.
To those who think it may be due to the gap of 0.1, maybe so, but CBC solves the same way with the same gap and does not exhibit this behavior.
Any ideas? Thanks,

error in function -not GIS - not 2D spatial, which arguments of spatstat

I am using spatstat in R to generate points on a linear network. I had successfully generated a poisson distributed poisson process on a road network.
Now when I want to use the 'nsim', to have more than one realization of the random process, I get an error (using this data) http://biogeo.ucdavis.edu/data/diva/rds/NLD_rds.zip
roads<-readShapeSpatial("NL_rd.shp")
spatstat.roads<-as.psp(roads)
final_roads<-as.linnet(spatstat.roads)
abc<-rpoislpp(0.02/5000,final_roads, nsim=2)
plot(abc)
when I use that nsim, I get an error as follows
Error in (function (..., check = TRUE, promote = TRUE, demote = FALSE) :
Some arguments of solist() are not 2D spatial objects
While it works for one realization, i wanted to draw two realizations to visually compare the randomness. So i not understand how one realization can be arrived but not two and the error talks of non-2D spatial objects. it is a little cryptic for me.
Could you please clarify on this error and how to resolve it.
Thanks.
This is a bug.
It will be fixed shortly in the development version of spatstat, version 1.47-0.018 and later.
In the meantime you can get the desired result by using replicate, e.g.
abc <- replicate(2, rpoislpp(0.02/5000,final_roads), simplify=FALSE)
Thank you for drawing this to our attention.

VBA Exp() Overflow

Here's a very easy question for the VBA guys. I'm running Newton's method on some functions, and occasionally I find a guess that I can only assume overflows the Exp() function (and stops the code). What suggestions do you guys have to simply handle this case? (Maybe some sort of error handling?)
If Newton's method fails because of this or any sort of blowup, I would like to proceed onto my bisection code below that point.
By the way, I have thought about maybe taking logs to make this situation less likely, but to be honest I am working with some math I do not yet completely understand, and I would like to handle the case of Newton's method failing in any case first.
Disclaimer: I'm a complete VBA beginner, so any suggestions would be read and appreciated. Thanks in advance.
Edit: I've been asked to post the code. First, thanks for reading. Unfortunately, I can't post the entire code due to business reasons, but I can give the very barebones outline. I've created a module and a function. Inside this function, I have:
Newtons Method Loop
Bisection Loop
Inside the Newton's method loop, I've traced to a point where I have a next guess of something around 28,000 or so, and I am assigning to a variable h the value Exp(28,000) or roundabouts. The debugger breaks at that point; my code essentially exits, and whatever value my function should be returning produces #VALUE! in my cell.
I know this is not a lot of information, but I hope (and think) it should be enough. Correct me if I am wrong.
Edit 2: If all else fails, I'm going to explicitly catch too large values, but I wonder if there is a more robust and elegant solution.
Not surprising it will overflow given that Exp(28,000) is 1.8x1012160, the maximum value you can pass to Exp is ~709.
If you want to exit your loop if you encounter a value that is too large, just check the value before passing it;
function Newton
const MAX_EXP_ARGUMENT as double = 709.782712893#
do ....
if (abs(var) <= MAX_EXP_ARGUMENT) then
r = exp(var)
else
exit do '// exit the loop
end if
'//use r
loop
do ....
There was a SO issue posted a while back dealing with numbers larger than Long in VBA.
The accepted answer pointed to a link called Large Number Arithmetic. I just tried to implement your example of exp(28000) using that example, but received a "Type Mismatch" error after processing a few loops. However, it may be the fault of my hasty implementation. If you've got no leads so far, I would start there.

Resources