how to add constraint in Pulp BinPacking such that two items are not assigned in same bag - constraint-programming

I'm using this code and want to add a constraint such that say item a and b are not assigned in the same bag

Let
x(i,j) = 1 if item i is placed in bin j
0 otherwise
Then
x(a,j) + x(b,j) <= 1 for all j
will prevent a and b to be placed in the same bin.

Related

Updating Pandas data fram cells by condition

I have a data frame and want to update specific cells in a column based on a condition on another column.
ID Name Metric Unit Value
2 1 K2 M1 msecond 1
3 1 K2 M2 NaN 10
4 2 K2 M1 usecond 500
5 2 K2 M2 NaN 8
The condition is, if Unit string is msecond, then multiply the corresponding value in Value column by 1000 and store it in the same place. Considering a constant step for row iteration (two-by-two), the following code is not correct
i = 0
while i < len(df_group):
x = df.iloc[i].at["Unit"]
if x == 'msecond':
df.iloc[i].at["Value"] = df.iloc[i].at["Value"] * 1000
i += 2
However, the output is the same as before modifications. How can I fix that? Also what are the alternatives for better coding instead of that while loop?
A much simpler (and more efficient) form would be to use loc:
df.loc[df['Unit'] == 'msecond', 'Value'] *= 100
If you consider it essentially to only update a specific step of indexes:
step = 2
start = 0
df.loc[df['Unit'].eq('msecond') & (df.index % step == start), 'Value'] *= 100

for given data i need to find the count of "a" in the column Key

Key
----------
0 a
1 a
2 b
3 b
4 a
5 c
so far i tried this:
df.groupby(["key1"],).count()
However it is also showing the counts of b and c, i want only for a.
Create mask and count by sum:
df["Key"].eq('a').sum()

how do i create decision variables

how can i create decision variable of type xp.sos(fico xpress solver) from a list like this
`i have 2 lists K and D with these set of elements as seen below.K is a list of trips and J is a list of machines
K= ['D-1-D', 'D-2-D', 'D-3-D', 'D-4-D', 'D-4-2-1-D', 'D-2-1-D', 'D-4-1-D', 'D-2-4-3-D'].
J= ["A","B"]
with decision variables
1) Y_{kj} is 0 or 1, equal to 1 if trip k is executed by UAV j
2)Z_{k1k2}^j is 0 or 1, equal to 1 if trip k1 and trip k2 are executed in sequence by UAV j and 0 otherwise

Table with 4 variable columns

I'm sure this is simple for all of you, but I'm new here. How do I create a formula or code that can output all of the potential scenarios for this type of array below? Basically, max is 60, min is 0, but I'm unsure how to make Excel spit out a table that represents this.
Solution:
Doing it with Excel formulas alone, while theoretically possible, is incredibly computationally demanding and crashed Excel when I was trying to do so. You can do this fairly easy with VBA though.
Create a VBA module, drop these two snippets in, press play, and wait for a few seconds to a minute while the code runs. The code is not the most efficient, but it is probably the simplest algorithm to understand.
Public Sub comb4()
Dim a, b, c, d, n, r, x As Integer
x = 60
a = x
Do While a >= n
b = x - a
Do While b >= n
c = x - a - b
Do While c >= n
d = x - a - b - c
Do While d >= n
If sumToZero(-x, a, b, c, d) Then
r = r + 1
Cells(r, 1).Value = a
Cells(r, 2).Value = b
Cells(r, 3).Value = c
Cells(r, 4).Value = d
End If
d = d - 1
Loop
c = c - 1
Loop
b = b - 1
Loop
a = a - 1
Loop
End Sub
Public Function sumToZero(ParamArray intNums())
For x = LBound(intNums) To UBound(intNums)
y = y + intNums(x)
Next x
If y = 0 Then sumToZero = True
End Function
Code explanation:
x is the max that you defined, while n is the min you defined.
r is a counter which helps us track what row to print to.
Since we always want to count down to 0 for each column's sub-permutations, each of our Do While loops will count from the theoretical maximum value down to 0.
The loop structure is nested so that each time we hit -1 in a column N we instead leave the loop, go into the loop one level higher, and decrease the value in column M by 1. The value in N is reset when we start the next instance of the loop for M.
Between each value change we need to check to see if the result is a valid solution. We pass the (negative) max and all 4 variables we are looping to a function which sums the values and returns true if the variables are equal to the max. When the function is true, we go to the next row in Excel and print the four values.

Comparing Range Between Two Arrays

I have two different arrays:
A = [1,6,8]
B = [2,5,6,9]
I want to check whether there is at least one element from B exist in Range of A
Example:
Let's say I have two values X = 1 and Y = 8 from list A.
I want to check whether there is such element in list B exist within the range of X and Y.
The answer that I expected is for python to tell me that 2, 5, 6 are
elements from B that fit in the range of A[0] and A[2]
I tried:
for n in range(x,y)
if n in B
but it didn't work.
Did I do it wrong?
This is how you can do it.
list_1 = [1, 6, 8]
list_2 = [2,5,5,8]
for n in range (list_1 [0], list_1 [2]):
if n in list_2:
print n
I want to check whether there is at least one element from B exist in Range of A
To find whether any element from B exists in A:
exists = not set(A).isdisjoint(B)
that is O(len(A) + len(B)) in time unlike exists = any(b in A for b in B) that is O(len(A) * len(B)) algorithm e.g., if A, B have one million elements each then the first method (set.isdisjoint()) requires around a million (10**6) operations while the second method (with the loop) requires around a million of millions (10**12) operations that is much too slow.
To find whether any element from B exists in range(A[0], A[2]):
exists = any(A[0] <= b < A[2] for b in B)

Resources