I was trying to solve the ZSUM problem at SPOJ using Python 2.7.9 as my coding language and designed a program to do so. As the code runs perfectly but gives a TLE at the judge, I guess it is not fast enough. Is there is possible to optimize the below code to meet the judge requirement or it is impossible to beat the challenge using Python.
Link to the problem: http://www.spoj.com/problems/ZSUM/
def zsum(n,k):
a=2*pow(n-1,k,10000007)
b=pow(n,k,10000007)
c=2*pow(n-1,n-1,10000007)
d=pow(n,n,10000007)
zsum=(a+b+c+d)%10000007
print zsum
def main():
while True:
n,k=map(int,raw_input().split())
if n==k==0:
break
else:
zsum(n,k)
main()
Since there are zero accepted python solutions in a total of 2335 successful submissions, i think, no matter how much you optimise your solution, it is unlikely to manage to get accepted with python. Although python is a very useful language, it is not preferred in programming contests, as it is extremely slow (compared to C/C++ for instance). If you know how to code in C++ you should definitely give it a shot, although you must write you own modular exponentiation procedure.
I don't know if this will help (if you read the problem's comments you will see someone saying that it is impossible to solve in Python - that can happen on online judges with slower languages) but you can optimize the code:
def zsum(n,k):
a=2*pow(n-1,k,10000007) # (1)
b=pow(n,k,10000007) # (2)
c=2*pow(n-1,n-1,10000007) # (1)
d=pow(n,n,10000007) # (2)
zsum=(a+b+c+d)%10000007
print zsum
Note that in (1), you are computing pow(n - 1, min(k, n - 1)) twice. You could compute it once and then use modular exponentiation only for what is left. Same for (2).
Related
I am currently trying to speed up an MIP. An approach I was considering was to implement a cut callback heuristic with PuLP (one which rounds relaxed integer variables greater than .9 to 1). Unfortunately, I do not believe PuLP has such a function to call, and I have looked into the mip module as well as dippy, but I don't feel like jumping to those. So, as a side note, if anyone knows how this can be done natively with PuLP let me know...
This leads me to my main question. Since PuLP is a wrapper and can be used with other solvers, I did see that Gurobi has such a function, and was able to call the code to Gurobi from PuLP with the code below:
Lp_prob = plp.LpProblem('Problem', plp.LpMinimize)
sd = plp.solvers.GUROBI(mip=True)
sd.actualSolve(Lp_prob, callback=mycallback)
Here is the function I am trying to call:
def mycallback(model, where):
model._vars = model.getVars()
if where == GRB.Callback.MIPNODE:
for x in model._vars:
if model.cbGetNodeRel(x) > 0.9 and model.cbGetNodeRel(x) < 1.0:
model.cbSetSolution(x, 1.0)
else:
return
However, after running a couple of times, the heuristic doesn't quite speed things up, in fact it kind of slows it down. I was wondering if this was implemented correctly, or if I were missing something. Any help or suggestions would be greatly appreciated.
I'm taking the Python course in Code Academy. I have never been good at math, never put much effort into it to be honest. My question is, below, when they say "Set bool_three equal to the result of 100 ** 0.5 >= 50 or False" as an example is "100 ** 0.5 >= 50 or False" - Just an example made up or would I be needing numbers like that when I start coding Python on my own? I have been doing great in the course so far, but when i get to questions like that I go brain dead for a second, I end up figuring it out, given some of them I do need to look up, but is this going to be a common theme I'm going to use when coding or did just explain it this way?
Thanks
Time to practice with or!
Set bool_one equal to the result of 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'
Set bool_two equal to the result of True or False
Set bool_three equal to the result of 100**0.5 >= 50 or False
Set bool_four equal to the result of True or True
Set bool_five equal to the result of 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1
What kind of math you will need depends entirely on your application. YOUR code, may not use math at all and all math needed is done by libraries and frameworks you use. - There is always math involved, whether you have contact with it is another question.
If you develop a solver for equations, then chances are, most of your code will end up "beeing maths".
If you meant to ask, if it's common to use boolean logic crossed with math, then in my experience, no it isn't. Typically you have flow control dictate which piece of math is used and then proceed. You don't mangle it all into a single expression.
Math (algebra) and programming are really coupled in my head. Being good at math would sharpen your skills in problems solving.
Don't worry! That's a thing that you can acquire by learning more about problems solving. You don't have to take classes in Math! (Even though it would still be better to improve the Math part by taking classes).
From the example you given, I can see that it caught your attention in the sense that you possibly were like:
OMG! Math?!!!!
The example is nothing at all. Therefore, this kind of math would be required to comprehend whilst you are taking the programming course. That's of course if you are interested to be a good programmer.
stackoverflow is here and always will be. Whenever you have questions about some problems you don't know how to work it out, just post your questions. That's how we all learn ;)
Happy learning.
I'm trying to minimize a function with lots of parameters (a little over 7000) using fmin_bfgs() or fmin_l_bfgs_b(). When I enter the command
opt_pars = fmin_l_bfgs_b(obj_f, pars, approx_grad=1)
(where obj_f is the function I'm trying to minimize and pars is the vector of initial parameters) the function just runs forever until python tells me that it has to terminate the program. There is never any output. I tried adding the argument maxfunc = 2 to see if it was getting anywhere at all and the same thing happened (ran forever then python terminated the program).
I'm just trying to figure out what could be going wrong with the function. It seems like maybe it's getting caught in a while loop or something. Has anyone encountered this problem? If not, I could also use some general debugging help here (as I'm relatively new to Python) on how to monitor what the function is doing.
And finally, maybe someone can recommend a different function or package for the task I'm attempting. I'm trying to fit a lasso regularized Poisson regression to sparse data with about 12 million observations of 7000 variables.
PS Sorry for not including the -log likelihood function I'm trying to minimize, but it would be completely uninterpretable.
Thanks a lot for any help!
Zach
Since you don't provide gradients to fmin_bfgs and fmin_l_bfgs_b, your objective function is evaluated len(x) > 7000 times each time the gradient is needed. If the objective function is slow to evaluate, that will add up.
The maxfun option doesn't apparently count the gradient estimation, so it's possible that it's actually not an infinite loop, just that it takes a very long time.
What do you mean by "python tells me that it has to terminate the program"?
Please in any case try to provide a reproducible test case here. It doesn't matter if the objective function is incomprehensible --- what is important is that people interested can reproduce the condition you encounter.
I don't see infinite loops problem on my system even for 7000 parameters. However, the function evaluation count was about 200000 for a simple 7000-parameter problem with l_bfgs_b and no gradient provided. Profile your code to see what such evaluation counts would mean for you. With gradient provided, it was 35 (+ 35 times the gradient). Providing a gradient may then help. (If the function is complicated, automatic differentiation may still work --- there are libraries for that in Python.)
Other optimization libraries for Python, see: http://scipy.org/Topical_Software (can't say which are the best ones, though --- ipopt or coin-or could be worth a try)
For reference: the L-BFGS-B implementation in Scipy is this one (and is written by guys who are supposed to know what they are doing):
http://users.eecs.northwestern.edu/~nocedal/lbfgsb.html
***
You can debug what is going on e.g. by using Python debugger pdb, python -m pdb your_script.py. Or just by inserting print statements inside it.
Also try to Google "debug python" and "profile python" ;)
Are there any easy ways to convert mathematical formulas to Python code?
Perhaps translators, web reference, specific book chapters, anything ~
For regular expressions there are programs such as Kodos and sites such as pythonregex.com, so I was hoping there would be something similar for formula notation and Python.
No, this isn't possible in general. There are mathematical functions that aren't computable (for example, see wikipedia/Halting_problem). There are other mathematical functions where it's just not obvious how to code them up (consider a difficult integral or differential equations). There are many books written on finding numerical solutions to these sorts of problems (you can find some links here: wikipedia/Numerical_analysis).
For simple cases, you can transcribe mathematical formulae directly, but any automated means of translation would require a formal language for writing mathematical formulae in what would be a programming language in itself. This would beg the question, since you would be trading writing mathematical formulae in one language with writing them in another.
You could try to make your own with sympy and pyparsing.
so how to convert a math formula into python?
well, i myself dont know pretty much of python (i am just a beginner) but i have got some tips to help you:
first, you should like calculate the formula using x and y (or a and b) and write that down on a paper.
lets say phythagoras theory ( which is kinda basic).
c2 = a2 + b2;
so you have to input those a and b and write this formula to calculate c (which will be c = sqrt a2 + b**2)
now lets say you dont know this formulas (i can hear you). then? well, you should search it up on internet, listen to its explanation and try to transform them to python.
this is like noobs advice, but you know - no easy ways in programming.
(well, just search it on internet, i guess?)
Is there a Python library out there that solves for the Nash equilibrium of two-person zero-games? I know the solution can be written down in terms of linear constraints and, in theory, scipy should be able to optimize it. However, for two-person zero-games the solution is exact and unique, but some of the solvers fail to converge for certain problems.
Rather than listing any of the libraries on Linear programing on the Python website, I would like to know what library would be most effective in terms of ease of use and speed.
Raymond Hettinger wrote a recipe for solving zero-sum payoff matrices. It should serve your purposes alright.
As for a more general library for solving game theory, there's nothing specifically designed for that. But, like you said, scipy can tackle optimization problems like this. You might be able to do something with GarlicSim, which claims to be for "any kind of simulation: Physics, game theory..." but I've never used it before so I can't recommend it.
There is Gambit, which is a little difficult to set up, but has a python API.
I've just started putting together some game theory python code: http://drvinceknight.github.com/Gamepy/
There's code which:
solves matching games,
calculates shapley values in cooperative games,
runs agent based simulations to identify emergent behaviour in normal form games,
(clumsily - my python foo is still growing) uses the lrs library (written in C: http://cgm.cs.mcgill.ca/~avis/C/lrs.html) to calculate the solutions to normal form games (this is I believe what you want).
The code is all available on github and that site (the first link at the beginning of this answer) explains how the code works and gives user examples.
You might also want to check out 'Gambit' which I've never used.