Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm writing a z3 python program that is running a bit slow. The reason why it is running slow is because there is one part of the program where I am adding many harder constraints in nested for loops. My instructor told us that adding equality constraints would make the program run faster in terms of limiting the possibilities the program goes through when the line Solver().check() is reached.
I'm wondering whether I should be adding the equality constraints before the "harder" constraints to make it go faster, or whether the equality constraints should go after the "harder" constraints?
I would want the equality constraints to be checked first to limit possibilities of the harder constraints, so I would assume s.add(x == y), or something should be added first so that it is checked first?
These sorts of questions come often; and the honest answer is that the performance of the solver on any given question relies on many factors. Changing the order of constraints should in general have no effect, but in practice it usually does. (See https://github.com/Z3Prover/z3/issues/5559 as an example.) Even renaming variables (something you'd think that'd have no effect) can change performance. (See here, for instance: https://github.com/Z3Prover/z3/issues/5147)
If you're having performance problems, it's best to address it as a modeling issue: i.e., how can you formulate your problem so it's "easier" to solve; instead of thinking about how to "reorder" the constraints, which is a never-ending guessing game. I suggest you actually post your actual encoding and ask about specific advice regarding that problem. Your question is unanswerable in the sense that there's no single strategy that will work well in all cases.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I do not need help with this, it is extremely easy, I need help to decide which of the several ways should I use. Like Do you think I can use recursion? I would need at least an
if
But, it says
only function calls
I am thinking of using recursion of course, but I would need an if-else, and yet it says "only function calls"
Consider the following Python function that outputs 4 times a quote from
a famous computer scientist named Brian Kernighan:
def print4x() :
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
Use this print4x function in a complete Python program that outputs the
Kernighan quote 64 times. Your solution must contain a main function, the
print4x function, and at least one additional function. You should not use
any for loops or while loops or anything like that —
just function calls!
Simply calling print4x 16 times is not an acceptable solution.
Since we cannot use loops or condition statements, and can only make use of function, here is my solution.
def print4x() :
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
print("Controlling complexity is the essence of programming!")
def repeat(num):
print4x()
numbers = (1, 2)
result = list(map(repeat, numbers))
Here we are only making use of functions. Yes there can be an improved code, I am open for suggestion in comments.
Discussion on language of Question
If we strictly stick to the language of the question, it says JUST FUNCTION CALLS
This prevents us from using loops and condition statements which makes it difficult to use recursion.
But some built-in functions also work as loops (like map()). So the question needs to elaborate on what kind of functions are allowed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working with Benders Decomposition and for the moment I am using the bendersatsp.py file in order to adapt this code for my own MIP problem.
The situation is that in my case the solution of the master takes part in some constraints of the subproblem, unlike this bendersatsp.py file. That's means that for each current solution of the master I have to modify the coeficients of these constraints.
So, what I could/must write in order to use the current solution vector of the master into the subproblem?
For the moment I have write this block of constraints into the separate function but the optimal objective value is not correct.
Why this is not a good way?
Thanks in advance!
In BendersLazyConsCallback, the self.get_values method:
Returns the solution values at the current node.
In the case that the node LP is unbounded, this method returns a
vector that corresponds to an unbounded direction, scaled so that at
least one of its elements has magnitude cplex.infinity. Thus, often
the vector can be used directly, for example to separate a lazy
constraint. However, due to the presence of large values in the vector
care must be taken to avoid potential numerical errors. If in doubt,
rescale the vector, and use it as an unbounded ray rather than a
primal vector.
The solution vector is passed into workerLP.separate and is used to modify the linear coefficients of the workerLP objective. Instead, it sounds like you would like to modify the constraints of workerLP in some way.
Rather than calling:
cpx.objective.set_linear(zip(thevars, thecoefs))
You would instead call methods in the Cplex.linear_constraints interface.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm implementing Kalman Filter on two types of measurements. I have GPS measurement every second (1Hz) and 100 measurment of accelration in one second (100Hz).
So basically I have two huge tables and they have to be fused at some point. My aim is: I really want to write readable and maintainable code.
My first approach was: there is a class for both of the datatables (so an object is a datatable), and I do bulk calculations in the class methods (so almost all of my methods include a for loop), until I get to the actual filter. I found this approach a bit too stiff. It works, but there is so much data-type transformation, and it is just not that convenient.
Now I want to change my code. If I would want to stick to OOP, my second try would be: every single measurment is an object of either the GPS_measurment or the acceleration_measurement. This approach seems better, but this way thousands of objects would have been created.
My third try would be a data-driven design, but I'm not really familiar with this approach.
Which paradigm should I use? Or perhaps it should be solved by some kind of mixture of the above paradigms? Or should I just use procedural programming with the use of pandas dataframes?
It sounds like you would want to use pandas. OOP is a concept btw, not something you explicitly code in inflexibly. Generally speaking, you only want to define your own classes if you plan on extending them or encapsulating certain features. pandas and numpy are 2 modules that already do almost everything you can ask for with regards to data and they are faster in execution.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can somebody help me write a code using python 3 that can solve the quadratic formula without using "import math"? Please !
As #Aurora001 mentioned, you should not ask for completed code. However, I think addressing the method you might use could be a good answer to your question, and I hope others agree. This is how I would approach the problem:
Prompt for input to get the coefficients a, b, and c.
Write a method that finds the square root of a number (several algorithms exist and can be found with a Google search) - There are also more built-in ways to do exponentiation in Python without the math module but this seems like it is for a class and the point may be to see if you can write a sqrt implementation.
Write a method that uses your custom square root implementation to solve the quadratic equation using the coefficients as parameters.
Test your two methods by doing a few test cases.
I hope this helps; post another question if you have more specific issues. I have noticed more people will be willing to assist if you generate your own code and ask about specific problems that you cannot solve.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Perhaps this is not the correct place to ask this question, and part of me thinks that there is no real answer to it, but I'm interested to see what experienced Python users have to say on the subject:
For maximum readability, concision, and utility, what is a range for an optimal length of a Python function? (Assuming that this function will be used in combination with other functions to do something useful.)
I recognize that this is incredibly dependent on the task at hand, but as a Sophomore Comp. Sci. major, one of the most consistent instructions from professors is to write programs that are comprised of short functions so as to break them up into "simple", discrete tasks.
I've done a big of digging, including through the Python style guide, but I haven't come up with a good answer. If there are any experienced Python users that would like to weigh in on this subject, I would appreciate the insight. Thanks.
I'm sure a lot of people have strong opinions about this, but for new programmers a good rule of thumb is to try and keep it below 10-20 lines. A better rule of thumb is that a function should do one thing and do that one thing well. If it becomes really long, it is likely doing more than one thing and can be broken down into several functions.