-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack.py
41 lines (29 loc) · 870 Bytes
/
knapsack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from dataclasses import dataclass
import numpy as np
@dataclass
class Problem:
"""Binary knapsack problem"""
name: str
size: int
capacity: int
weights: np.ndarray
profits: np.ndarray
optimal: float
def evaluate(self, cells: np.ndarray) -> int:
"""Calculate the profit of the cells"""
return np.dot(cells, self.profits)
def weigh(self, cells: np.ndarray) -> int:
"""Calculate the weigh of the cells"""
return np.dot(cells, self.weights)
@dataclass
class Solution:
"""Posible solution for knapsack problem"""
cells: np.ndarray
fitness: float
weight: float
def __lt__(self, other):
return self.fitness < other.fitness
def __ge__(self, other):
return self.fitness > other.fitness
def __eq__(self, other):
return self.fitness == other.fitness