Confusion about Decision / Condition testing - unit-testing

For this function, i'm trying to write test sets that that cover Decision, then another set that covers Condition. I'm having trouble differentiating the two. For condition coverage, I have this:
Test case 1: A = 3, B = 4, C = 1 :: Line 4 True / True
Test case 2: A = 3, B = 4 C = 5 :: Line 4 True / False
Test case 3: A = 4, B = 3, C = 1 :: Line 4 False / True
Test case 4: A = 3, B = 4, C= 3 :: Line 5 True / True
Test case 5: A = 3, B = 4, C = 1 :: Line 5 True / False
Test case 6: A = 3, B = 3, C = 1 :: Line 5 False / False
Test case 7: A = 3, B =3, C = 3 :: Line 6 True / True
Test case 8: A = 3, B = 3, C = 1 :: Line 6 True / False
Test case 9: A = 3, B = 10, C = 1:: Line 6 False / False
Would this be correct for condition coverage?
EDIT --------------
This is my Decision coverage:
Test case 1: A = 4, B = 6, C = 2
Test case 2: A = 4, B = 6, C = 4
Test case3: A = 4, B = 2, C = 4
Is this correct for Decision?

Related

Pyomo bin sizes

I am new to pyomo. I would like to ask if there is a way to achieved this requirement.
I want my asset to be assigned to 5 different bins. Each bin will have max capacity. for example, y1 has max 50, y2 has max 20,..
some of my assets can only go to certain bin. For example, A can only go to y1, y2. B can go to y4 and y5.
minimise the number of the bin used
Currently my code shown below and the year will all be filled with at least 1 asset . But I would like if only 2 or 3 of the year are used(minimize the number of bins) and would like if assets can be placed from smallest year to highest year
from pyomo.opt import SolverFactory
value_asset = {'J': 2, 'B': 4, 'D': 18, 'C': 34, 'A': 20, 'E': 31}
bins = {'y1': 50, 'y2': 20, 'y3': 30, 'y4': 70, 'y5': 40}
Assets = {'A': ['y1', 'y2'], 'J': ['y1', 'y2'], 'E': ["y4", "y5"], 'B': ["y4", "y5"],
'D': ['y5', "y4", "y3"],
'C': ["y1", "y2", 'y3', 'y4', 'y5']}
model = pyo.ConcreteModel()
model.Assets = pyo.Set(initialize=Assets.keys())
model.budget = pyo.Set(initialize=bins.keys())
model.x = pyo.Var(model.Assets, model.budget, within=pyo.Integers, bounds=(0, None))
model.less_budget = pyo.ConstraintList()
# make sure that all the total are always less than or equal to the budget
for b in model.budget:
model.less_budget.add(expr=sum(model.x[asset, b]*value_asset[asset] for asset in model.Assets) <= bins[b])
# we want to exclude certain year that some assets cannot do
model.excluded = pyo.ConstraintList()
for asset in model.Assets:
inc = Assets[asset]
exc = list(bins.keys() - inc)
for t in exc:
model.excluded.add(expr=model.x[asset, t] == 0)
# each item can only go to 1 bin
model.one_bins = pyo.ConstraintList()
for asset in model.Assets:
model.one_bins.add(expr=sum(model.x[asset, b] for b in (model.budget )) <= 1)
model.obj = pyo.Objective(expr=sum(model.x[asset, b] for asset in model.Assets for b in model.budget),sense=pyo.maximize)
solver = pyo.SolverFactory('cbc', executable=r'C:\Users\cc\Downloads\Cbc-2.10-win64-msvc15-md\bin\cbc.exe')
solver.solve(model)
model.x.display()

Newbye in pyomo : translate a Concrete problem into Abstract

I'm encoutering some issues trying to translate a Contrete Model into Abstract one.
Months = RangeSet(6)
RequiredHours = {1: 8000, 2: 9000, 3: 9800, 4: 9900, 5: 10050, 6: 10500}
Notifications = {1: 2, 2: 0, 3: 2, 4: 0, 5: 1, 6: 0}
Costs = {'Attendants': 5100, 'Trainees': 3600}
Hours = {'Attendants': 150, 'Trainees': 25}
TraineeMax = 5
model = ConcreteModel()
model.TraineesNb = Var(Months,domain=NonNegativeIntegers,bounds=(0,TraineeMax))
AvailableAttendants = {}
AvailableTrainees = {}
for m in Months:
if m == 1:
AvailableAttendants[m] = 62
AvailableTrainees[m] = model.TraineesNb[m]
if m == 2:
AvailableAttendants[m] = AvailableAttendants[m-1] - Notifications[m-1]
AvailableTrainees[m] = model.TraineesNb[m] + model.TraineesNb[m-1]
if m > 2:
AvailableAttendants[m] = AvailableAttendants[m-1] - Notifications[m-1] + model.TraineesNb[m-2]
AvailableTrainees[m] = model.TraineesNb[m] + model.TraineesNb[m-1]
# Objective
model.Costs = Objective(
expr = sum(AvailableAttendants[m] for m in Months)*Costs["Attendants"] +
sum(AvailableTrainees[m] for m in Months)*Costs["Trainees"] ,
sense = minimize)
# declare constraints
model.NeededHours = ConstraintList()
for m in Months:
model.NeededHours.add(expr = AvailableAttendants[m]*Hours["Attendants"] +
AvailableTrainees[m]*Hours["Trainees"] >= RequiredHours[m])
The concrete model is forwarding the adequate resulys (perhaps not the most elegant code, but it works). In Discrete version of this model, an error occurs at the loop [for m in model.Months]
Thanks for your help and comments
Naji

Create a dataframe based on an old one

I have a dataframe as :
A B C D
0 s 3 a
4 s 2 a
5 s 2 a
6 s 1 a
7 s 2 b
7 s 3 b
6 s 0 b
How can I create a new dataframe as the following?
A B C D
0 4 8 4-a
7 3 5 3-b
The new dataframe summarize the old one by grouped the elements of column "D", So "A" is the index, "B" is count of elements, "C" is sum of element where "D" has the same value.
Well, assuming that your data is stored in df, it's a multistep process which could be done like this
import pandas as pd
data = {'A': {0: 0, 1: 4, 2: 5, 3: 6, 4: 7, 5: 7, 6: 6},
'B': {0: 's', 1: 's', 2: 's', 3: 's', 4: 's', 5: 's', 6: 's'},
'C': {0: 3, 1: 2, 2: 2, 3: 1, 4: 2, 5: 3, 6: 0},
'D': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'b', 5: 'b', 6: 'b'}}
df = pd.DataFrame(data)
# Handling column A (first index per value in D)
output_df = df.drop_duplicates(subset='D', keep='first')
# Itering through rows
for index, row in output_df.iterrows():
#Calcultating the counts in B
output_df.loc[index, 'B'] = df[df.D == row.D].B.count()
#Calcultating the sum in C
output_df.loc[index, 'C'] = df[df.D == row.D].C.sum()
#Finally changing values in D by concatenating values in B and D
output_df.loc[:, 'D'] = output_df.B.map(str) + "-" + output_df.D
Output :
A B C D
0 4 8 4-a
7 3 5 3-b

Accumulate monotonically increasing sequence list to a linear list

Hi I'm new to python and in programming in general.
I have a monotonic list, ex. L = [1,2,3,4,0,1,2,3,4,5,0,1,2,3,4,0,1...]
And whenever the series reaches 0, I would like to take the previous value and add to the remaining of the list so the list becomes linear.
L_sequence = [1,2,3,4,0,1,2,3,4,5,0,1,2,3,4,0,1...]
L_linear = [1,2,3,4,4,5,6,7,8,9,9,10,11,12,13,13,14...]
I know a nasty way to do this, but if any of you have a good solution, you are welcome to share.
I think it should solve your problem:
l = [1,2,3,4,0,1,2,3,4,5,0,1,2,3,4,0,1]
x = []
helperOne = 0
helperTwo = 0
goForIt = False
for i in l:
if i == 0:
goForIt = True
helperTwo = helperOne + helperTwo
if goForIt==True:
t = helperTwo + i
x.append(t)
else:
x.append(i)
helperOne = i
print x
Output:
[1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 13, 14]

Using Gurobi in Python and adding variables

I am trying to write my first Gurobi optimization code and this is where I am stuck with:
I have the following dictionary for my first subscript:
input for k in range(1,11):
i[k] = int(k)
print i
output {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}
And, I have the following dictionaries for my second subscript:
c_il = {1: 2, 2: 1, 3: 1, 4: 4, 5: 3, 6: 4, 7: 3, 8: 2, 9: 1, 10: 4}
c_iu = {1: 3, 2: 2, 3: 2, 4: 5, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 5}
I am trying to create variables as following:
x = m.addVars(i, c_il, vtype=GRB.BINARY, name="x")
x = m.addVars(i, c_iu, vtype=GRB.BINARY, name="x")
Apparently, it is not giving what I am looking for. What I am looking for is x_(i),(c_il) and x_(i),(c_iu); ignore parenthesis.
More clearly, the following is what I am trying to obtain by using dicts i, c_il, and c_iu:
{1: <gurobi.Var x[1,2]>,
2: <gurobi.Var x[2,1]>,
3: <gurobi.Var x[3,1]>,
4: <gurobi.Var x[4,5]>,
5: <gurobi.Var x[5,3]>,
6: <gurobi.Var x[6,4]>,
7: <gurobi.Var x[7,3]>,
8: <gurobi.Var x[8,2]>,
9: <gurobi.Var x[9,1]>,
10: <gurobi.Var x[10,4]>,
11: <gurobi.Var x[1,3]>,
12: <gurobi.Var x[2,2]>,
13: <gurobi.Var x[3,2]>,
14: <gurobi.Var x[4,5]>,
15: <gurobi.Var x[5,4]>,
16: <gurobi.Var x[6,5]>,
17: <gurobi.Var x[7,4]>,
18: <gurobi.Var x[8,3]>,
19: <gurobi.Var x[9,2]>,
20: <gurobi.Var x[10,5]>}
Since I am using dictionaries everywhere, I want to keep it consistent by continuing to use dictionaries so that I can do multiplications and additions with my parameters which are all in dictionaries. Is there any way to create these variables with m.addVars or m.addVar?
Thanks!
Edit: Modified to make it more clear.
It looks like you want to create 10 variables that are indexed by something. The best way to do this is to create the two indexes as lists. If you want x[12], x[21], then write:
from gurobipy import *
m = Model()
il = [ 12, 21, 31, 44, 53, 64, 73, 82, 91, 104 ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
And if you want to write x[1,2], x[2,1], then write:
from gurobipy import *
m = Model()
il = [ (1,2), (2,1), (3,1), (4,4), (5,3), (6,4), (7,3), (8,2), (9,1), (10,4) ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
After a few years of experience, I can easily write the below as an answer. Since the past myself was concerned with keeping the dictionaries as is (I highly criticize and question...), a quick solution is as follows.
x = {}
for (i,j) in c_il.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
Alternatively,
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_il.items()}
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
One liner alternative:
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in [(k,l) for (k,l) in c_il.items()] + [(k,l) for (k,l) in c_iu.items()]}