Skip to content

Commit

Permalink
Still need to work on knapsack problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugarosa committed May 4, 2020
1 parent 7e16750 commit 7b647ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/markers/create_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from opytimark.markers.boolean import Knapsack

# Declaring a function from the `boolean` package
f = Knapsack()
f = Knapsack(costs=[55, 10, 47, 5, 4], weights=[95, 4, 60, 32, 23], max_capacity=100)

# Declaring an input variable for feeding the function
x = np.array([1, 0, 1, 0])
x = np.array([1, 0, 1, 0, 1])

# Printing out the function's output
print(f(x))
32 changes: 25 additions & 7 deletions opytimark/markers/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import opytimark.utils.decorator as d
import opytimark.utils.exception as e
from opytimark.core import Benchmark


Expand All @@ -20,7 +21,7 @@ class Knapsack(Benchmark):
"""

def __init__(self, name='Knapsack', dims=-1, continuous=False, convex=False,
differentiable=False, multimodal=False, separable=False):
differentiable=False, multimodal=False, separable=False, costs=None, weights=None, max_capacity=0):
"""Initialization method.
Args:
Expand All @@ -31,13 +32,33 @@ def __init__(self, name='Knapsack', dims=-1, continuous=False, convex=False,
differentiable (bool): Whether the function is differentiable.
multimodal (bool): Whether the function is multimodal.
separable (bool): Whether the function is separable.
costs (list): List of items costs.
weights (list): List of items weights.
max_capacity: Maximum capacity of the knapsack.
"""

# Override its parent class
super(Knapsack, self).__init__(name, dims, continuous,
convex, differentiable, multimodal, separable)

# Checking if costs and weights have the same length
if len(costs) != len(weights):
raise e.SizeError('`costs` and `weights` needs to have the same size')

#
self.costs = costs

#
self.weights = weights

#
self.max_capacity = max_capacity

#
self.dims = len(costs)


@d.check_dimension
def __call__(self, x):
"""This method returns the function's output when the class is called.
Expand All @@ -50,13 +71,10 @@ def __call__(self, x):
"""

a = [55, 10, 47, 5, 4, 50, 8, 61, 85, 87]
c = [95, 4, 60, 32, 23, 72, 80, 62, 65, 46]

fixedCapacity = 269
fixedCapacity = self.max_capacity

profit = list(it.compress(a, x))
capacity = list(it.compress(c, x))
profit = list(it.compress(self.costs, x))
capacity = list(it.compress(self.weights, x))
n = len(capacity)

mat = [[0 for i in range(fixedCapacity + 1)]
Expand Down

0 comments on commit 7b647ac

Please sign in to comment.