Prove a Boolean formula under some Implies conditions in z3py - boolean

I'm new in using Z3py, and my assignment is to generate counter examples for both solutions (sat and unsat).
Is there any function to generate counterexample for unsat solution?

unsat means there is no model that satisfies the assertions given. You can only extract a model when the problem is sat. So, to answer your question as you posed it, you cannot create models from an unsat solution: It just does not exist.
A typical approach is to assert the negation of the formula you are trying to prove; and if that formula is satisfiable then your original formula is falsifiable; i.e., there is a counter-example for it. Perhaps that's what you are trying to do? That is: If the negation of the formula has a satisfying model, then that model is a counter-example for the original formula. This is how most provers built on top of SMT solvers work, by sending the negation of what they are trying to prove and getting back unsat. If the prover returns a model, then it is a counter-example.

Related

Clingo: How to do "If p causes UNSAT then q."

In the code below
% Facts
a.
% Rules
-a :- a, not not p.
Adding the fact p. to the above would cause it to be UNSAT. Is there a way in clingo to add a rule to show this? Something like
q :- Assuming p causes UNSAT.
Solutions like adding the rule
{p; q} = 1.
Wouldn't work. It would give q. in the answer set if p. causes UNSAT, as I'd want. But, it would give p. and q. as answer sets when p. doesn't cause UNSAT. In the case of p. not causing UNSAT I wouldn't want q. in the answer set.
I'd like to be able to check whether certain facts causes a certain complex condition to not hold. For example, suppose one part of a problem requires you to check that a graph does NOT contain a Hamiltonian cycle. A program that finds Hamiltonian cycles would return UNSAT if the graph satisfies the condition, but I wouldn't want the program to end, since there would be other calculations to do.
You can probably treat it as an optimisation problem. So something that would normally be a constraint is turned into a special predicate which you then seek to minimise. Basically you have something of the form:
err(err1) :- some-bad-condition.
err(err2) :- some-other-bad-condition.
#minimize{ 1,XXX: err(XXX) }.
Here the XXX is a unique identifier for each error condition. The optimisation statement finds a model that minimises the number of errs.
The only caveat is that this increases the complexity of the problem; you're now solving an optimisation problem instead of a decision problem. It is a useful trick when debugging asp programs, but for large/difficult problems it may be too slow.

Iterative use of bintprog on MATLAB

We have a problem formulation as shown in this link.
Considering that the first call of bintprog gives a solution x that after some post processing does not adequately addresses the physical problem, is it possible to recall bintprog and exclude the prior solution x?
You need a nogood cut.
Suppose you find a solution \hat{x} that you then decide is infeasible (through some sort of post-processing). Let x and \hat{x} be indexed by i.
You can add a constraint of the following form:
\sum_{i : \hat{x}_i = 0} x_i + \sum_{i : \hat{x} = 1} (1-x_i) \geq 1
This constraint is an example of a no-good cut: the solution must differ from \hat{x} by at least one index i, otherwise it is infeasible. If your variables are not binary no-goods can be a little more complex.
You can add a no-good to your solution by appending the constraint as a row to your constraint matrix and re-solving with the bintprog() function. I'll leave it to you to you rewrite it in the MATLAB notation.
You didn't say what your post-processing does, but it would be even better if the post-processing could infer from your solution \hat{x} that other solutions are also infeasible, and you can add more than one row per iteration. This is a form of logic-based Benders decomposition, and the inference of other infeasible solutions is called solving an inference dual (as opposed to standard Benders decomposition, where you're solving the linear programming dual). More on logic based Benders decomposition from the man who coined the term, John Hooker of CMU.
Sorry for the formatting. I need to go but I'll figure out a way to display equations more nicely later.

why am I getting different answers with dsolve?

I have defined the following ODE
syms R1 C1 vc0 Vin
Vc_ode = 'Dvc+vc/(R1*C1)=(Vin)/(R1*C1)';
Vc=dsolve(Vc_ode,'vc(0)=vc0','t');
and the solution I receive is
Vin - (Vin - vc0)/exp(t/(C1*R1))
while solving manually I get
Vin +vc0*exp(-t/(C1*R1))
both are correct solutions, but is there a way to reach my desired solution?
I think the practical answer would be: No, you cannot let MATLAB reach your desired solution.
When looking at the dsolve input there is no option to specify what the output should look like. It is just a guess but this may be because it is hard to translate your desired style into code.
The only thing that might make a difference is the way you write the input formula but I would suspect it is not going to make much difference.
On the other hand, the academic answer would be: Everything is possible, you may need to make your own dsolve function though.
The problem is that the manual solution vc(t) = Vin +vc0*exp(-t/(C1*R1)) is incorrect. That solutions has vc(0) = Vin + vc0 which is not equal to vc0, so this is why your solutions differ. There is a theorem that states that a first order linear ODE with a initial condition like vc(t_0) = ... has exactly one solution. I suggest that you carefully go through you steps.

Matlab: `gfrank` over GF(2^m)

I've been working with matrices over GF(2) in Matlab. Well, I've been working with 0/1 matrices that I've been treating as being defined over GF(2). I was surprised/happy to see that Matlab provides some functionality in the Communications System Toolbox for working over finite fields. In particular, if I want to find the rank of a matrix over a finite field, there are a couple of methods: (1) use gfrank on the matrices that I already have defined, or (2) use rank on a Galois field array (created with gf). For matrices over GF(2), the former method seems to be significantly faster; however, there's a problem...
The documentation for gfrank says that the function doesn't work over fields of the form GF(2^m). I double checked on a toy example, and specifying GF(2) as the field to work over seems to output correct results. Moreover, the function's m-file specifies GF(2) as the default field (by specifying the second argument as 2 if nargin < 2). Something has to be wrong here, and it seems to be the documentation. However, I'd hate to assume that the documentation is wrong only to find out much later that the computation doesn't always work over GF(2^m). Does anybody know for sure what's wrong here? Thanks for your help.

Modelica execution order

Just starting with Modelica and having trouble understanding how it works.
In the below 'method' of the model, qInflow and qOutflow are used in the second line to evaluate der(h), but they have not received a value yet! (they were not defined in the 'data' of the method)? In what order is the code executed.
equation
assert(minV >= 0, "minV must be greater or equal to zero");
der(h)=(qInflow - qOutflow)/area;
qInflow=if time > 150 then 3*flowLevel else flowLevel;
qOutflow=Functions.LimitValue(minV, maxV, -flowGain*outCtr);
error=ref - h;
der(x)=error/T;
outCtr=K*(error + x);
end FlatTank;
From http://www.mathcore.com/resources/documents/ie_tank_system.pdf
This is an understandable point of confusion when coming from languages and systems that utilize imperative semantics. But Modelica doesn't work like that.
When working with Modelica it is important to understand that an equation section contains equations, not assignments. Consider this, if I gave you the following equations:
x + y = 3;
x + 2*y = 5;
If you understand that this is a mathematical context, you can then determine that x must have a value of 1 and y must have a value of 2. In other words, you have to solve a system of simultaneous equations. You'll note that the left hand side of these equations are not variables (in general), they are expressions. An equation is simply a relationship that equates one expression, on the left hand side, with another expression, on the right hand side. Furthermore, this relationship is always true and so order is irrelevant.
This is quite different from imperative programming languages with imperative semantics. But it is also very powerful because you can state these relationships (linear systems of equations, non-linear systems of equations, implicit equations, etc) and the compiler will work out the most efficient way to solve them.
Getting back to your example, when you look at the code in your question you are interpreting those equations as assignment statements. This notion is reinforced because they just happen to have variables on the left hand sides. But they are really equations. In an equation based system, you do not worry about whether a given variable has been assigned to previously. Instead, the requirement is simply that for every variable there exists (somewhere) an equation and that there are no extra equations. In other words, you should have the same number of variables as unknowns and that the system of equations has a unique solution. That is all that Modelica requires.
Now, Modelica supports the kind of imperative semantics you are used to. But they are only to be used in special cases because they constrain the interpretation of the mathematical behavior in such a way that it interferes with the symbolic manipulation that allows Modelica compilers to generate really fast code. So it is more than a question of style. You should use equations if at all possible and algorithms in Modelica should only be used as a last resort.
One last note. Some people may be wondering "Are you telling me that these equations will be put into some giant system of equations and solved by matrix inversion or Newton-Raphson or something? Why make it so complicated when it could obviously be solved in a much easier way!" But it will not be solved as a giant system of equations. If it can be solved as a simple set of assignments it will. That is one (among many) of the different symbolic manipulation techniques that will be applied. In fact, this is a key point about Modelica...you don't need to worry about optimizing the solution method, the tool will take care of that. And more importantly, if you connect components in such a way that a simultaneous system does arise, you don't need to worry about that either. Modelica tools can handle such "algebraic loops" for you, they will optimize it to find the most computationally efficient formulation and won't depend on you reformulating your model for those cases.
Does that help?
You cannot know the execution order of the equations in a Modelica model until you run a Modelica tool on it (you can re-order any equation in the source model and get the same result). And then the order is only true for this tool with the settings you used.
This was the order chosen by the OpenModelica compiler (omc +s +simCodeTarget=Dump model.mo):
error = ref - h;
outCtr = K * (error + x);
der(x) = DIVISION(error, T, #SHARED_LITERAL_2(String#);
qOutflow = LimitValue(minV, maxV, (-flowGain) * outCtr);
qInflow = if time > 150.0 then 3.0 * flowLevel else flowLevel;
der(h) = DIVISION(qInflow - qOutflow, area, #SHARED_LITERAL_3(String#);
This example was a little boring because the left and right sides of no equation changed place (h = error - ref would be viable if h was not chosen as a state variable, etc).