how to compute weakest condition? - if-statement

if(x < y)
x = x + 1
else
x = 3 * x
{x < 0}
my answer :
x+1<0 --> x<-1
x*3<0 --> x<0
so weakest condition is (x<-1) ∩ (x<0) = x<-1
However, the answer sheet says that x<0 is the answer. How can I get the weakest condition?

Related

Why is the output 0 (=false)?

I'm exercising opreators in C++ and I don't understand the output of the code bellow
int x = 21, z = 33, y = 43;
cout << (!(z < y&& x < z) || !(x = z - y)) << endl;
I wrote it with the thought to be true and I understand it as "it's not the case z is less than y and x is less than z (which is false) or it's not the case x is equal to the difference of z and y (which is true)" so I expected output 1 (=true) and I'm confused that's not the case. Can you explain me where I'm making a mistake?
edit: Thanks for the answers, it's funny how I made such trivial mistakes I actually read about.
The part that you misinterpreted:
!(x = z - y))
x = z - y is assignment. It yields -10 as result. -10 is not 0, hence negating it yields false.
Now, first part of the expression:
!(z < y&& x < z)
!(33 < 43 && 21 < 33)
!(true && true)
!(true)
false
Putting it together:
(false || false) == false
This = is an assignment operator. It assigns values.
This == is a comparison operator. It is used to compare two values.
int value = 5, value2 = 12;
if(value == value2)
{
// do something if value and value2 are EQUAL (which they are not)
}
See this link for more information on operators in C++.

Setting a variable equal to 0 or 1 using an IF statement in SAS

I've been searching the documentation and experimenting myself but I can't figure out whether or not you can set a variable using an IF statement. For instance
x = if y >= 1;
would set x equal to 1 if y is greater than or equal to 1 and 0 otherwise. Is this possible in SAS? Do you have to do
if y >= 1 then x = 1; else x = 0;
Almost there... just remove the if :
x = (y >= 1) ;
Remember, all evaluations equate to either true (1) or false (0), so you can simplify lots of code in this manner, especially with the addition of ifn and ifc.
x = (y >= 1) ;
z = (index(name,'Dave') > 0) ;
q = ifc(x and not z,'This','That') ;
Or mixing boolean & regular algebra :
points = ((product = 'SHOES') * 100 * sale_price) + ((product = 'HATS') * 200 * sale_price) ;

Mixing integer arithmetic with boolean - Z3 prover

Before reading this question please consider that it is intended for use with the Z3 solver tool and it's c++ api (everything is redefined so it's not normal c++ syntax)
Can someone explain how do I mix boolean logic with integers (programing wise)?
Example:
y = (x > 10 and x < 100) //y hsould be true or false (boolean)
z = (y == true and k > 20 and k < 200)
m = (z or w) //suppose w takes true of false (boolean)
I tried with the examples given in the c++ file but I can't figure out how it works when mixing integer arithmetic and boolean.
Writing answer assuming you a beginner of c++.
May be you are looking for this.
bool y,z,m,w;
int x, k;
y = (x>10 && x<100);
z = (y == true && k > 20 && k < 200);
m = (z || w);
Let see what this line means:
y = (x>10 && x<100);
here if x is greater than 10 x>10 results true. In the same way if x is less than 100 x<100 results true. if both of them are true, the right side results true, which will be assigned to y.
|| means or.

Python My programming Lab

I have a problem doing one of the questions from my programming lab.
The question was like "Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min."
And my work area looks like
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
When I turned in, the feedback said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
although I tried may ways to figure it out but none of them worked.
Plz help.
Thx.
For the first reply, the system said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
Problems Detected:
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ min does not contain the correct value
⇒ y was modified
⇒ z was modified
For the second reply, the system said:
Remarks:
⇒ Unexpected identifiers: and, def, minist
⇒ You have to use the min variable .
⇒ You should use an assignment operator (=) in this exercise.
More Hints:
⇒ You almost certainly should be using: =
⇒ You almost certainly should be using: min
⇒ I haven't yet seen a correct solution that uses: , (comma)
I see three issues with the code as you've shown it in the question.
The first is that your indentation is a bit messed up. You probably want all your if statements to be at the same level, with the corresponding assignment statements indented under each one:
if something:
foo = bar
if something_else:
foo = baz
# etc
You could also use elif statements instead of the second and third if, since you never expect to have more than one of the conditions true at the same time.
The second issue is that you're doing your assignment statements backwards. If you want to assign the value in a variable x to a new variable named min, you should put min on the left side and x on the right:
min = x
This is part of the reason you're getting lots of confusing errors. Before your code runs, min is the name of a builtin function (which I suspect you'll learn about later in your class). When you did x = min you were replacing the old x value with a reference to the function.
The last thing is a logic issue. Your comparisons are all using the < operator to test if one value is smaller than the others. The issue is what happens if two of the values are equal. If x and y have the same value, both x < y and y < x will be False. That's not very good for your code, and so you probably want to use the <= operator instead. It tests if the left side is less than or equal to the right side. With this version, if you're using elifs, you can actually do away with the last condition and just use else, since if neither x or y is less than or equal to the other values, z must be the smallest value by process of elimination.
Anyway, here's the code with all three issues fixed:
if x <= y and x <= z:
min = x
elif y <= x and y <= z:
min = y
else: # no "z <= x and z <= y" check needed here, will always be true if reached
min = z
Maybe the tab problem.
You can try this:
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
I make a sample:
#-*- coding:utf-8 -*-
def minist(x,y,z):
if x < y and x < z:
return x
if y < x and y < z:
return y
if z < x and z < y:
return z
x = 1
y = 2
z = 3
test = -1
test = minist(x,y,z)
print(test)
This sample will print 1.

Is there a way to set a decision variable to true iff a variable is in a range in an integer linear program?

I have an integer-valued bounded variable, call it X. (Somewhere around 0<=X<=100)
I want to have a binary variable, call it Y, such that Y=1 if X >= A and X <= B, otherwise Y=0.
The best I've come up with thus far is the following (where T<x> are introduced binary variables, and M is a large number)
(minimize Y)
(X - A) <= M*Ta
(B - X) <= M*Tb
Y <= Ta
Y <= Tb
Y >= Ta + Tb - 1
(In other words, introducing two binary variables that are true if the variable satisfies the lower and upper bounds of the range, respectively, and setting the result to the binary multiplication of those variables)
This... Works, sort of, but has a couple major flaws. In particular, it's not rigorously defined - Y can be 1 even if X is outside the range.
So: is there a better way to do this? In particular: is there a way to rigorously define it, or if not, a way to at least prevent false positives?
Edit: to clarify: A and B are variables, not parameters.
I think the below works.
(I) A * Y <= X <= B * Y + 100 * (1 - Y)
(II) (X - A) <= M * Ta
(III) (B - X) <= M * Tb
(IV) Y >= Ta + Tb - 1
So X < A makes:
(I)Y=0
and (II), (III), (IV) do not matter.
X > B makes:
(I) Y = 0
and (II), (III), (IV) do not matter.
A <= X <= B makes:
(I) Y = 1 or Y = 0
(II) Ta = 1
(III) Tb = 1
(IV) Y = 1
Rewriting loannis's answer in a linear form by expanding out multiplication of binary variables with a continuous variable:
Tc <= M*Y
Tc <= A
Tc >= A - M*(1-Y)
Tc >= 0
Tc <= X
Td <= M*Y
Td <= B
Td >= B - M*(1-Y)
Td >= 0
X <= Td + 100*(1-Y)
(X - A + 1) <= M * Ta
(B - X + 1) <= M * Tb
Y >= Ta + Tb - 1
This seems to work, although I have not yet had the chance to expand it out to prove it. Also, some of these constraints may be unnecessary; I have not checked.
The expansion I did was according to the following rule:
If b is a binary variable, and c is a continuous one, and 0 <= c <= M, then y=b*c is equivalent to the following:
y <= M*b
y <= c
y >= c - M*(1 - b)
y >= 0