Answered step by step
Verified Expert Solution
Question
1 Approved Answer
KPItem: public class KPItem{ //declare fields associated with an item for the problem given in Q2 } KPItemInfo: import com.opencsv.bean.CsvBindByName; public class KPItemInfo { }
KPItem:
public class KPItem{ //declare fields associated with an item for the problem given in Q2 }
KPItemInfo:
import com.opencsv.bean.CsvBindByName; public class KPItemInfo { }
H3_Q2:
import com.opencsv.bean.CsvToBeanBuilder; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.LinkedList; import java.util.List; public class H3_Q2 { public static void main(String[] args) throws FileNotFoundException { // LinkedListQuestion 2 Consider the following optimization problem: s.t. I w;1; sc I; {0,1,2} 1 Sin Notice that this version of the knapsack problem contains a single knapsack, but either 0. 1 or 2 of each item can be added to the knapsack. You are asked to solve this problem in the questions. a. Modify the class KPtemInfo so that it binds the columns in KP012_Item_Data.csv to fields in the class using OpenCSV. b. Add to the class KPItem appropriate constructers and fields for w, and p in the problem given above. c. In H3_Q2, write a static method called load Problem with the following properties: arguments: Linked List named emph{items} containing objects of type KPItem return: nothing functionality: reads values from Item_Data.csv and adds them to items d. In H3_Q2, write a static method called solveKP012 that solves the {0,1.2} knapsack problem and returns an integer. The method should be recursive. You should determine what arguments the method accepts (hint: again, think about how we handled the 1 knapsack problem in class and expand upon it). e. Add a line of code to the end of the main method that outputs the optimal solution to the {0.1.2}- knapsack problem. Question 2 Consider the following optimization problem: s.t. I w;1; sc I; {0,1,2} 1 Sin Notice that this version of the knapsack problem contains a single knapsack, but either 0. 1 or 2 of each item can be added to the knapsack. You are asked to solve this problem in the questions. a. Modify the class KPtemInfo so that it binds the columns in KP012_Item_Data.csv to fields in the class using OpenCSV. b. Add to the class KPItem appropriate constructers and fields for w, and p in the problem given above. c. In H3_Q2, write a static method called load Problem with the following properties: arguments: Linked List named emph{items} containing objects of type KPItem return: nothing functionality: reads values from Item_Data.csv and adds them to items d. In H3_Q2, write a static method called solveKP012 that solves the {0,1.2} knapsack problem and returns an integer. The method should be recursive. You should determine what arguments the method accepts (hint: again, think about how we handled the 1 knapsack problem in class and expand upon it). e. Add a line of code to the end of the main method that outputs the optimal solution to the {0.1.2}- knapsackitems = new LinkedList(); int c = 9; //Uncomment line below once loadProblem method is complete // loadProblem(items); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started