From 09ed314a9382f68ae270669f4eb010402d8bbdfe Mon Sep 17 00:00:00 2001 From: Sebastian Quintero Date: Thu, 12 Sep 2024 22:32:55 -0500 Subject: [PATCH] Fix multicsv app --- .../input/items.csv | 24 +++++++++---------- .../input/weight_capacity.csv | 2 +- python-ortools-knapsack-multicsv/main.py | 6 ++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/python-ortools-knapsack-multicsv/input/items.csv b/python-ortools-knapsack-multicsv/input/items.csv index e2b2179..913b789 100644 --- a/python-ortools-knapsack-multicsv/input/items.csv +++ b/python-ortools-knapsack-multicsv/input/items.csv @@ -1,12 +1,12 @@ -"id","value","weight" -"cat",100,20 -"dog",20,45 -"water",40,2 -"phone",6,1 -"book",63,10 -"rx",81,1 -"tablet",28,8 -"coat",44,9 -"laptop",51,13 -"keys",92,1 -"nuts",18,4 +id,value,weight +cat,100,20 +dog,20,45 +water,40,2 +phone,6,1 +book,63,10 +rx,81,1 +tablet,28,8 +coat,44,9 +laptop,51,13 +keys,92,1 +nuts,18,4 diff --git a/python-ortools-knapsack-multicsv/input/weight_capacity.csv b/python-ortools-knapsack-multicsv/input/weight_capacity.csv index 0db5197..d8f8d2a 100644 --- a/python-ortools-knapsack-multicsv/input/weight_capacity.csv +++ b/python-ortools-knapsack-multicsv/input/weight_capacity.csv @@ -1,2 +1,2 @@ -"weight_capacity" +weight_capacity 50 diff --git a/python-ortools-knapsack-multicsv/main.py b/python-ortools-knapsack-multicsv/main.py index 4741838..f531eff 100644 --- a/python-ortools-knapsack-multicsv/main.py +++ b/python-ortools-knapsack-multicsv/main.py @@ -58,12 +58,12 @@ def solve(input: nextmv.Input, options: nextmv.Options) -> nextmv.Output: for item in input.data["items"]: item_variable = solver.IntVar(0, 1, item["id"]) items.append({"item": item, "variable": item_variable}) - weights += item_variable * item["weight"] - values += item_variable * item["value"] + weights += item_variable * int(item["weight"]) + values += item_variable * int(item["value"]) # This constraint ensures the weight capacity of the knapsack will not be # exceeded. - capacity = input.data["weight_capacity"][0]["weight_capacity"] # Read as a CSV. + capacity = int(input.data["weight_capacity"][0]["weight_capacity"]) # Read as a CSV. solver.Add(weights <= capacity) # Sets the objective function: maximize the value of the chosen items.