How to write a loop with an "or" condition - while-loop

What would be the best way to write this statement in Java.
continue loop while either x or y equals true
I have tried these and they fail. I am not sure of the proper way to construct this statement.
while (x || y == true)
while (x | y == true)
while (y == true) || (x == true)
while (y == true) | (x == true)
Thanks in advance.

Alright, so in Java, the best way would be
while(x || y)
If x and y are Booleans. If you are testing conditions,
while(x == 5 || y == 3)

while( x == true || y == true ){
//do loopy stuff
}
This may be different for some languages, but for C based syntax languages, this should work

Related

Axiomatic Semantics - How to calculate a weakest precondition of a program

Assuming the post-condition, how can I compute the weakest pre-condition of a program containing two statements?
For example :
a=x;
y = 0
{x = y + a}
Another example:
y = x;
y = x + x + y
{y = 3x ^ z> 0}
I tried to solve them but both questions resulted in pre-conditions or post-condition that are identical to the statement and I don't know if this is valid.
for example, the precondition of the last statement is "y=x" , thus it is the post condition of the preceding statement which is " y=x" as well
You can apply the rules of Hoare Logic here. Specifically, for the examples you have, you only need the rule for assignment:
{ P[E/x] } x = E { P }
Here, P[E/x] means take P and substitute (i.e. replace) all occurrences of x with E. For example, if P is x == 0 then P[0/x] gives 0 == 0.
To calculate the weakest precondition, you start from the end and work backwards. For your first example, we start with the last statement:
{ ??? } y = 0 { x == y + a }
The goal is to determine something suitable for ???. Applying our rule for assignment above, we can see that this is a solution:
{ x == 0 + a } y = 0 { x == y + a }
We can further simplify this to { x == a }. Then, we move on to address the statement before y = 0, and so on.

Anylogic, parameter variation error during iteration

I am trying to run a parameter variation experiment with 3 iterations and 100 replications in Anylogic. I use the same 100 seeds {10, 20, 30, ..., 1000} for every replication using
root.getDefaultRandomGenerator().setSeed((long) listOfSeeds.get(getCurrentReplication()));
in "before simulation run" java action field in parameter variation properties. I am varying one parameter. For retrieving data after each run I inserted following code in "after simulation run" section
replication = getCurrentReplication();
double totalMins = 0;
double totalLoading = 0;
double totalUnloading = 0;
int i = 0;
while(i < root.pickTime.size()){
double x = root.pickTime.getX(i);
if(i+7 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5) && x == root.pickTime.getX(i+6) && x == root.pickTime.getX(i+7)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5) +root.pickTime.getY(i+6) +root.pickTime.getY(i+7);
pickTimePV.add(x, y);
i += 8;
continue;}
if(i+6 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5) && x == root.pickTime.getX(i+6)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5) +root.pickTime.getY(i+6);
pickTimePV.add(x, y);
i += 7;
continue;}
if(i+5 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5);
pickTimePV.add(x, y);
i += 6;
continue;}
if(i+4 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4);
pickTimePV.add(x, y);
i += 5;
continue;}
if(i+3 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3);
pickTimePV.add(x, y);
i += 4;
continue;}
if(i+2 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2);
pickTimePV.add(x, y);
i += 3;
continue;}
if(i+1 < root.pickTime.size() && x == root.pickTime.getX(i+1)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1);
pickTimePV.add(x, y);
i += 2;
continue;}
else{
pickTimePV.add(x, root.pickTime.getY(i));
i++;}}
excelFile.writeDataSet(pickTimePV, 1, 1, 1 + iteration);
pickTimePV.reset();
iteration +=3;
kommiUtilization.add(getCurrentIteration(), root.kommiPool.utilization());
aushilfeUtilization.add(getCurrentIteration(), root.aushilfePop.get(0).timeInState(ResourceUsageState.USAGE_BUSY, MINUTE) / (root.kommissionierer.get(0).timeInState(ResourceUsageState.USAGE_BUSY, MINUTE)/root.kommiPool.utilization()));
ordersInQueue.add(getCurrentIteration(), root.orderQueue.size());
for(int j = 0; j < root.loadingTime.size(); j++){
totalLoading += root.loadingTime.getY(j);}
loadingTimePV.add(getCurrentIteration(), totalLoading/root.totalLoadTrucks);
for(int k = 0; k < root.unloadingTime.size(); k++){
totalUnloading += root.unloadingTime.getY(k);}
unloadingTimePV.add(getCurrentIteration(), totalUnloading/root.totalUnloadTrucks);
for(int t = 0; t < root.pickTime.size(); t++){
totalMins += root.pickTime.getY(t);}
totalPickTime.add(getCurrentIteration(), totalMins);
totalLoadTrucksPV.add(getCurrentIteration(), root.totalLoadTrucks);
totalUnloadTrucksPV.add(getCurrentIteration(), root.totalUnloadTrucks);
After replication No. 61 (183 simulation runs) the system throws the error:
Error in the model during iteration 3
java.lang.RuntimeException: Error in the model during iteration 3
at com.anylogic.engine.ExperimentParamVariation$b.i(Unknown Source)
at com.anylogic.engine.n$b.run(Unknown Source)
Before the error occurs, the last simulation run finishes succesfully (visibile using traceln()). I checked all possible parameter configurations and seed numbers where the error could be thrown with simulation experiments, but everything works fine for simulation experiments. So I guess the error has to do something with the parameter variation experiment setup.
Do you have any ideas how to fix the problem?
Thanks in advance.

Pandas .loc[] method is too slow, how can I speed it up

I have a dataframe with 40 million rows,and I want to change some colums by
age = data[data['device_name'] == 12]['age'].apply(lambda x : x if x != -1 else max_age)
data.loc[data['device_name'] == 12,'age'] = age
but this method is too slow, how can I speed it up.
Thanks for all reply!
you might wanna change the first part to :
age = data[data['device_name'] == 12]['age']
age[age == -1] = max_age
data.loc[data['device_name'] == 12,'age'] = age
you could use, to me more concise(this could gain you a little speed)
cond = data['device_name'] == 12
age = data.loc[cond, age]
data.loc[cond,'age'] = age.where(age != -1, max_age)

Julia-lang comparison of expression and commutativity

OK my title isn't great but it's easily explainable with an example.
julia>a = :(1 + 2)
julia>b = :(2 + 1)
julia>a == b
false
I have two expressions a and b. I would like to know if they will give me the same results without being evaluated.
I though that commutative operators like + or * could infer that the results would be the same.
EDIT:
Another way to understand it is to compare a very specific subset of expressions that can infer the commutativity of the function:
Expr(:call, +, a, b) <=> Expr(:call, +, b, a)
We can write a fairly simple function to check if two arrays have the same elements, modulo ordering:
function eq_modulo_ordering!(xs, ys) # note !, mutates xs and ys
while !isempty(xs)
i = findfirst(isequal(pop!(xs)), ys)
i === nothing && return false
deleteat!(ys, i)
end
isempty(ys)
end
eq_modulo_ordering(xs, ys) = eq_modulo_ordering!(copy(xs), copy(ys))
We can use then use this function to check if two top-level expressions are equivalent.
function expr_equiv(a::Expr, b::Expr, comm)
a.head === b.head || return false
a.head === :call || return a == b
a.args[1] ∈ comm || return a == b
eq_modulo_ordering(a.args, b.args)
end
expr_equiv(a, b, comm) = a == b
expr_equiv(a, b) = expr_equiv(a, b, [:+])
In the case that we want to check that two expressions are fully equivalent beyond the top-level, we could modify our functions to use mutual recursion to check if the subexpressions are expr_equiv, rather than isequal.
function eq_modulo_ordering!(xs, ys, comm) # note !, mutates xs and ys
while !isempty(xs)
x = pop!(xs)
i = findfirst(b -> expr_equiv(x, b, comm), ys)
i === nothing && return false
deleteat!(ys, i)
end
isempty(ys)
end
eq_modulo_ordering(xs, ys, comm) = eq_modulo_ordering!(copy(xs), copy(ys), comm)
function expr_equiv(a::Expr, b::Expr, comm)
a.head === b.head || return false
a.head === :call || return a == b
a.args[1] ∈ comm || return all(expr_equiv.(a.args, b.args, Ref(comm)))
eq_modulo_ordering(a.args, b.args, comm)
end
expr_equiv(a, b, comm) = a == b
expr_equiv(a, b) = expr_equiv(a, b, [:+])
We can now use expr_equiv as expected, optionally supplying a list of functions which are commutative.
julia> expr_equiv(:((a + b + b) * c), :((b + a + b) * c))
true
julia> expr_equiv(:((a + a + b) * c), :((b + a + b) * c))
false
julia> expr_equiv(:(c * (a + b + b)), :((b + a + b) * c), [:+, :*])
true
This is impossible. Figuring out whether two programs have the same result without evaluating them is called the Function Problem and is provably equivalent to solving the Halting Problem.
It is not possible to compute whether to pieces of code will have the same result.

Disregard component of a Triple in a comparison

I am attempting to compare Triples while disregarding certain values of the Triple. The value I wish to disregard below is signified by _. Note the below code is for example purposes and does not compile because _ is an Unresolved reference.
val coordinates = Triple(3, 2, 5)
when (coordinates) {
Triple(0, 0, 0) -> println("Origin")
Triple(_, 0, 0)-> println("On the x-axis.")
Triple(0, _, 0)-> println("On the y-axis.")
Triple(0, 0, _)-> println("On the z-axis.")
else-> println("Somewhere in space")
}
I know you can use _ when destructuring if you would like to ignore a value but that doesn't seem to help me with the above issue:
val (x4, y4, _) = coordinates
println(x4)
println(y4)
Any ideas how I can achieve this?
Thank you!
Underscore for unused variables was introduced in Kotlin 1.1 and it is designed to be used when some variables are not needed in the destructuring declaration.
In the branch conditions of your when expression, Triple(0, 0, 0) is creating an new instance but not destructuring. So, using underscore is not permitted here.
Currently, destructuring in the branch conditions of when expression is not possible in Kotlin. One of the solutions for your case is to compare each of the component verbosely in each branch condition:
val (x, y, z) = Triple(3, 2, 5)
when {
x == 0 && y == 0 && z == 0 -> println("Origin")
y == 0 && z == 0 -> println("On the x-axis.")
x == 0 && z == 0 -> println("On the y-axis.")
x == 0 && y == 0 -> println("On the z-axis.")
else -> println("Somewhere in space")
}
Here is a discussion on destructuring in when expression.