Does Julia have a way to solve for unknown variables - excel

Is there a function in Julia that is similar to the solver function in Excel where I can provide and equation, and it will solve for the unknown variable? If not, does anybody know the math behind Excel's solver function?
I am not expecting anybody to solve the equation, but if it helps:
Price = (Earnings_1/(1+r)^1)+(Earnings_2/(1+r)^2)++(Earnings_3/(1+r)^3)+(Earnings_4/(1+r)^4)+(Earnings_5/(1+r)^5)+(((Earnings_5)(RiskFreeRate))/((1+r)^5)(1-RiskFreeRate))
The known variables are: Price, All Earnings, and RiskFreeRate. I am just trying to figure out how to solve for r.

Write this instead as an expression f(r) = 0 by subtracting Price over to the other side. Now it's a rootfinding problem. If you only have one variable you're solving for (looks to be the case), then Roots.jl is a good choice.
fzero(f, a::Real, b::Real)
will search for a solution between a and b for example, and the docs have more choices for algorithms when you don't know a range to start with and only give an initial condition for example.
In addition, KINSOL in Sundials.jl is good when you know you're starting close to a multidimensional root. For multidimensional and needing some robustness to the initial condition, I'd recommend using NLsolve.jl.

There's nothing out of the box no. Root finding is a science in itself.
Luckily for you, your function has an analytic first derivative with respect to r. That means that you can use Newton Raphson, which will be extremely stable for your function.
I'm sure you're aware your function behaves badly around r = -1.

Related

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.

Computing numeric derivatives with pyomo

I need to repeatedly compute derivatives at a given point of nonlinear pyomo constraints. According to this post, there are basically two options: one symbolic approach (which uses sympy) and one numeric approach via the NL file generated by Pyomo.
I have tried the symbolic approach which looks like:
def gradient(constr, variables):
grad_num = np.array([value(partial) for partial in
differentiate(constr.body, wrt_list=variables)])
if (not (is_leq_constr(constr))):
grad_num = -grad_num
return grad_num
In principle, this approach works. However, when dealing with larger problems, the function call takes very long and I would expect the numeric approach to be much faster.
Is this true? If so, can anyone help me, to "translate" the above function to the gjh_asl_json approach? I am grateful for any hint.

PySCIPOpt/SCIP - Branching/Separation with fractional variable

I began using PySCIPOpt/SCIP for a Coursera course on discrete optimization. I'd need to implement a simple separation from a fractional variable and wonder how to do it. Online SCIP literature does not provide relevant example.
Any Python example for me to get inspired for my assignment?
Thank you for the answer. Indeed I spent some hours reading SCIP documentation and I have trouble interfacing SCIp methods in Python.
I have been able to implement in Python a simple constraint handler to add first-type cuts and I'd like to add a separator to add second-type cuts.
The latter cuts are typically x = 0 or 1 cuts based on fractional x values and I stumble more with syntax - addCut() - and using generic methods than the process itself.
A Python example, a bit more involved than tsp.py, would greatly help me.
Your question is quite broad. I try to give some hints on where to look for an answer:
general information on separators in SCIP
difference between constraint handlers and separators
Python example on a separator implemented as constraint handler to solve the Traveling Salesman Problem
I can try to answer your question more precisely, if you explain your application/problem in more detail.

excel solver (Simplex LP) binary constraints

I am solving an optimization problem. the problem has binary constraints. solver is (during iteration) setting those binary constraints to decimals between 0 and 1 (approximating a relaxed gradient search). I wish to indicate to solver that it should just search over the discontinous values for 0..1.
Is there a way to do this?
Alternatively, is there an algorithm in OpenSolver which does this, that mimics the simplex-lp, and provides a global optimum?
the cheap way to do it, is to right a for-loop, and iterate over the values. I was wondering if there was a way to phrase it so that a nonlinear problem, becomes a linear problem.
Thanks.
The GRG Nonlinear and Simplex LP methods both use the Branch & Bound method when faced with integer constraints. This method "relaxes" the integer requirement first, finds a solution, then fixes one of the constraints to an integer and finds a new solution. See the Solver on-line documentation.
It is a brute force search method and can take a considerable amount of time.
The Evolutionary method uses it's own algorithm for dealing with integer constraints and is typically much faster than the other two methods.
You ask about linearizing a non-linear problem - you would need to provide more specific information in order to answer that (e.g. What is your equation? How have you set up your solver problem? etc.)

Excel Solver Curve Fitting Failing - MatLab recast

I am having some strange problems with excel's solver. Basically what I am trying to do is curve fit my data. I have two different lines, one is my calibration line and the other is the derived line that I am attempting to match up to the calibration line. My line depends on 19 different variable parameters (Perhaps this is too many? I have tried fewer without result) and I am using solver to adjust these parameters to make the two lines as close as possible.
For Example:
The QP column contains the variables I would like changed, changing these will draw me closer or further from the calibration curve. Each subsequent value of QP must be greater than the first.
Col=B Col=C
Power .QP_'
1 ..... 57000
2 ..... 65000
3 ..... 70000
4 ..... 80000
5 ..... 80000
Therefore my excel solver parameters look like this: C1:C19>=0,C1:C19<=100000 and C2>=C1, C3>=C2,C4>=C3... I have also tried making another column of the differences between each value and then saying that these must be diff>=0.
To compare this with my calibration curve I have taken the calibration curve data and subtracted my data derived from QP and then squared that to create my sum of the squares error. For example:
(Calibration-DerivedQP)^2=SS(x) <- where x represents the row number
Sum(SS(x))=SSE
SSE is what I have set solver to minimize. And upon changing QP everything automatically updates. There are no if statements being used and no pivot tables are used.
If I remove the parameters similar to C2>=C1 everything works perfectly, except the derived values are not feasible. But when the solver is run with these parameters, nothing gets changed and no matter which guesses I used as starting values ( so that I can ensure I haven't guessed a local minimum), the solver cannot improve upon my solution. This has led me to believe that something in my parameters is being broken, since I can very easily improve on my solution by guess and check. The rest of solvers settings are at the defaults, and the evolutionary method is used since my curve isn't smooth (I don't think) I had this working in the past and now something seems to be broken. Any ideas are appreciated! Thank you so much! Sorry if I am missing any critical information. I am also familiar with matlab and R if there are better methods in those languages.
I found the solution to my problem. I don't know if this will be helpful to anyone else since my problem vague and pretty specific to me. That being said, my problem was in the constraints. I changed some data on my excel sheet to allow for fewer constraints. An example might look like this:
Guess..........Squared......Added..................Q
-12..............(-12)^2....... 0
-16..............(-16)^2.......=(-16)^2+0.............256
+7.................(7)^2..........=(7)^2+(-16)^2+0....305
Now I allow solver to guess any number subject to minimal constraints.
Essentially, what is happening now, is the excel sheet allows for any guess that solver makes to work. By squaring the numbers it give me positive values, and the added column ensures that each successive value is equal to or greater than the first. This means there are very few constraints. I also changed the solver option from evolutionary to GRG Nonlinear.
Tips for getting solver to work:
Try and use the spreadsheet to set constraints (other than bounds, bounds seem to be good) wherever possible, the more constraints that I set in solver, the less likely my solution was to work.
Hope that helps, sorry if I have provided any incorrect information.

Resources