I am trying to use the Z3 solver (which works over SMT-LIB) to reason over C programs involving structs. I would like some way to represent that the struct is a variable that contains other variables in SMT-LIB, but I can't find a way to do that. Does anyone know of a way to represent C structs in SMT-LIB?
You can use algebraic data types feature of SMTLib 2.6 to model structs. See Section 4.2.3 of http://smtlib.cs.uiowa.edu/papers/smt-lib-reference-v2.6-r2017-07-18.pdf
This feature allows not only regular struct declarations but also recursive ones; i.e., you can also model structs that have fields of the same type.
I should add that algebraic data types in SMT are actually more general than what you need, they actually can be used to model values constructed with different algebraic constructors. (For the straightforward record case, you'll simply use one constructor.)
Algebraic-data types are rather a new feature in SMTLib, but both Z3 and CVC4 support it. Solver quality might vary depending on the features you use, but if you simply use datatypes to construct and deconstruct values it should work pretty much out of the box.
Related
I need to minimize a huge linear programming system where all related data (objective function, constraints) are stored in the memory in arrays and structures but not in lp file format or CPLEX
I saw that there are many solvers like here and here but the problem is how can I minimize the model without calling it from a file of a special format?
I did the same work previously in R and Python by solving the model directly after producing it without the need to save it initially in a special file and then call it by the solver. Here is an example in Python:
from lpsolve55 import *
from lp_maker import *
from lp_solve import *
lp = lp_maker(obj_func, constraints , rhs, sense_equality)
solvestat = lpsolve('solve', lp)
obj = lpsolve('get_objective', lp)
I think this is possible to do in C but really I don't know where to find how it is possible to do it.
One option is to use the APIs that commercial solvers like CPLEX and Gurobi provide for C/C++. Essentially, these APIs let you build the model in logical chunks (objective function, constraints, etc.). The APIs do the work of translating the logic of the model to the matrices and vectors that the solver actually needs in order to solve the model.
Another approach is to use a modeling language like AMPL or GAMS. AMPL, for example, also provides a C/C++ API.
Which one you choose probably depends on what solver you plan to use and how often you need to modify your model and/or data programmatically.
I have a memory sensitive project that uses ints to store an index of a vertex in a dense predecessor tree of a graph. (something that you generate using e.g. Bellman Ford). I know for a fact that I won't be having graphs with more than 10000 vertices, and the datatype is immutable, once created.
So is it a bad practice in my case to replace
int predecessors[blah];
with
short predecessors[blah];
if my work is to be used as a library.
Avoid premature optimization. Measure the impact of using short instead of int then decide based on that information.
As #HolyBlackCat said - consider using a typedef (or similar) so you can change the underlying type (i.e. short to int) in the event of changing requirements.
If you are using short, add sanity checks to avoid someone, someday feeding 100.000 vertices in your library and not realizing it cannot handle those (until a silent failure turns into a very non-silent one by expensive equipment loudly crashing into something).
Clearly document that you can only handle up to X number of vertices.
there,
I am new to pulp. I learn pulp from some examples I got online. These examples are very helpful and now I am able to write simple models by mtself. But I still feel difficult to build complex model, especailly model with sparse matrix.
Could you please kindly post with some complex examples with sparse matrix, and conplex constraints. I want to learn how to create necessary variables only, instead of simple one, such as, y = LpVariable.dicts("y", (Factorys, Customers) ,0,1,LpBinary).
I have another question: What happen if I simply use y = LpVariable.dicts("y", (Factorys, Customers) ,0,1,LpBinary) to define variables, in which most of variables are useless in model objective function and constraints, and I add some constraints to explicitly set such useless variable to 0? Does pulp algorithm is able to firstly identify such uesless variables and remove them first, then run Integer Programming algorithm (such as B&B or B&C) to solve the problem with reduced size? If this is true, It looks the "setting useless variable to 0" method will not decrease the solution speed at all. Am I right?
This may help
http://www.stuartmitchell.com/journal/2012/2/3/my-top-n-tips-for-python-coding-in-optimisation-1.html
In particular generate a set of of factories and customers first that is sparse.
factories_customers = [(f,c) for f in factories for c in customers
if <insert your condition here>]
Then use
y = LpVariable.dicts("y", factories_customers ,0,1,LpBinary)
Pulp does not remove "useless" variables and constraints so the model build time will be long.
However, the solution algorithms (CBC by default contain pre-solve algorithms that will remove the variables).
Not sure if this is within the realm of SO but:
Using VDM-SL, I have been looking around for the 'best' way of describing a single, finite subset of ℕ. In my travels I have found several ways that people are conveying this but I wonder which is the most accepted.
I initially thought that F(ℕ) would do but I believe that this is the set of finite subsets of ℕ, rather than a single subset.
Would it be enough to say, "Let S be finite: S ⊂ ℕ?"
Or does such a notation exist?
All sets in VDM language are finite by definition, so I believe there is no need to explicitly specify that part. As defined here http://wiki.overturetool.org/images/c/cb/VDM10_lang_manV2.pdf section 3.2.1
Now, to model a type which is a subset of a set s2 , one of the ways is to use an invariant on that type. such as "inv t == s1 subset s2".
Does anybody have any pointers to Naive Bayes Classifier Implementation preferably in C. I have 5 dimensional binary dataset. The class labels are also binary. I used Naive Bayes Classifier in Matlab with good results. However, is there any machine learning algorithm and its implementation which allows me to infer data from the class labels? Here in this case I want five dimensional binary data inferred from a binary class label. A sample of data is [1 1 0 1 0] and class is 0.
As you have a binary dataset, here is a nice implementation using C:
http://users.ics.tkk.fi/jhollmen/BernoulliMix/
It is a open source software that we are using currently in our course, you can actually check how he implemented the algorithm.
And about the question you made, here is my understanding.
What naive bayes classifier(NBC) does is to predict P(C|X) given some data and label. According to Bayes' theorem,
P(C|X) = \frac{P(X|C)P(C)}{P(X)}
which means that all you can do with predict the class of unknown data. Conversely, what you want to do there is P(X|C). Therefore, you can train your model like this,
P(X|C) = \frac{P(C|X)P(X)}{P(C)}
Accordingly, you have to assume distribution for your data...and stuff like that, therefore,it might be so accurate if you have a wrong assumption with your data. In you case, you have binary attributes X that is wanted be estimated from the label class, if you assume the attributes are independent, what you need to is like this,
P(C|X_1,X_2,X_3,X_4,X_5) \proportional P(X_1|C)P(X_2|C)P(X_3|C)P(X_4|C)P(X_5|C)P(C)
which is not so easy to solve.....
Hava a look at this package of the R-project:
http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/e1071/html/naiveBayes.html
http://cran.r-project.org/web/packages/e1071/index.html
You have tagged [C]: it is possible to link R with your own C-programs.