Knapsack is a Java library. Knapsack has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. However Knapsack build file is not available. You can download it from GitHub.

4 implementations of the 0-1 knapsack problem and a comparison of their effectiveness.

Support

Support

Quality

Quality

Security

Security

License

License

Reuse

Reuse

Support

- Knapsack has a low active ecosystem.
- It has 30 star(s) with 20 fork(s). There are 4 watchers for this library.
- It had no major release in the last 12 months.
- There are 1 open issues and 0 have been closed. On average issues are closed in 1036 days. There are 1 open pull requests and 0 closed requests.
- It has a neutral sentiment in the developer community.
- The latest version of Knapsack is current.

Knapsack Support

Best in #Java

Average in #Java

Knapsack Support

Best in #Java

Average in #Java

Quality

- Knapsack has 0 bugs and 33 code smells.

Knapsack Quality

Best in #Java

Average in #Java

Knapsack Quality

Best in #Java

Average in #Java

Security

- Knapsack has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
- Knapsack code analysis shows 0 unresolved vulnerabilities.
- There are 0 security hotspots that need review.

Knapsack Security

Best in #Java

Average in #Java

Knapsack Security

Best in #Java

Average in #Java

License

- Knapsack is licensed under the MIT License. This license is Permissive.
- Permissive licenses have the least restrictions, and you can use them in most projects.

Knapsack License

Best in #Java

Average in #Java

Knapsack License

Best in #Java

Average in #Java

Reuse

- Knapsack releases are not available. You will need to build from source code and install.
- Knapsack has no build file. You will be need to create the build yourself to build the component from source.
- Knapsack saves you 114 person hours of effort in developing the same functionality from scratch.
- It has 288 lines of code, 24 functions and 8 files.
- It has medium code complexity. Code complexity directly impacts maintainability of the code.

Knapsack Reuse

Best in #Java

Average in #Java

Knapsack Reuse

Best in #Java

Average in #Java

Top functions reviewed by kandi - BETA

kandi has reviewed Knapsack and discovered the below as its top functions. This is intended to give you an instant insight into Knapsack implemented functionality, and help decide if they suit your requirements.

- Main entry point .
- Starts the trace .
- Get cell .
- Returns a string representation of the items .
- Returns a list of subsets for a list of items
- Calculate the weight for a list of items
- Get the value sum of all items
- Comparator to sort items by label .
- Create a comparator for items by percentage .

Get all kandi verified functions for this library.

Get all kandi verified functions for this library.

4 implementations of the 0-1 knapsack problem and a comparison of their effectiveness.

Using Iterative deepening DFS for knapsack similar problem

CopyDownload

```
weight = [1,1,3,4,5]
value = [2,1,5,3,5]
name = ['A','B','C','D','E']
knapsack = []
limit_weight = 8
limit_value = 8
n = 5 # number of items
def ID(maxDepth):
for i in range(maxDepth):
found, val, arr = DFS(i)
if found:
return arr
return False
def DFS(maxDepth, idx = 0, sum_val = 0, sum_weight = 0, arr = []):
if maxDepth < 0 : return False, None, None
if sum_weight > limit_weight : return False, None, None # elements weights exceed knapsack limit
if sum_val > limit_value : return True, sum_val, arr # elements values greater than required
if idx >= n : return False, None, None # no more elements and no solution (otherwise we would have returned from the previous line)
for i in range(idx,n):
n_arr = arr.copy()
n_arr.append(name[i])
found, new_sum_val, new_sum_weight = DFS(maxDepth-1, i+1, sum_val + value[i], sum_weight + weight[i], n_arr)
if found : return found, new_sum_val, new_sum_weight
return False, None, None # didn't find a solution
res = ID(4)
print(res)
```

MongoDB - bulk updating values in arrays of nested objects

CopyDownload

```
db.collection.updateMany(
{ 'chores.0': { '$exists': true } },
[
{ '$set': {
'chores': {
'$map': {
'input': '$chores',
'as': 'chore',
'in': {
'$mergeObjects': [
'$$chore',
{
'done': {
'$switch': {
'branches': [
{ 'case': { $eq: [ '$$chore.done', true ] }, then: 1 },
{ 'case': {
$eq: [
{ $ifNull: ['$$chore.done', 'Unmarked'] },
'Unmarked'
] },
then: 0
},
{ 'case': { $eq: [ '$$chore.done', false ] }, then: -1 },
],
default: 0
}
}
}
]
}
}
}
} }
]
)
```

what is the maximum possible number of monsters you can defeat?

CopyDownload

```
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int exp = s.nextInt();
int monst[] = new int[n];
int bonus[] = new int[n];
for (int i = 0; i < n; i++) {
monst[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
bonus[i] = s.nextInt();
}
class Monster {
private final int power, bonus;
public Monster(int power, int bonus){
this.power = power;
this.bonus = bonus;
}
}
Monster[] monsters = new Monster[n];
for(int i = 0; i < n; i++) monsters[i] = new Monster(monst[i], bonus[i]);
Arrays.sort(monsters, Comparator.comparingInt(m -> m.power));
int count = 0;
for(Monster m: monsters){
if(exp < m.power) break;
exp += m.bonus;
++count;
}
System.out.println(count);
}
```

```
public class Monster
{
final int m_Power;
final int m_Bonus;
public Monster( final int power, final int bonus )
{
m_Power = power;
m_Bonus = bonus;
}
public final int getPower() { return m_Power; }
public final int getBonus() { return m_Bonus; }
}
```

```
public static void main( String... args )
{
final var scanner = new Scanner( System.in );
final var n = scanner.nextInt();
final var experience = scanner.nextInt();
final var power [] = new int [n];
for( var i = 0; i < n; ++i )
{
power [i] = scanner.nextInt();
}
List<Monster> monsters = new ArrayList<>( n );
for( var i = 0; i < n; ++i )
{
monsters.add( new Monster( power [i], scanner.nextInt();
}
monsters.sort( (m1,m2) ->
{
final var p = m1.getPower() - m2.getPower();
return p == 0 ? m2.getBonus() - m1.getBonus(() : p;
} ); //*1
System.out.println( defeat( monsters, experience ) );
}
```

```
public final int defeat( final List<Monster> m, final int initialExperience )
{
var experience = initialExperience;
var retValue = 0;
final Stack<Monster> monsters = new LinkedList<>( m );
while( !monsters.empty() )
{
var monster = monsters.pop();
if( experience > monster.getPower() )
{
experience += monster.getBonus();
++retValue;
}
else break;
}
return retValue;
}
```

```
public class Monster
{
final int m_Power;
final int m_Bonus;
public Monster( final int power, final int bonus )
{
m_Power = power;
m_Bonus = bonus;
}
public final int getPower() { return m_Power; }
public final int getBonus() { return m_Bonus; }
}
```

```
public static void main( String... args )
{
final var scanner = new Scanner( System.in );
final var n = scanner.nextInt();
final var experience = scanner.nextInt();
final var power [] = new int [n];
for( var i = 0; i < n; ++i )
{
power [i] = scanner.nextInt();
}
List<Monster> monsters = new ArrayList<>( n );
for( var i = 0; i < n; ++i )
{
monsters.add( new Monster( power [i], scanner.nextInt();
}
monsters.sort( (m1,m2) ->
{
final var p = m1.getPower() - m2.getPower();
return p == 0 ? m2.getBonus() - m1.getBonus(() : p;
} ); //*1
System.out.println( defeat( monsters, experience ) );
}
```

```
public final int defeat( final List<Monster> m, final int initialExperience )
{
var experience = initialExperience;
var retValue = 0;
final Stack<Monster> monsters = new LinkedList<>( m );
while( !monsters.empty() )
{
var monster = monsters.pop();
if( experience > monster.getPower() )
{
experience += monster.getBonus();
++retValue;
}
else break;
}
return retValue;
}
```

```
public class Monster
{
final int m_Power;
final int m_Bonus;
public Monster( final int power, final int bonus )
{
m_Power = power;
m_Bonus = bonus;
}
public final int getPower() { return m_Power; }
public final int getBonus() { return m_Bonus; }
}
```

```
public static void main( String... args )
{
final var scanner = new Scanner( System.in );
final var n = scanner.nextInt();
final var experience = scanner.nextInt();
final var power [] = new int [n];
for( var i = 0; i < n; ++i )
{
power [i] = scanner.nextInt();
}
List<Monster> monsters = new ArrayList<>( n );
for( var i = 0; i < n; ++i )
{
monsters.add( new Monster( power [i], scanner.nextInt();
}
monsters.sort( (m1,m2) ->
{
final var p = m1.getPower() - m2.getPower();
return p == 0 ? m2.getBonus() - m1.getBonus(() : p;
} ); //*1
System.out.println( defeat( monsters, experience ) );
}
```

```
public final int defeat( final List<Monster> m, final int initialExperience )
{
var experience = initialExperience;
var retValue = 0;
final Stack<Monster> monsters = new LinkedList<>( m );
while( !monsters.empty() )
{
var monster = monsters.pop();
if( experience > monster.getPower() )
{
experience += monster.getBonus();
++retValue;
}
else break;
}
return retValue;
}
```

```
n=int(input())
e=int(input())
P=[]
for i in range(n):
P.append(int(input()))
B=[]
for i in range(n):
B.append(int(input()))
c=0
f=True
while n>0 and f:
f=False
i=0
while i<n:
if e>=P[i]:
e+=B[i]
P.pop(i)
B.pop(i)
n-=1
c+=1
f=True
i-=1
i+=1
print(c)
```

```
import java.io.*;
import java.util.*;
class Player {
int exp;
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
}
class Monster {
int power;
int bonus;
public int getPower() {
return this.power;
}
public int getBonus() {
return this.bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public void setPower(int power) {
this.power = power;
}
}
class Calc {
public int calc() {
Scanner sc = new Scanner(System.in);
//number of monsters
System.out.println("enter the number of monsters");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("enter the power of the player");
Player player = new Player();
player.setExp(sc.nextInt());
//declaration of array type object of monster
Monster monster[] = new Monster[n];
//value setting completed
for (int i = 0; i < n; i++) {
//allocation of object to the real
monster[i] = new Monster();
System.out.println("enter the power of monster");
monster[i].setPower(sc.nextInt());
System.out.println("enter the bonus that to be earned after killing the monster");
monster[i].setBonus(sc.nextInt());
}
//calculate win or loose
int count = 0;
int flag = 0;
//**
for (int i = 0; i < n; i++) {
if (player.getExp() >= monster[i].getPower()) {
player.setExp(player.getExp() + monster[i].getBonus());
count = count + 1;
// flag = flag + 1;
} else if (player.getExp() < monster[i].getPower()) {
for (int j = 0; j < n; j++)
arr[j] = i;
//count = count;
flag = flag + 1;
}
//**
for (int t = 0; t < flag; t++) {
if (player.getExp() >= monster[arr[t]].getPower()) {
count = count + 1;
}
}
}
return count;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("welcome to the loosing game");
Calc c = new Calc();
System.out.println("no of monster killed" + c.calc());
}
}
```

Counting the number of ways to make up a string

CopyDownload

```
#!/usr/bin/env python
def countWays(origString, toMatch, maxNum):
origLen = len(origString)
matchLen = len(toMatch)
state = {}
for i in range(origLen):
for j in range(matchLen):
o = i + j
if origString[o] != toMatch[j]:
break
state[(o, j)] = 1
sums = {}
for n in range(1, maxNum):
if not state:
break
nextState = {}
for istart, jstart in state:
prev = state[(istart, jstart)]
for i in range(istart + 1, origLen):
for j in range(jstart + 1, matchLen):
o = i + j - jstart - 1
if origString[o] != toMatch[j]:
break
nextState[(o, j)] = prev + nextState.get((o, j), 0)
sums[n] = sum(state[(i, j)] for i, j in state if j == matchLen - 1)
state = nextState
sums[maxNum] = sum(state[(i, j)] for i, j in state if j == matchLen - 1)
return sums
result = countWays(origString='ppkpke', toMatch='ppke', maxNum=5)
print('for an exact number of substrings:', result)
print(' for up to a number of substrings:', {
n: s for n, s in ((m, sum(result[k] for k in range(1, m + 1)))
for m in range(1, 1 + max(result.keys())))})
```

```
for an exact number of substrings: {1: 0, 2: 4, 3: 8, 4: 4, 5: 0}
for up to a number of substrings: {1: 0, 2: 4, 3: 12, 4: 16, 5: 16}
```

```
#!/usr/bin/env python
def countWays(origString, toMatch, maxNum):
origLen = len(origString)
matchLen = len(toMatch)
state = {}
for i in range(origLen):
for j in range(matchLen):
o = i + j
if origString[o] != toMatch[j]:
break
state[(o, j)] = 1
sums = {}
for n in range(1, maxNum):
if not state:
break
nextState = {}
for istart, jstart in state:
prev = state[(istart, jstart)]
for i in range(istart + 1, origLen):
for j in range(jstart + 1, matchLen):
o = i + j - jstart - 1
if origString[o] != toMatch[j]:
break
nextState[(o, j)] = prev + nextState.get((o, j), 0)
sums[n] = sum(state[(i, j)] for i, j in state if j == matchLen - 1)
state = nextState
sums[maxNum] = sum(state[(i, j)] for i, j in state if j == matchLen - 1)
return sums
result = countWays(origString='ppkpke', toMatch='ppke', maxNum=5)
print('for an exact number of substrings:', result)
print(' for up to a number of substrings:', {
n: s for n, s in ((m, sum(result[k] for k in range(1, m + 1)))
for m in range(1, 1 + max(result.keys())))})
```

```
for an exact number of substrings: {1: 0, 2: 4, 3: 8, 4: 4, 5: 0}
for up to a number of substrings: {1: 0, 2: 4, 3: 12, 4: 16, 5: 16}
```

```
def countWays(source, target, k, continued=False):
if len(target) == 0:
return (k == 0)
elif (k == 0 and not continued) or len(source) == 0:
return 0
elif source[0] == target[0]:
if continued:
return countWays(source[1:], target[1:], k, True) + countWays(source[1:], target[1:], k-1, True) + countWays(source[1:], target, k, False)
else:
return countWays(source[1:], target[1:], k-1, True) + countWays(source[1:], target, k, False)
else:
return countWays(source[1:], target, k, False)
print(countWays('ppkpke', 'ppke', 1))
# 0
print(countWays('ppkpke', 'ppke', 2))
# 4
print(countWays('ppkpke', 'ppke', 3))
# 8
print(countWays('ppkpke', 'ppke', 4))
# 4
print(countWays('ppkpke', 'ppke', 5))
# 0
```

How does the O(n sqrt(n)) algorithm work that lists all possible sums given an array of numbers?

CopyDownload

```
import collections
import math
def subset_sums(lst):
n = sum(lst)
dp = [False] * (n + 1)
dp[0] = True
for a, count in collections.Counter(lst).items():
dp = [min_count <= count for min_count in min_counts(dp, a)]
return {x for (x, dp_x) in enumerate(dp) if dp_x}
def min_counts(dp, a):
dp = [(0 if dp_x else math.inf) for dp_x in dp]
for x in range(a, len(dp)):
dp[x] = min(dp[x], dp[x - a] + 1)
return dp
def baseline_subset_sums(lst):
n = sum(lst)
dp = [False] * (n + 1)
dp[0] = True
for a in lst:
for x in range(len(dp) - 1, a - 1, -1):
dp[x] = dp[x] or dp[x - a]
return {x for (x, dp_x) in enumerate(dp) if dp_x}
import random
if __name__ == "__main__":
for rep in range(100000):
rand_lst = [random.randrange(20) for i in range(random.randrange(20))]
assert subset_sums(rand_lst) == baseline_subset_sums(rand_lst), rand_lst
```

Confusing Output for 0/1 Knapsack Problem

CopyDownload

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
| 0 | 1500 | 1500 | 2000 | 3500 | stereo
+---+------+------+------+------+
3500
```

```
from tabulate import tabulate
def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
# Build table K[][] in bottom up manner
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i - 1] <= w:
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w])
else:
K[i][w] = K[i - 1][w]
print(tabulate(K, tablefmt="pretty"))
return K[n][W]
# Driver code
val = [1500, 3000, 2000]
wt = [1, 4, 3]
W = 4
n = len(val)
print(knapSack(W, wt, val, n))
```

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 1500 | 3000 | stereo
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
+---+------+------+------+------+
3500
```

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
| 0 | 1500 | 1500 | 2000 | 3500 | stereo
+---+------+------+------+------+
3500
```

```
from tabulate import tabulate
def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
# Build table K[][] in bottom up manner
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i - 1] <= w:
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w])
else:
K[i][w] = K[i - 1][w]
print(tabulate(K, tablefmt="pretty"))
return K[n][W]
# Driver code
val = [1500, 3000, 2000]
wt = [1, 4, 3]
W = 4
n = len(val)
print(knapSack(W, wt, val, n))
```

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 1500 | 3000 | stereo
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
+---+------+------+------+------+
3500
```

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
| 0 | 1500 | 1500 | 2000 | 3500 | stereo
+---+------+------+------+------+
3500
```

```
from tabulate import tabulate
def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
# Build table K[][] in bottom up manner
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i - 1] <= w:
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w])
else:
K[i][w] = K[i - 1][w]
print(tabulate(K, tablefmt="pretty"))
return K[n][W]
# Driver code
val = [1500, 3000, 2000]
wt = [1, 4, 3]
W = 4
n = len(val)
print(knapSack(W, wt, val, n))
```

```
+---+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1500 | 1500 | 1500 | 1500 | guitar
| 0 | 1500 | 1500 | 1500 | 3000 | stereo
| 0 | 1500 | 1500 | 2000 | 3500 | laptop
+---+------+------+------+------+
3500
```

Knapsack problem with a total item limit in Python

CopyDownload

```
from ortools.sat.python import cp_model
# DATA
capacity = 6
money = 20
beers = [
{"name":"Beer1", "type":"Lager", "price":3.50, "score":4.1},
{"name":"Beer2", "type":"Porter", "price":4.90, "score":4.5},
{"name":"Beer3", "type":"IPA", "price":3.70, "score":4.0},
{"name":"Beer4", "type":"Stout", "price":3.20, "score":4.2},
{"name":"Beer5", "type":"Amber", "price":3.80, "score":3.9},
{"name":"Beer6", "type":"Stout", "price":2.70, "score":2.9},
{"name":"Beer7", "type":"IPA", "price":2.50, "score":3.2},
{"name":"Beer8", "type":"Pilsner", "price":3.10, "score":4.0},
{"name":"Beer9", "type":"Amber", "price":3.00, "score":4.1},
{"name":"Beer10", "type":"Porter", "price":2.80, "score":3.3},
{"name":"Beer11", "type":"IPA", "price":3.70, "score":4.0},
{"name":"Beer12", "type":"Lager", "price":3.20, "score":4.2},
{"name":"Beer13", "type":"Amber", "price":3.30, "score":3.5},
{"name":"Beer14", "type":"Stout", "price":2.90, "score":2.8},
{"name":"Beer15", "type":"Lager", "price":3.20, "score":4.2},]
# PREPROCESSING
n_beers = len(set([entry['name'] for entry in beers]))
# MODEL
model = cp_model.CpModel()
x_select = [model.NewBoolVar('') for i in range(n_beers)]
# select exactly "capacity"
model.Add(sum(x_select) == capacity)
# spend not too much -> # ASSUMPTION: * 100 makes all the values integral
model.Add(sum([x_select[i] * int(round(beers[i]['price']*100)) for i in range(n_beers)]) <= money * 100)
# >= 2 lagers needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Lager']) >= 2)
# >= 1 Stout needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Stout']) >= 1)
# >= 1 Amber needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Amber']) >= 1)
# maximize sum of scores selected -> # ASSUMPTION: * 10 makes all the values integral
model.Maximize(sum([x_select[i] * int(round(beers[i]['score']*10)) for i in range(n_beers)]))
# SOLVE
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
model.Proto().objective.scaling_factor = -1./10 # inverse scaling for solver logging output
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
selected = [i for i in range(n_beers) if solver.Value(x_select[i]) == 1]
print("\n".join([str(beers[i]) for i in selected]))
```

```
status: OPTIMAL
objective: 24.8
best_bound: 24.8
booleans: 8
conflicts: 0
branches: 19
propagations: 20
integer_propagations: 42
restarts: 17
lp_iterations: 4
walltime: 0.0689822
usertime: 0.0689823
deterministic_time: 2.03413e-05
primal_integral: 1.69201e-05
Total cuts added: 3 (out of 4 calls) worker: ''
- num simplifications: 0
- added 1 cut of type 'CG'.
- added 1 cut of type 'MIR_1'.
- added 1 cut of type 'MIR_2'.
{'name': 'Beer1', 'type': 'Lager', 'price': 3.5, 'score': 4.1}
{'name': 'Beer4', 'type': 'Stout', 'price': 3.2, 'score': 4.2}
{'name': 'Beer8', 'type': 'Pilsner', 'price': 3.1, 'score': 4.0}
{'name': 'Beer9', 'type': 'Amber', 'price': 3.0, 'score': 4.1}
{'name': 'Beer12', 'type': 'Lager', 'price': 3.2, 'score': 4.2}
{'name': 'Beer15', 'type': 'Lager', 'price': 3.2, 'score': 4.2}
```

```
from ortools.sat.python import cp_model
# DATA
capacity = 6
money = 20
beers = [
{"name":"Beer1", "type":"Lager", "price":3.50, "score":4.1},
{"name":"Beer2", "type":"Porter", "price":4.90, "score":4.5},
{"name":"Beer3", "type":"IPA", "price":3.70, "score":4.0},
{"name":"Beer4", "type":"Stout", "price":3.20, "score":4.2},
{"name":"Beer5", "type":"Amber", "price":3.80, "score":3.9},
{"name":"Beer6", "type":"Stout", "price":2.70, "score":2.9},
{"name":"Beer7", "type":"IPA", "price":2.50, "score":3.2},
{"name":"Beer8", "type":"Pilsner", "price":3.10, "score":4.0},
{"name":"Beer9", "type":"Amber", "price":3.00, "score":4.1},
{"name":"Beer10", "type":"Porter", "price":2.80, "score":3.3},
{"name":"Beer11", "type":"IPA", "price":3.70, "score":4.0},
{"name":"Beer12", "type":"Lager", "price":3.20, "score":4.2},
{"name":"Beer13", "type":"Amber", "price":3.30, "score":3.5},
{"name":"Beer14", "type":"Stout", "price":2.90, "score":2.8},
{"name":"Beer15", "type":"Lager", "price":3.20, "score":4.2},]
# PREPROCESSING
n_beers = len(set([entry['name'] for entry in beers]))
# MODEL
model = cp_model.CpModel()
x_select = [model.NewBoolVar('') for i in range(n_beers)]
# select exactly "capacity"
model.Add(sum(x_select) == capacity)
# spend not too much -> # ASSUMPTION: * 100 makes all the values integral
model.Add(sum([x_select[i] * int(round(beers[i]['price']*100)) for i in range(n_beers)]) <= money * 100)
# >= 2 lagers needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Lager']) >= 2)
# >= 1 Stout needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Stout']) >= 1)
# >= 1 Amber needed
model.Add(sum([x_select[i] for i in range(n_beers) if beers[i]['type'] == 'Amber']) >= 1)
# maximize sum of scores selected -> # ASSUMPTION: * 10 makes all the values integral
model.Maximize(sum([x_select[i] * int(round(beers[i]['score']*10)) for i in range(n_beers)]))
# SOLVE
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
model.Proto().objective.scaling_factor = -1./10 # inverse scaling for solver logging output
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
selected = [i for i in range(n_beers) if solver.Value(x_select[i]) == 1]
print("\n".join([str(beers[i]) for i in selected]))
```

```
status: OPTIMAL
objective: 24.8
best_bound: 24.8
booleans: 8
conflicts: 0
branches: 19
propagations: 20
integer_propagations: 42
restarts: 17
lp_iterations: 4
walltime: 0.0689822
usertime: 0.0689823
deterministic_time: 2.03413e-05
primal_integral: 1.69201e-05
Total cuts added: 3 (out of 4 calls) worker: ''
- num simplifications: 0
- added 1 cut of type 'CG'.
- added 1 cut of type 'MIR_1'.
- added 1 cut of type 'MIR_2'.
{'name': 'Beer1', 'type': 'Lager', 'price': 3.5, 'score': 4.1}
{'name': 'Beer4', 'type': 'Stout', 'price': 3.2, 'score': 4.2}
{'name': 'Beer8', 'type': 'Pilsner', 'price': 3.1, 'score': 4.0}
{'name': 'Beer9', 'type': 'Amber', 'price': 3.0, 'score': 4.1}
{'name': 'Beer12', 'type': 'Lager', 'price': 3.2, 'score': 4.2}
{'name': 'Beer15', 'type': 'Lager', 'price': 3.2, 'score': 4.2}
```

Allocation optimization problem using python

CopyDownload

```
int: nSectors = 5;
int: nSectorGroups = 3;
int: nTeams = 8;
set of int: SECTOR = 1..nSectors;
set of int: SECTOR_GROUP = 1..nSectorGroups;
set of int: TEAM = 1..nTeams;
array[SECTOR] of int: sectorSize = [80, 20, 10, 20, 110];
array[SECTOR] of int: sectorGroup = [1, 1, 2, 3, 3];
array[TEAM] of int: teamSize = [20, 15, 100, 20, 85, 35, 25, 7];
array[SECTOR, TEAM] of var 0..1: z; % sector is used by team?
array[SECTOR, TEAM] of var int: x; % amount of team in sector
array[TEAM] of var 0..1: w; % team is distributed on any sector?
array[SECTOR_GROUP, TEAM] of var 0..1: g; % team is distributed on a sector group?
% maximum one team per sector
constraint forall(s in SECTOR)
(sum(t in TEAM)(z[s,t]) <= 1);
% a team can not be split over different sector groups
constraint forall(t in TEAM)
(sum(sg in SECTOR_GROUP)(g[sg,t]) <= 1);
% connect g and z
constraint forall(t in TEAM, s in SECTOR)
(g[sectorGroup[s],t] >= z[s,t]);
% connect x and z
constraint forall(s in SECTOR, t in TEAM)
(x[s,t] <= max(teamSize)*z[s,t] /\
x[s,t] >= z[s,t]);
% connect w and z
constraint forall(t in TEAM)
(nTeams*w[t] >= sum(s in SECTOR)(z[s,t]) /\
w[t] <= nTeams*sum(s in SECTOR)(z[s,t]));
% team size constraint
constraint forall(t in TEAM)
(sum(s in SECTOR)(x[s,t]) = teamSize[t]*w[t]);
% sector size constraint
constraint forall(s in SECTOR)
(sum(t in TEAM)(x[s,t]) <= sectorSize[s]);
var int: obj = sum(x);
solve
maximize obj;
output ["obj=\(obj)\n"] ++
["w="] ++ [show(w)] ++ ["\n"] ++
["z=\n"] ++ [show2d(z)] ++
["x=\n"] ++ [show2d(x)] ++
["g=\n"] ++ [show2d(g)];
```

```
obj=212
w=[0, 0, 1, 1, 1, 0, 0, 1]
z=
[| 0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 1 |
0, 0, 0, 1, 0, 0, 0, 0 |
0, 0, 1, 0, 0, 0, 0, 0 |]
x=
[| 0, 0, 0, 0, 65, 0, 0, 0 |
0, 0, 0, 0, 20, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 7 |
0, 0, 0, 20, 0, 0, 0, 0 |
0, 0, 100, 0, 0, 0, 0, 0 |]
g=
[| 0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 1 |
0, 0, 1, 1, 0, 0, 0, 0 |]
```

```
int: nSectors = 5;
int: nSectorGroups = 3;
int: nTeams = 8;
set of int: SECTOR = 1..nSectors;
set of int: SECTOR_GROUP = 1..nSectorGroups;
set of int: TEAM = 1..nTeams;
array[SECTOR] of int: sectorSize = [80, 20, 10, 20, 110];
array[SECTOR] of int: sectorGroup = [1, 1, 2, 3, 3];
array[TEAM] of int: teamSize = [20, 15, 100, 20, 85, 35, 25, 7];
array[SECTOR, TEAM] of var 0..1: z; % sector is used by team?
array[SECTOR, TEAM] of var int: x; % amount of team in sector
array[TEAM] of var 0..1: w; % team is distributed on any sector?
array[SECTOR_GROUP, TEAM] of var 0..1: g; % team is distributed on a sector group?
% maximum one team per sector
constraint forall(s in SECTOR)
(sum(t in TEAM)(z[s,t]) <= 1);
% a team can not be split over different sector groups
constraint forall(t in TEAM)
(sum(sg in SECTOR_GROUP)(g[sg,t]) <= 1);
% connect g and z
constraint forall(t in TEAM, s in SECTOR)
(g[sectorGroup[s],t] >= z[s,t]);
% connect x and z
constraint forall(s in SECTOR, t in TEAM)
(x[s,t] <= max(teamSize)*z[s,t] /\
x[s,t] >= z[s,t]);
% connect w and z
constraint forall(t in TEAM)
(nTeams*w[t] >= sum(s in SECTOR)(z[s,t]) /\
w[t] <= nTeams*sum(s in SECTOR)(z[s,t]));
% team size constraint
constraint forall(t in TEAM)
(sum(s in SECTOR)(x[s,t]) = teamSize[t]*w[t]);
% sector size constraint
constraint forall(s in SECTOR)
(sum(t in TEAM)(x[s,t]) <= sectorSize[s]);
var int: obj = sum(x);
solve
maximize obj;
output ["obj=\(obj)\n"] ++
["w="] ++ [show(w)] ++ ["\n"] ++
["z=\n"] ++ [show2d(z)] ++
["x=\n"] ++ [show2d(x)] ++
["g=\n"] ++ [show2d(g)];
```

```
obj=212
w=[0, 0, 1, 1, 1, 0, 0, 1]
z=
[| 0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 1 |
0, 0, 0, 1, 0, 0, 0, 0 |
0, 0, 1, 0, 0, 0, 0, 0 |]
x=
[| 0, 0, 0, 0, 65, 0, 0, 0 |
0, 0, 0, 0, 20, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 7 |
0, 0, 0, 20, 0, 0, 0, 0 |
0, 0, 100, 0, 0, 0, 0, 0 |]
g=
[| 0, 0, 0, 0, 1, 0, 0, 0 |
0, 0, 0, 0, 0, 0, 0, 1 |
0, 0, 1, 1, 0, 0, 0, 0 |]
```

```
from collections import defaultdict
import numpy as np
from ortools.sat.python import cp_model
# INPUT
# -----
capacities = np.array([80, 20, 10, 50, 20, 110])
sector_partition = np.array([0, 0, 1, 2, 2, 2]) # 11 2 333
members = np.array([20, 15, 100, 20, 85, 35, 25, 7])
# PREPROC
# -------
n_teams = len(members)
n_sectors = len(capacities)
incompatible_sectors = defaultdict(list) # list of incompatible sectors for each sector
for sLeft in range(n_sectors):
for sRight in range(n_sectors):
if sLeft < sRight: # symmetry
if sector_partition[sLeft] != sector_partition[sRight]:
incompatible_sectors[sLeft].append(sRight)
incompatible_sectors[sRight].append(sLeft)
# MODEL
# -----
model = cp_model.CpModel()
var_assign_int = np.empty((n_teams, n_sectors), dtype=object)
var_assign_nnz_ind = np.empty((n_teams, n_sectors), dtype=object)
for t in range(n_teams):
for s in range(n_sectors):
# VAR assignment-matrix: member <->- team mapping : integer / amount
var_assign_int[t, s] = model.NewIntVar(0, min(capacities[s].item(), members[t].item()), '') # .item() because of types!
# VAR assignment-matrix: boolean indicator of above being strictly positiv
var_assign_nnz_ind[t, s] = model.NewBoolVar('')
for t in range(n_teams):
for s in range(n_sectors):
# CONSTRAINT: sector not used / not strictly positive -> no amount allowed to distribute
model.Add(var_assign_int[t, s] == 0).OnlyEnforceIf(var_assign_nnz_ind[t, s].Not())
# CONSTRAINT: sector used / strictly positive -> amount distributed over sectors must equal member cardinality
model.Add(var_assign_int[t, :].sum() == members[t].item()).OnlyEnforceIf(var_assign_nnz_ind[t, s])
for s in range(n_sectors):
# CONSTRAINT: only one team maps to this sector
model.Add(var_assign_nnz_ind[:, s].sum() <= 1)
for t in range(n_teams):
# CONSTRAINT: sector used -> every other incompat sector not used!
for incompat_s in incompatible_sectors[s]:
model.AddImplication(var_assign_nnz_ind[t, s], var_assign_nnz_ind[t, incompat_s].Not())
# OBJECTIVE
model.Maximize(var_assign_int.sum())
# SOLVE
# -----
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
status = solver.Solve(model)
# PRINT SOL
# ---------
vfunc = np.vectorize(lambda x: solver.Value(x), otypes=[int])
print(vfunc(var_assign_int))
print(vfunc(var_assign_nnz_ind))
```

```
...
CpSolverResponse summary:
status: OPTIMAL
objective: 247
best_bound: 247
booleans: 603
conflicts: 3612
branches: 3938
propagations: 557068
integer_propagations: 70215
restarts: 424
lp_iterations: 0
walltime: 0.292556
usertime: 0.292556
deterministic_time: 0.117155
primal_integral: 0.7551
[[ 0 0 0 0 20 0]
[ 0 0 0 0 0 0]
[80 20 0 0 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 85]
[ 0 0 0 35 0 0]
[ 0 0 0 0 0 0]
[ 0 0 7 0 0 0]]
[[0 0 0 0 1 0]
[0 0 0 0 0 0]
[1 1 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 1]
[0 0 0 1 0 0]
[0 0 0 0 0 0]
[0 0 1 0 0 0]]
```

```
from collections import defaultdict
import numpy as np
from ortools.sat.python import cp_model
# INPUT
# -----
capacities = np.array([80, 20, 10, 50, 20, 110])
sector_partition = np.array([0, 0, 1, 2, 2, 2]) # 11 2 333
members = np.array([20, 15, 100, 20, 85, 35, 25, 7])
# PREPROC
# -------
n_teams = len(members)
n_sectors = len(capacities)
incompatible_sectors = defaultdict(list) # list of incompatible sectors for each sector
for sLeft in range(n_sectors):
for sRight in range(n_sectors):
if sLeft < sRight: # symmetry
if sector_partition[sLeft] != sector_partition[sRight]:
incompatible_sectors[sLeft].append(sRight)
incompatible_sectors[sRight].append(sLeft)
# MODEL
# -----
model = cp_model.CpModel()
var_assign_int = np.empty((n_teams, n_sectors), dtype=object)
var_assign_nnz_ind = np.empty((n_teams, n_sectors), dtype=object)
for t in range(n_teams):
for s in range(n_sectors):
# VAR assignment-matrix: member <->- team mapping : integer / amount
var_assign_int[t, s] = model.NewIntVar(0, min(capacities[s].item(), members[t].item()), '') # .item() because of types!
# VAR assignment-matrix: boolean indicator of above being strictly positiv
var_assign_nnz_ind[t, s] = model.NewBoolVar('')
for t in range(n_teams):
for s in range(n_sectors):
# CONSTRAINT: sector not used / not strictly positive -> no amount allowed to distribute
model.Add(var_assign_int[t, s] == 0).OnlyEnforceIf(var_assign_nnz_ind[t, s].Not())
# CONSTRAINT: sector used / strictly positive -> amount distributed over sectors must equal member cardinality
model.Add(var_assign_int[t, :].sum() == members[t].item()).OnlyEnforceIf(var_assign_nnz_ind[t, s])
for s in range(n_sectors):
# CONSTRAINT: only one team maps to this sector
model.Add(var_assign_nnz_ind[:, s].sum() <= 1)
for t in range(n_teams):
# CONSTRAINT: sector used -> every other incompat sector not used!
for incompat_s in incompatible_sectors[s]:
model.AddImplication(var_assign_nnz_ind[t, s], var_assign_nnz_ind[t, incompat_s].Not())
# OBJECTIVE
model.Maximize(var_assign_int.sum())
# SOLVE
# -----
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
status = solver.Solve(model)
# PRINT SOL
# ---------
vfunc = np.vectorize(lambda x: solver.Value(x), otypes=[int])
print(vfunc(var_assign_int))
print(vfunc(var_assign_nnz_ind))
```

```
...
CpSolverResponse summary:
status: OPTIMAL
objective: 247
best_bound: 247
booleans: 603
conflicts: 3612
branches: 3938
propagations: 557068
integer_propagations: 70215
restarts: 424
lp_iterations: 0
walltime: 0.292556
usertime: 0.292556
deterministic_time: 0.117155
primal_integral: 0.7551
[[ 0 0 0 0 20 0]
[ 0 0 0 0 0 0]
[80 20 0 0 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 85]
[ 0 0 0 35 0 0]
[ 0 0 0 0 0 0]
[ 0 0 7 0 0 0]]
[[0 0 0 0 1 0]
[0 0 0 0 0 0]
[1 1 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 1]
[0 0 0 1 0 0]
[0 0 0 0 0 0]
[0 0 1 0 0 0]]
```

```
# Team Assignment
import pyomo.environ as pyo
### DATA
sectors = {
"1A": 80,
"1B": 20,
"2A": 10,
"3A": 50,
"3B": 20,
"3C": 110
}
teams = {
"TeamA":20,
"TeamB":15,
"TeamC":100,
"TeamD":20,
"TeamF":85,
"TeamG":35,
"TeamH":25,
"TeamI":7,
}
sector_zones = { s:s[0] for s in sectors} # a pairing of the sector with the zone (first digit)
### SETS
m = pyo.ConcreteModel("Team Assignment")
m.S = pyo.Set(initialize=sectors.keys())
m.T = pyo.Set(initialize=teams.keys())
m.Z = pyo.Set(initialize=set(sector_zones.values()))
m.SZ = pyo.Set(within=m.S*m.Z,initialize=[(s,z) for (s,z) in sector_zones.items()])
### PARAMS
m.sector_cap = pyo.Param(m.S, initialize=sectors)
m.team_size = pyo.Param(m.T, initialize=teams)
### VARS
# assign X players from team T to sector S in zone Z
m.X = pyo.Var(m.T, m.SZ, domain=pyo.NonNegativeIntegers)
# include team T at sector S
m.team_sector = pyo.Var(m.T, m.S, domain=pyo.Binary)
# include team T in zone Z
m.team_zone = pyo.Var(m.T, m.Z, domain=pyo.Binary)
### OBJ
m.obj = pyo.Objective(expr=sum(m.X[t,s,z] for t in m.T for s,z in m.SZ), sense=pyo.maximize)
### CONSTRAINTS
# All-or-nothing in a particular zone
def all_or_not(m, t):
return sum(m.X[t,s,z] for s,z in m.SZ) == sum(m.team_zone[t,z] * m.team_size[t] for z in m.Z)
m.C1 = pyo.Constraint(m.T, rule=all_or_not)
# Don't bust sector limit
def sector_lim(m, t, s, z):
return m.X[t,s,z] <= m.team_sector[t,s] * m.sector_cap[s]
m.C2 = pyo.Constraint(m.T, m.SZ, rule=sector_lim)
# 1 team per sector max
def one_per_sector(m, s):
return sum(m.team_sector[t,s] for t in m.T) <= 1
m.C3 = pyo.Constraint(m.S, rule=one_per_sector)
# link sector assignment to zone
def sector_zone(m, t, z):
return sum(m.team_sector[t,s] for s in m.S if (s,z) in m.SZ) <= \
m.team_zone[t, z] * len(m.S)
m.C4 = pyo.Constraint(m.T, m.Z, rule=sector_zone)
# 1 zone assignment per team
def one_zone(m, t):
return sum(m.team_zone[t,z] for z in m.Z) <= 1
m.C5 = pyo.Constraint(m.T, rule=one_zone)
### Solve
solver = pyo.SolverFactory('cbc')
res = solver.solve(m)
print(res)
#m.X.display()
assigned = 0
for idx in m.X.keys():
val = m.X[idx].value
if val:
print(f'assign {val} from {idx[0]} to {idx[1]}')
assigned += val
print(f'total assigned: {assigned}')
```

```
assign 20.0 from TeamA to 3B
assign 80.0 from TeamC to 1A
assign 20.0 from TeamC to 1B
assign 85.0 from TeamF to 3C
assign 35.0 from TeamG to 3A
assign 7.0 from TeamI to 2A
total assigned: 247.0
```

```
# Team Assignment
import pyomo.environ as pyo
### DATA
sectors = {
"1A": 80,
"1B": 20,
"2A": 10,
"3A": 50,
"3B": 20,
"3C": 110
}
teams = {
"TeamA":20,
"TeamB":15,
"TeamC":100,
"TeamD":20,
"TeamF":85,
"TeamG":35,
"TeamH":25,
"TeamI":7,
}
sector_zones = { s:s[0] for s in sectors} # a pairing of the sector with the zone (first digit)
### SETS
m = pyo.ConcreteModel("Team Assignment")
m.S = pyo.Set(initialize=sectors.keys())
m.T = pyo.Set(initialize=teams.keys())
m.Z = pyo.Set(initialize=set(sector_zones.values()))
m.SZ = pyo.Set(within=m.S*m.Z,initialize=[(s,z) for (s,z) in sector_zones.items()])
### PARAMS
m.sector_cap = pyo.Param(m.S, initialize=sectors)
m.team_size = pyo.Param(m.T, initialize=teams)
### VARS
# assign X players from team T to sector S in zone Z
m.X = pyo.Var(m.T, m.SZ, domain=pyo.NonNegativeIntegers)
# include team T at sector S
m.team_sector = pyo.Var(m.T, m.S, domain=pyo.Binary)
# include team T in zone Z
m.team_zone = pyo.Var(m.T, m.Z, domain=pyo.Binary)
### OBJ
m.obj = pyo.Objective(expr=sum(m.X[t,s,z] for t in m.T for s,z in m.SZ), sense=pyo.maximize)
### CONSTRAINTS
# All-or-nothing in a particular zone
def all_or_not(m, t):
return sum(m.X[t,s,z] for s,z in m.SZ) == sum(m.team_zone[t,z] * m.team_size[t] for z in m.Z)
m.C1 = pyo.Constraint(m.T, rule=all_or_not)
# Don't bust sector limit
def sector_lim(m, t, s, z):
return m.X[t,s,z] <= m.team_sector[t,s] * m.sector_cap[s]
m.C2 = pyo.Constraint(m.T, m.SZ, rule=sector_lim)
# 1 team per sector max
def one_per_sector(m, s):
return sum(m.team_sector[t,s] for t in m.T) <= 1
m.C3 = pyo.Constraint(m.S, rule=one_per_sector)
# link sector assignment to zone
def sector_zone(m, t, z):
return sum(m.team_sector[t,s] for s in m.S if (s,z) in m.SZ) <= \
m.team_zone[t, z] * len(m.S)
m.C4 = pyo.Constraint(m.T, m.Z, rule=sector_zone)
# 1 zone assignment per team
def one_zone(m, t):
return sum(m.team_zone[t,z] for z in m.Z) <= 1
m.C5 = pyo.Constraint(m.T, rule=one_zone)
### Solve
solver = pyo.SolverFactory('cbc')
res = solver.solve(m)
print(res)
#m.X.display()
assigned = 0
for idx in m.X.keys():
val = m.X[idx].value
if val:
print(f'assign {val} from {idx[0]} to {idx[1]}')
assigned += val
print(f'total assigned: {assigned}')
```

```
assign 20.0 from TeamA to 3B
assign 80.0 from TeamC to 1A
assign 20.0 from TeamC to 1B
assign 85.0 from TeamF to 3C
assign 35.0 from TeamG to 3A
assign 7.0 from TeamI to 2A
total assigned: 247.0
```

How to solve this variant of the Knapsack problem?

CopyDownload

```
a = [10, 20, 5, 12]
t = [2, 3, 3, 1]
```

```
[(1, 12), (2, 10), (3, 20), (3, 5)]
```

```
a = [10, 5, 7, 20, 15, 1]
t = [2, 2, 2, 3, 3, 1]
[(1, 1), (2, 10), (2, 7), (2, 5), (3, 20), (3, 15)]
```

```
import heapq
def get_max_shipping_cost(a, t):
if len(a) == 0:
return 0
items = sorted(zip(t,a), key = lambda tup: (tup[0], -tup[1]))
l = []
heapq.heappush(l, items[0][1])
s = items[0][1]
i = 1
prev = items[0]
while i < len(items):
if items[i][0] == prev[0]:
prev = items[i]
if s - l[0] + items[i][1] > s:
s = s - l[0] + items[i][1]
heapq.heappop(l)
heapq.heappush(l,items[i][1])
i += 1
elif items[i][0] == prev[0] + 1:
prev = items[i]
heapq.heappush(l,items[i][1])
s += items[i][1]
i += 1
else:
prev = (prev[0] + 1, 0)
heapq.heappush(l,0)
return s
```

```
a = [10, 20, 5, 12]
t = [2, 3, 3, 1]
```

```
[(1, 12), (2, 10), (3, 20), (3, 5)]
```

```
a = [10, 5, 7, 20, 15, 1]
t = [2, 2, 2, 3, 3, 1]
[(1, 1), (2, 10), (2, 7), (2, 5), (3, 20), (3, 15)]
```

```
import heapq
def get_max_shipping_cost(a, t):
if len(a) == 0:
return 0
items = sorted(zip(t,a), key = lambda tup: (tup[0], -tup[1]))
l = []
heapq.heappush(l, items[0][1])
s = items[0][1]
i = 1
prev = items[0]
while i < len(items):
if items[i][0] == prev[0]:
prev = items[i]
if s - l[0] + items[i][1] > s:
s = s - l[0] + items[i][1]
heapq.heappop(l)
heapq.heappush(l,items[i][1])
i += 1
elif items[i][0] == prev[0] + 1:
prev = items[i]
heapq.heappush(l,items[i][1])
s += items[i][1]
i += 1
else:
prev = (prev[0] + 1, 0)
heapq.heappush(l,0)
return s
```

```
a = [10, 20, 5, 12]
t = [2, 3, 3, 1]
```

```
[(1, 12), (2, 10), (3, 20), (3, 5)]
```

```
a = [10, 5, 7, 20, 15, 1]
t = [2, 2, 2, 3, 3, 1]
[(1, 1), (2, 10), (2, 7), (2, 5), (3, 20), (3, 15)]
```

```
import heapq
def get_max_shipping_cost(a, t):
if len(a) == 0:
return 0
items = sorted(zip(t,a), key = lambda tup: (tup[0], -tup[1]))
l = []
heapq.heappush(l, items[0][1])
s = items[0][1]
i = 1
prev = items[0]
while i < len(items):
if items[i][0] == prev[0]:
prev = items[i]
if s - l[0] + items[i][1] > s:
s = s - l[0] + items[i][1]
heapq.heappop(l)
heapq.heappush(l,items[i][1])
i += 1
elif items[i][0] == prev[0] + 1:
prev = items[i]
heapq.heappush(l,items[i][1])
s += items[i][1]
i += 1
else:
prev = (prev[0] + 1, 0)
heapq.heappush(l,0)
return s
```

```
a = [10, 20, 5, 12]
t = [2, 3, 3, 1]
```

```
[(1, 12), (2, 10), (3, 20), (3, 5)]
```

```
a = [10, 5, 7, 20, 15, 1]
t = [2, 2, 2, 3, 3, 1]
[(1, 1), (2, 10), (2, 7), (2, 5), (3, 20), (3, 15)]
```

```
import heapq
def get_max_shipping_cost(a, t):
if len(a) == 0:
return 0
items = sorted(zip(t,a), key = lambda tup: (tup[0], -tup[1]))
l = []
heapq.heappush(l, items[0][1])
s = items[0][1]
i = 1
prev = items[0]
while i < len(items):
if items[i][0] == prev[0]:
prev = items[i]
if s - l[0] + items[i][1] > s:
s = s - l[0] + items[i][1]
heapq.heappop(l)
heapq.heappush(l,items[i][1])
i += 1
elif items[i][0] == prev[0] + 1:
prev = items[i]
heapq.heappush(l,items[i][1])
s += items[i][1]
i += 1
else:
prev = (prev[0] + 1, 0)
heapq.heappush(l,0)
return s
```

GEKKO knapsack Optimization can't reach the right result

CopyDownload

```
m.options.RTOL = 1e-10
m.options.OTOL = 1e-10
m.solver_options = ['minlp_gap_tol 1.0e-5',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 10000',\
'minlp_integer_tol 1e-8']
```

```
from gekko import GEKKO
input_data = '30 100000\n90000 90001\n89750 89751\n10001 10002\n89500 89501\n10252 10254\n89250 89251\n10503 10506\n89000 89001\n10754 10758\n88750 88751\n11005 11010\n88500 88501\n11256 11262\n88250 88251\n11507 11514\n88000 88001\n11758 11766\n87750 87751\n12009 12018\n87500 87501\n12260 12270\n87250 87251\n12511 12522\n87000 87001\n12762 12774\n86750 86751\n13013 13026\n86500 86501\n13264 13278\n86250 86251\n'
# parse the input
lines = input_data.split('\n')
firstLine = lines[0].split()
item_count = int(firstLine[0])
capacity = int(firstLine[1])
v = []
w = []
for i in range(1, item_count+1):
line = lines[i]
parts = line.split()
v.append(int(parts[0]))
w.append(int(parts[1]))
print('v: ')
print(v[0:3])
print('w: ')
print(w[0:3])
print('capacity:')
print(capacity)
# Create model
m = GEKKO(remote = False)
m.options.RTOL = 1e-10
m.options.OTOL = 1e-10
m.solver_options = ['minlp_gap_tol 1.0e-5',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 10000',\
'minlp_integer_tol 1e-8']
x = m.Array(m.Var,item_count,lb=0,ub=1,integer=True)
#sol1 = [1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#sol2 = [0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0]
#for i,xi in enumerate(x):
# xi.value = sol1[i]
m.Maximize(m.sum([v[i]*x[i] for i in range(item_count)]))
s = m.Var()
m.Equation(s==m.sum([w[i]*x[i] for i in range(item_count)]))
m.Equation(s <= capacity)
m.options.SOLVER = 1
m.solve()
value = int((m.options.objfcnval)*-1)
taken = []
for i in range(item_count):
taken.append(int(x[i].value[0]))
print(taken)
print(s.value[0])
```

```
m.options.RTOL = 1e-10
m.options.OTOL = 1e-10
m.solver_options = ['minlp_gap_tol 1.0e-5',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 10000',\
'minlp_integer_tol 1e-8']
```

```
from gekko import GEKKO
input_data = '30 100000\n90000 90001\n89750 89751\n10001 10002\n89500 89501\n10252 10254\n89250 89251\n10503 10506\n89000 89001\n10754 10758\n88750 88751\n11005 11010\n88500 88501\n11256 11262\n88250 88251\n11507 11514\n88000 88001\n11758 11766\n87750 87751\n12009 12018\n87500 87501\n12260 12270\n87250 87251\n12511 12522\n87000 87001\n12762 12774\n86750 86751\n13013 13026\n86500 86501\n13264 13278\n86250 86251\n'
# parse the input
lines = input_data.split('\n')
firstLine = lines[0].split()
item_count = int(firstLine[0])
capacity = int(firstLine[1])
v = []
w = []
for i in range(1, item_count+1):
line = lines[i]
parts = line.split()
v.append(int(parts[0]))
w.append(int(parts[1]))
print('v: ')
print(v[0:3])
print('w: ')
print(w[0:3])
print('capacity:')
print(capacity)
# Create model
m = GEKKO(remote = False)
m.options.RTOL = 1e-10
m.options.OTOL = 1e-10
m.solver_options = ['minlp_gap_tol 1.0e-5',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 10000',\
'minlp_integer_tol 1e-8']
x = m.Array(m.Var,item_count,lb=0,ub=1,integer=True)
#sol1 = [1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#sol2 = [0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0]
#for i,xi in enumerate(x):
# xi.value = sol1[i]
m.Maximize(m.sum([v[i]*x[i] for i in range(item_count)]))
s = m.Var()
m.Equation(s==m.sum([w[i]*x[i] for i in range(item_count)]))
m.Equation(s <= capacity)
m.options.SOLVER = 1
m.solve()
value = int((m.options.objfcnval)*-1)
taken = []
for i in range(item_count):
taken.append(int(x[i].value[0]))
print(taken)
print(s.value[0])
```

QUESTION

To which Knapsack-problem variation does this problem correspond?

Asked 2022-Mar-19 at 17:06- Each item has an associated weight wi and profit pi
- With a maximum total weight Wmax

- There are categories of items and I have to choose
**exactly one item**from each category - Of course, the aim is to choose items to
**maximise the sum of the profits**

Books | Books weights | Books profits | Food | Food weights | Food profits |
---|---|---|---|---|---|

The Bible | 500 | 25 | Cheese | 80 | 120 |

The little prince | 150 | 5 | Banana | 250 | 200 |

Here, the best solution is (The little prince, Banana)

**I have a similar problem and I'd like to find out the best way to code it but I can't figure out what version/ variation of the probleme this is, is it a known variation ?**

ANSWER

Answered 2022-Mar-19 at 17:06I’m not sure if there’s an existing variation that matches yours, but it’s easy to draw inference from the classical variant and solve this.

Classic variant has 2D dynamic programming (`DP[N][W]`

, where `N`

and `W`

are number of items and max weight).

In this variant, since we can only pick one of each category, you can use 3D DP like `dp[i][w][j]`

, which denotes the maximum value you can get from the first `i`

items with weight `w`

and `j`

is a 0/1 int denoting whether an item from category number `j`

has been selected or not.

I’ll leave the implementation, since the recursive relation is relatively simple and quite similar to the classic variant.

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

No vulnerabilities reported

You can download it from GitHub.

You can use Knapsack like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the Knapsack component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

You can use Knapsack like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the Knapsack component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

For any new features, suggestions and bugs create an issue on GitHub.
If you have any questions check and ask questions on community page Stack Overflow .

over 430 million Knowledge Items

Save this library and start creating your kit

over 430 million Knowledge Items

Save this library and start creating your kit