My tests is showing 1 of 4 branches missing and I am not sure why.
public Classification classify() {
if (points >= 1 && points <= 4) {
return Classification.First;
}
Tests I ran:
#Test
void Test1() {
int p = 3;
Grade grade = new Grade(p);
assertEquals(grade.classify(), Classification.First);
}
#Test
void Test2() {
int p = 6;
Grade grade = new Grade(p);
assertNotEquals(grade.classify(), Classification.First);
}
The 1/4 branch coverage is showing on the if (points >= 1 && points <= 4) {
Not really sure which branch I am missing at this point. Any suggestions?
Short answer: you should test also with p < 1 so for example with p = 0.
Long answer: when testing such stuff you should always test inside bounds, outside bounds and in this case also including the bound values. Actually coverage does not even tell you about the missing cases for the included limits.
Your case points >= 1 && points <= 4 will have the following cases:
points < 1
points = 1
points between 1 < points < 4 (so 2 or 3)
points = 4
points > 4
Cases 2,3,4 are considered as the same in the coverage as a branch but as a requirement they actually are not the same. We might see from the implementation (if white box testing) that there should not be problems if tested with only on evlaue in between (2,3) value 1 <= points <= 4 but if we do not know implementation (black box testing) there might be. And again it is still a good idea to test the limits even if implementation is known since implementation may change later.
Related
I have a binary variable y[k][t], where k = 1..3 (machines) and t = 1..10 (time). Variable Y is 1 if machine is active and 0 otherwise.
In the optimization if machine 1 is active in period 1 e.g. Y[1][1] = 1, i want the machine to continue to operate for at least 3 time periods. i.e. Y[1][1] = Y[1][2] = Y[1][3] = Y[1][4] = 1.
I only want the succesive variable for t+1,t+2,t+3 to be same as t if it is active.
how can i do that in cplex studio?
This is sometimes called a minimum run length. There are different approaches to handle this. Say we have x(t) as our binary variables indicating if the machine is operating at time t. The first thing is to introduce a binary variable start(t) that indicates when a run starts. This is what I mean:
t 1 2 3 4 5 6 7 8
x 0 1 1 1 0 1 1 0
start 0 1 0 0 0 1 0 0
A run starts when x(t-1)=0 and x(t)=1, or more formally:
start(t) = x(t)*(1-x(t-1))
This is nonlinear. We can linearize this using:
start(t) <= x(t)
start(t) <= 1-x(t-1)
start(t) >= x(t)-x(t-1)
Often we just use the bound:
start(t) >= x(t)-x(t-1)
Next we need:
start(t) = 1 ==> x(t)...x(t+K-1) = 1
where K is the minimum run length.
This can be modeled as:
x(t+1) >= start(t)
...
x(t+K-1) >= start(t)
(we already know that x(t)=1 if start(t)=1).
what Erwin wrote about minimum run length is fine. If you rely on logical constraints that are available in CPLEX the model is a bit easier:
range K=1..3;
range T=1..10;
dvar boolean Y[K][T];
dvar boolean start[K][T];
subject to
{
forall(k in K) start[k][1]==Y[k][1];
forall(k in K,t in T:t!=1) start[k][t] == ((Y[k][t]==1) && (Y[k][t-1]==0));
forall(k in K) forall(l in 1..3)
forall(t in T:(t+l) in T) (start[k][t]==1) => (Y[k][t+l]==1);
}
But what could lead to long solve time if you grow the time horizon is that we enumerate time. Within CPLEX and OPL you may also use CPOptimizer and its dedicated scheduling concepts : intervals.
Then you would write
using CP;
range K=1..3;
range T=1..10;
int nbMaxIntervals=4;
dvar interval itvs[K][1..nbMaxIntervals] optional in T size 3..10;
subject to
{
forall(k in K) forall(i in 1..nbMaxIntervals-1)
endBeforeStart(itvs[k][i],itvs[k][i+1]);
}
What makes sure that you are on for at least 3 time periods is
size 3..10;
NB: More about CPOptimizer
I would like to know where I can read about algorithms for solving this problem efficiently:
Four directions allowed: up, down, left, right
Cells containing zero can't be visited.
Visiting the same cell twice is illegal.
Moves wraps around the edges:
(first row is connected with last row)
(first col is connected with last col)
Example, 5x5 and 5 steps:
9 1 3 1 9
6 3 2 4 1
0 7 * 7 7
5 4 9 4 9
7 9 1 5 5
Starting point: *
Solution: down,left,down,left,down. That is 9 + 4 + 9 + 7 + 9 = 38
[9] 1 3 1 9
6 3 2 4 1
0 7 * 7 7
5 [4][9] 4 9
[7][9] 1 5 5
This problem is probably not related to:
Finding the maximum sub matrix
Dynamic programming
You specified in comments that you wanted a sub-second way of finding the best value 20-step path out of a 5x5 matrix. I've implemented a basic recursive search tree that does this. Ultimately, the difficulty of this is still O(3^k), but highly saturated cases like yours (21 out of 24 allowed nodes visited) will solve much faster because the problem simplifies to "skip the n*n-z-k-1 lowest value cells" (in this case, n=5, z=1 and k+1 = 21; the winning path skips three 1's).
The problem instance in your question solves in 0.231seconds on a 3 year old i5 laptop and about half a second on ideone.com. I've provided code here http://ideone.com/5kOyxq (note that 'up' and 'down' are reversed because of the way I input the data).
For less saturated problems you may need to add a Bound/Cut method. You can generate a Bound as follows:
First, run over the NxN matrix and collect the K highest value elements (can be done in N² log K) and sort them by max-first. Then, cumulatively calculate the value UB[t] = SUM[i::0->t] SortedElements[i]. So, any t-length path has a UB of UB[t] (max t elements).
At step T, the current Branch's UB is UB[t]. If ValueSoFar[T] + UB[K-T] <= BestPathValue, you can stop that branch.
There may be better ways, but this should be sufficient for reasonably sized matrices and path lengths.
Game or puzzle. Given a matrix, number of steps and a sum, find the path.
Would be nice if there is a real world application for this, but i haven't found it.
Games tend to "burn in" knowledge in young brains, so why not burn in something useful, like addition?
#include<iostream>
#include<climits>
#define R 3
#define C 3
int MAX(int x, int y, int z);
int Max_Cost(int cost[R][C], int m, int n)
{
if (n < 0 || m < 0)
return INT_MIN;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n] + MIN( Max_Cost(cost, m-1, n-1),
Max_Cost(cost, m-1, n),
Max_Cost(cost, m, n-1)
);
}
int MAX(int x, int y, int z)
{
return max(max(x, y), z);
}
int main()
{
int cost[R][C] = { {3, 2, 5},
{5, 8, 2},
{9, 7, 1}
};
cout<<Max_Cost(cost, 2, 1);
return 0;
}
Say I have turn based game with 20 attack turns and the players attack speed determines how often they get to attack, how would you calculate and or graph a table of "who attacks when" when one person has an attack speed of 5 and another a speed of 8? (These are test values, the values I will be using will vary. 20 attack turns will be the cap, however, each player will be able to invest in their own attack speed with their skill points up to a value of 10)
I've been programming in C for about a 2 years and I'm currently playing with Obj-C making my first indy game, any advise or knowledge would be of great help.
If I understand the problem correctly:
The first to attack will be min(a,b), where a and b are your initial "attack speeds", 5 and 8. Then subtract the min value of both, and if the result is 0 or less add the attacker's original value again. So after the first attack, a=5 again, but now b=8-5=3. Then it's b's turn:
a = 5 - 8 = -3
b = 3 - 8 = -5
Both less than zero, so add 5 and 8 again:
a = 2
b = 3
a attacks:
a = -3 -> a = 2
b = -2 -> b = 6
and thus a gets to score another hit. And so on, until you get a tie -- you're bound to run into that one way or another. You could return a "tie" result, or let the last attacker or last defender "win" that one. Put some thought in this, because what if both players have the same "attack" value?
This math problem tells me who’s turn is when -
For example:
There’s 11 attacks
you attack 8 times and your enemy attacks 3
Determine an attack sequence thats evenly distributed and consistently repeatable.
In this case:
1. player 1 attacks
2. player 1 attacks
3. player 2 attacks
4. player 1 attacks
5. player 1 attacks
6. player 1 attacks
7. player 2 attacks
8. player 1 attacks
9. player 1 attacks
10. player 1 attacks
11. player 2 attacks
(repeat these 11 steps as long as needed)
I first thought the solution would be best done with division until I ran into 20 walls, so
I choose subtraction bc it can be reproduced and works better programmatically, instead of creating an attack sequence by division and storing it as an array and indexing the array each time you need to determine a persons turn.
so my code to do this was:
if (playerTwoWent == 0)
{
difference = difference - player_twos_speed;
}
else if (playerTwoWent == 1)
{
difference = player_ones_speed + difference;
playerTwoWent = 0;
}
if (difference >= 0)
{
printf("player 1s turn: %d\n", difference);
}
else if (difference < 0)
{
if (playerTwoWent == 0)
{
printf("player 2s turn: %d\n", difference);
playerTwoWent = 1;
}
}
I'm working on some Hoare logic and I am wondering whether my approach is the right one.
I have the following program P:
s = 0
i = 1
while (i <= n) {
s = s + i
i = i + 1
}
It should satisfy the hoare triple {n >= 0}P{s = n*(n+1)/2} (so it just takes the sum). Now, initially I had |s = i*(i-1)/2| as my invariant, which works fine. However, I had a problem from going to the end of my loop, to my desired postcondition. Because for the impliciation
|s = i*(i-1)/2 & i > n|
=>
| s = n * (n+1) / 2 |
to hold, I need to prove that i is n+1, and not just any i bigger than n. So what I thought of is to add a (i <= n + 1) to the invariant, so that it becomes :
|s = i * (i-1)/2 & i <= n+1|
Then I can prove the program so I think it should be correct.
Nonetheless, I find the invariant to be a bit, less "invariantly" :). And not like anything I've seen in the course or in the exercises so far, so I was wondering if there was a more elegant solution here?
So what I thought of is to add a (i <= n + 1) to the invariant, so that it becomes :
|s = i * (i-1)/2 & i <= n+1|
Nonetheless, I find the invariant to be a bit, less "invariantly" :). And not like anything I've seen in the course or in the exercises so far, so I was wondering if there was a more elegant solution here?
Nope, given the way the code is written, that's exactly the way to go. (I can tell from experience since I've been teaching Hoare logic during several semesters in two different courses and since it's part of my graduate studies.)
Using i <= n is common practice when programming. In your particular program, you could just as well have written i != n+1 instead, in which case your first invariant (which indeed looks cleaner) would have sufficed since you get
| s=i*(i-1)/2 & i=n+1 |
=>
| s=n*(n+1)/2 |
which evidently holds.
There is another way to reason,given a more appropriate invariant (and other code)...searh n for final value of i...
I : s = i*(i+1)/2 and 0 <= i <=n
B : i < n
Now,evidently you have for post condition:
I and i >= n => s = i*(i+1)/2 and i=n => s = n*(n+1)/2
The code now becomes
s = 0
i = 0
while (i < n) {
s = s + (i+1)
i = i + 1
}
The invariant holds at init and keeps after each loop,since rewriting I as 2s=i*(i+1) we have to proof
I and i<n => 2(s + (i+1)) = (i+1)*(i+2)
2(s + (i+1) )=
2s + 2(i+1) =
i*(i+1) + 2(i+1)= (since I holds)
(i+1)(i+2)
Qed.
I got this question in a programming test. Do you think this question is even correct? Look at the answer choices. (2^x means 2 raised to x)
Consider the following pseudocode.
x := 1;
i := 1;
while (x >= 1000)
begin
x := 2^x;
i := i + 1;
end;
What is the value of i at the end of the pseudocode?
a) 4
b) 5
c) 6
d) 7
e) 8
I am sure that the value of i will 1. I told the examiner of the discrepancy and he advised me the leave the question unanswered if I felt it was incorrect. What else could I have done?
1
X < 1000, so it doesn't enter the while.
Or there is an error in the Question (and X should be <= 1000 and not >=1000)
If it's <= 1000 it should be 5:
2 - 4 - 16 - 65K
2 - 3 - 4 - 5
This question tests two things:
can you read code
can you communicate / interact
Since you asked about the discrepancy, you showed 1. to be true. I'm not so sure if you passed 2, it depends too much on the situation / expectations.
I believe I would have left a note on the answer sheet stating 'none of the given'.
Not an easy situation!
As written, the answer would be 1.
Had the test on the while been reversed (i.e. x < 1000), then the series is:
At the end of each loop iteration
i = 2, x = 2
i = 3, x = 2^2 = 4
i = 4, x = 2^4 = 16
i = 5, x = 2^16 = 65,536
So i would be 5
If I where you, I would say that it is none of the above and basically say that since x is less than 1000 when the while loop starts, the value of i is never modified. I think that it is a bad thing to leave blank answers in a quiz, it is always better to write something which you think is relevant. If you think that there is a mistake, you can always state an assumption before stating your answer, so in this case you can either say that the while loop never works, or else, you explicitly state an assumption, in this case, it would be something like "Assuming that there is a mistake in the question, and that "while (x >= 1000)" should in fact be "while (x <= 1000)"...
Then, you proceed with your working.