self.taken_items=[]# A list items already inserted inserted into the knapsack; i.e. items i for which branch-and-bound decided that x_i = 1

self.taken_cost=0# Cost of items in taken_items

self.capacity=instance.capacity# The remaining capacity; i.e. capacity minus weight of items in taken_items

self.update_optimal_integer_relaxed_solution()

returnself

# Create a copy of a given branch

# Copy only data that does not need to be computed by optimal_integer_relaxed_solution()

@staticmethod

definit_from_branch(branch):

self=Branch()

self.items=branch.items.copy()# We really need to copy the list since the original branch may change the list later

self.taken_items=branch.taken_items.copy()

self.taken_cost=branch.taken_cost

self.capacity=branch.capacity

returnself

# This function may be usefull to select new branch to be processed

def__lt__(self,other):

returnself.upper_bound>other.upper_bound

# Solves the optimal integer-relaxed problem

# Since items has to be ordered by density, the optimal relaxed solution constains the first k items not exceeding the capacity and a part of the next item which cannot be included completely.

# Returns a tuple of

# * Total cost of the first k items (plus cost of earlier taken items)

# * The optimal value of the relaxed problem; i.e. the upper bound on the optimal integer solution

# * k

# * The portion of the k+1 -th item which needs to be taked to completely fill the knapsack