I need to construct some logic according to the above table - where S1, S2 ,S3 are strings and I need to find if the field values (strings) CONTAIN these strings.
Green rows should be returned, red rows not. The list of red and green rows is not exhaustive, but enough that you get the idea.
I feel I am missing a really simple way to do this - any ideas?
((Field1 == S1) || (Field1 == S2) || (Field1 == S3)) && ((Field2 == S1) || (Field2 == S2) || (Field2 == S3)) && ((Field3 == S1) || (Field3 == S2) || (Field3 == S3)) Should be the condition to check this.
Related
When I started to traverse a code of my friend, I came across two declarations of if clause.
one is declared as follows:
(CASE A)
if x not in ('a', 'b'):
pass
another is declared like this:
(CASE B)
if x != 'a' or x != 'b':
pass
(CASE C)
if x != 'a' and x != 'b':
pass
I've simplified the values as 'a' and 'b' but the actual values are complicated and pretty long. When I run them separately, I got the same output.
Which one does CASE A match with? CASE B or CASE C.
Also which form of writing is correct? using in or using equality operator.
They not the same, if you start from Case A, hopefully it is easy to see.
Consider case A.
x not in ('a', 'b')
This literally means, x is not equal to 'a' and x is not equal to 'b'. Or written as code.
x != 'a' and x != 'b'
That happens to be case C.
Case B on the other hand is exactly the same as C but it has an 'or' instead of an 'and'.
'or' does not perform the same operation as 'and', therefore case C is different.
x = 'a'
print("A", x not in ('a', 'b') )
print("B", x != 'a' or x != 'b')
print("C", x != 'a' and x != 'b')
In or statement you are saying that if you found any of the operands true then no need to check the other operands because the result will be true anyway.
In CASE B if x was (not 'a') then it's true and it'll not check if x is equals to 'b'.
You can simply test the cases by printing x as follows:
>>> x='b'
>>> if x not in ('a', 'b'): #CASE A (nothing printed when x='b')
... print(x)
...
>>> if x != 'a' or x!='b': #CASE B
... print(x)
...
b # The condition is true so x's value printed
>>> if x != 'a' and x!='b': #CASE C (nothing printed when x='b')
... print(x)
...
>>>
Here you can find that A and C have the same output (which is nothing).
Definitely, CASE C.
x not in ('a', 'b') means that x has not been appeared in given elements.
CASE B can be converted into correct form with:
if not (x == 'a' or x == 'b'):
pass
The MiniZinc constraint solver allows to express cardinality constraints very easily using the built-in sum() function:
% This predicate is true, iff 2 of the array
% elements are true
predicate exactly_two_sum(array[int] of var bool: x) =
(sum(x) == 2);
The cardinality constraint is met, if and only if the number if true elements in the array of Boolean variables is as specified. Booleans are automatically mapped to integer values 0 and 1 to compute the sum.
I implemented my own cardinality constraint predicate as set of counter slices:
% This predicate is true, iff 2 of the array
% elements are true
predicate exactly_two_serial(array[int] of var bool: x) =
let
{
int: lb = min(index_set(x));
int: ub = max(index_set(x));
int: len = length(x);
}
in
if len < 2 then
false
else if len == 2 then
x[lb] /\ x[ub]
else
(
let
{
% 1-of-3 counter is modelled as a set of slices
% with 3 outputs each
array[lb+1..ub-1] of var bool: t0;
array[lb+1..ub-1] of var bool: t1;
array[lb+1..ub-1] of var bool: t2;
}
in
% first two slices are hard-coded
(t0[lb+1] == not(x[lb] \/ x[lb+1])) /\
(t1[lb+1] == (x[lb] != x[lb+1])) /\
(t2[lb+1] == (x[lb] /\ x[lb+1])) /\
% remaining slices are regular
forall(i in lb+2..ub-1)
(
(t0[i] == t0[i-1] /\ not x[i]) /\
(t1[i] == (t0[i-1] /\ x[i]) \/ (t1[i-1] /\ not x[i])) /\
(t2[i] == (t1[i-1] /\ x[i]) \/ (t2[i-1] /\ not x[i]))
) /\
% output 2 of final slice must be true to fulfil predicate
((t1[ub-1] /\ x[ub]) \/ (t2[ub-1] /\ not x[ub]))
)
endif endif;
This implementation is using a parallel encoding with fewer lines/variables between the slices:
% This predicate is true, iff 2 of the array
% elements are true
predicate exactly_two_parallel(array[int] of var bool: x) =
let
{
int: lb = min(index_set(x));
int: ub = max(index_set(x));
int: len = length(x);
}
in
if len < 2 then
false
else if len == 2 then
x[lb] /\ x[ub]
else
(
let
{
% counter is modelled as a set of slices
% with 2 outputs each
% Encoding:
% 0 0 : 0 x true
% 0 1 : 1 x true
% 1 0 : 2 x true
% 1 1 : more than 2 x true
array[lb+1..ub] of var bool: t0;
array[lb+1..ub] of var bool: t1;
}
in
% first two slices are hard-coded
(t1[lb+1] == (x[lb] /\ x[lb+1])) /\
(t0[lb+1] == not t1[lb+1]) /\
% remaining slices are regular
forall(i in lb+2..ub)
(
(t0[i] == (t0[i-1] != x[i]) \/ (t0[i-1] /\ t1[i-1])) /\
(t1[i] == t1[i-1] \/ (t0[i-1] /\ x[i]))
) /\
% output of final slice must be 1 0 to fulfil predicate
(t1[ub] /\ not t0[ub])
)
endif endif;
Question:
Does it make sense to use home-grown cardinality predicates? Or is the MiniZinc implementation of sum() beyond all doubts in terms of solution speed?
Update:
I am using Gecode as solver back-end.
Linear sums is typically one of the most significant constraints to implement well in a constraint solver, so for your case the initial version using a simple sum is much better. In particular, the propagator in Gecode that implements the Boolean sum is heavily optimized to be as efficient as possible.
As a general rule, it is typically a good idea to use the constraints that are available. In particular, if what one is doing maps well to a global constraint that is typically a good idea. A related example would be if you want to count the occurrences of several different numbers in an array of integers, in which case the global cardinality constraint is very useful.
For completeness: When using lazy clause generation solvers (Chuffed for example), (novel) decompositions may sometimes be surprisingly useful. But that is a much more advanced topic.
I have two numbers, X and Y, and I want to assert that X is a multiple of Y -- i.e., I want to assert that there exists some arbitrary integer n such that Y * n == X.
If Y is guaranteed to be greater than zero, I can write
assert!(x % y == 0)
But in my case, Y may be zero. If Y is zero, then X must also be zero, so I could write
if y == 0 {
assert!(x == 0);
} else {
assert!(x % y == 0);
}
But this is 5 lines long and has two branches vs. the assert's single branch. Is it possible to do this in one line in an elegant way?
if is an expression in Rust, so you could write
assert_eq!(0, if y == 0 { x } else { x % y });
As mentioned in the comment, you could also write
assert!(y == 0 && x == 0 || x % y == 0);
but the if expression approach is more general.
You could also use checked_rem so that x.checked_rem(0) == None, but that is not going to be simpler than an if expression.
assert_eq!(0, x.checked_rem(y).unwrap_or(x));
Waveform:-
I did a property as:
property p1;
a |=> (b == 1)[=2] ##1 (c == 1)[=2]
endproperty
But this property doesn't work well for this waveform, it isn't working for 3 or more "b's" before the "c's" and it isn't working for "c's" after the first "b".
I need a property that just can pass after the "a" signal just 2 "b's" and after just "2 c's" with any quantity of gaps between them.
thanks for help.
You do not specify that b should not be 1 when during the pulses on c, nor do you specify that c should not be 1 during the pulses on b.
So, how about something like this:
property p1;
a |=> ((c == 0) throughout (b == 1)[->2]) ##1 ((b == 0) throughout (c == 1)[->2]);
endproperty
The [->N] operator is the exact non-consecutive repetition operator or goto repetition operator. With goto repetition, the expression must hold in the final cycle of the match; in other words, the match is achieved as soon as the specified number of repetitions has occurred.
Can you have multiple evaluations in your WHEN clause?
For example is there a way to do something like this:
when (x == "Open" and y == "Ready") $ do...
I can get a single condition to work, but what if I need both to be true?
How about something like
when (x == "Open" && y == "Ready") $ do
...
&& is just a normal infix operator defined as
True && a = a
False && _ = False
Notice that with laziness this has exactly the same short circuiting properties we get in other languages!
On a totally separate note, instead of using strings to represent different states the Haskell-ish way would be to define your own type
data StateOfFoo = Ready
| Open
| Closed
| ...
deriving(Eq)
and then use x == Open && y == Ready for example. This way you make it clear in the types what you expect x and y to be.