How to add a new constraint using ConstraintList() and "rule" in Pyomo - pyomo

I am working in a optimization problem that has many iterations, and for each iteration the size of problem increases.
I am trying to use model.c = ConstraintList() to create my set of constraints.
And later I have to add new constraints.
Today I am using model.c.add(expr). However, I would like to use model.c.add(rule=my_rule)
But this provide the error add() got an unexpected keyword argument 'rule'
Using .add(expr) works fine, but using rule would improve the performance of the algorithm.
I can't use model.c = Constraint(rule=my_rule) because I don't have the exactly size of the problem in the first iteration.
Could someone help me please?

Related

Either or constraint in linear programming PULP

I am new to LP and also to using pulp. I'm trying to add an either/or constraint of the below form:
x ==0 or x >=35
I understand introducing a binary decision variable might help, but the model won't be a linear problem anymore. Are there other python packages I can use in that case?

Can't disable messages for pymc3.find_MAP()

Sorry for the dumb question but I am unable to find any way to disable messages for pymc3.find_MAP(). As someone new to PyMC3 I think I've just learned to use the syntax properly (shakily) to do a single MAP estimate, given prior and likelihood functions. I'm about to try and scale this up to process a large number of variables but to so I'd like to eliminate messages of the type that I'm getting, e.g.:
Optimization terminated successfully.
Current function value: 1.889038
Iterations:2
Function evaluations: 4
Gradient evaluations: 4
but can't find anything in the documentation indicating how to do so. I figure I can turn them back on when I need to debug. Any suggestions ? Thanks in advance.
Extra arguments are passed into the optimization function, which by default is from scipy.optimize.*, so passing disp=False to pm.find_MAP will suppress messages.
As an aside, find_MAP is no longer recommended to initialize sampling.

Matlab coder build error when trying to access structures

I have a nested structure with some fields labeled as L1, L2 etc. I try to access a substructure within the structure using the following code.
lfield = lfidcalc(le);
substruct = bmstruct.(lfield);
Since le changes its value in every iteration, lfield also changes its value from L1 to L9.
However, when I try to build this code to generate a C++ executable, I get the following error.
Non-constant expression or empty matrix. This expression must be
constant because its value determines the size or class of some
expression. In this context, the value of the string must be known.
Can anybody please try to help me sort out this problem?
I'm not sure exactly what's causing the error message you're seeing, but in any case MATLAB Coder does not support accessing the fields of a structure using dynamic field names.
Perhaps that is the direct cause of what you're seeing, perhaps not: but in either case you have a problem.
Not all correct matlab code can be converted to C/C++. Especially this kind of (very handy) code. Have you put '%#eml' on the second line of your function? It indicates that you will generate code. From matlab website: " We use the %#eml directive to turn on the MATLAB M-Lint code analyzer and check the function code for errors and recommend corrections."

How do I know when a variable is accessed within my code?

I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.

best practice for implementing a Search-Function via prepared statements

I'm trying to implement a Search-Function using c++ and libpqxx.
But I've got the following problem:
The user is able to specify 4 different search patterns (each of them optional):
from date
till date
document type
document id
Each of them is optional. So if I want to use prepared statements I would need 2^4 = 16 different prepared statements. Well, it's possible, but I want to avoid this.
Here as an example what a prepared statement in libpqxx looks like:
_connection->prepare("ExampleStmnt", "SELECT * FROM foo WHERE title=$1 AND id=$2 AND date=$3")
("text", pqxx::prepare::treat_string)
("smallint", pqxx::prepare::treat_direct)
("timestamp", pqxx::prepare::treat_direct);
Therefore I also have no idea how I would piece such a prepared statement together.
Is there any other 'nice' way that I didn't think of?
The best you can do is to have four different ->prepare clauses, depending on how many search criteria are actually used, concatenate the criteria into your String, and then branch to one of the four prepare code blocks. (That will probably spook your style checker into thinking you are creating an injection vulnerability, but of course you aren't, as long as you insert only elements of the closed set os column names.)
Note that this isn't a very nice solution, but even Stephane Faroult (in The Art of SQL) says it's the best one possible, so who am I to argue?