Package org.ddolib.examples.knapsack
Class KSProblem
java.lang.Object
org.ddolib.examples.knapsack.KSProblem
Represents an instance of the Knapsack Problem (KS).
The state of the problem is the remaining capacity of the knapsack (represented as an Integer).
Decisions correspond to selecting or not selecting a specific item (1 for selected, 0 for not selected).
This class supports creating instances from:
- Explicit arrays of profits and weights, with or without a known optimal value.
- A file formatted with the number of items, capacity, (optional) optimal value, and a list of item profits and weights.
Costs are negated profits to allow using solvers designed for minimization.
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal intMaximum capacity of the knapsack.Optional name of the instance (usually the filename).Optional known optimal solution value.final int[]Profits of the items.final int[]Weights of the items. -
Constructor Summary
ConstructorsConstructorDescriptionKSProblem(int capa, int[] profit, int[] weight) Constructs a Knapsack problem with given capacity, profits, and weights.KSProblem(int capa, int[] profit, int[] weight, double optimal) Constructs a Knapsack problem with given capacity, profits, weights, and known optimal value.Constructs a Knapsack problem from a file. -
Method Summary
Modifier and TypeMethodDescriptionReturns the domain of possible values for a given variable when applied to a specific state.doubleevaluate(int[] solution) Given a solution such thatsolution[i]is the value of the variablex_i, returns the value of this solution and checks if the solution respects the problem's constraints.Returns the initial state of the problem.doubleReturns the initial objective value associated with the initial state.intnbVars()Returns the known optimal value of the problem, if available.toString()transition(Integer state, Decision decision) Applies a decision to a state, computing the next state according to the problem's transition function.doubletransitionCost(Integer state, Decision decision) Computes the change in objective value resulting from applying a decision to a given state.
-
Field Details
-
capa
public final int capaMaximum capacity of the knapsack. -
profit
public final int[] profitProfits of the items. -
weight
public final int[] weightWeights of the items. -
optimal
Optional known optimal solution value. -
name
Optional name of the instance (usually the filename).
-
-
Constructor Details
-
KSProblem
public KSProblem(int capa, int[] profit, int[] weight, double optimal) Constructs a Knapsack problem with given capacity, profits, weights, and known optimal value.- Parameters:
capa- maximum capacity of the knapsackprofit- array of item profitsweight- array of item weightsoptimal- known optimal value
-
KSProblem
public KSProblem(int capa, int[] profit, int[] weight) Constructs a Knapsack problem with given capacity, profits, and weights. No optimal value is provided.- Parameters:
capa- maximum capacity of the knapsackprofit- array of item profitsweight- array of item weights
-
KSProblem
Constructs a Knapsack problem from a file.The file format should contain:
- First line: number of items, capacity, (optional) optimal value.
- Following lines: item profit and weight for each item.
- Parameters:
fname- path to the file- Throws:
IOException- if an I/O error occurs while reading the file
-
-
Method Details
-
toString
-
nbVars
public int nbVars() -
initialState
Description copied from interface:ProblemReturns the initial state of the problem.- Specified by:
initialStatein interfaceProblem<Integer>- Returns:
- the state representing the starting point of the optimization
-
initialValue
public double initialValue()Description copied from interface:ProblemReturns the initial objective value associated with the initial state.- Specified by:
initialValuein interfaceProblem<Integer>- Returns:
- the starting value of the objective function
-
domain
Description copied from interface:ProblemReturns the domain of possible values for a given variable when applied to a specific state. -
transition
Description copied from interface:ProblemApplies a decision to a state, computing the next state according to the problem's transition function.- Specified by:
transitionin interfaceProblem<Integer>- Parameters:
state- the state from which the transition originatesdecision- the decision to apply- Returns:
- the resulting state after applying the decision
-
transitionCost
Description copied from interface:ProblemComputes the change in objective value resulting from applying a decision to a given state.- Specified by:
transitionCostin interfaceProblem<Integer>- Parameters:
state- the state from which the transition originatesdecision- the decision to apply- Returns:
- the incremental objective cost/value associated with this decision
-
optimalValue
Description copied from interface:ProblemReturns the known optimal value of the problem, if available.Note: This value should correspond to the expected output of the solver. For maximization problems, be careful with negative values.
- Specified by:
optimalValuein interfaceProblem<Integer>- Returns:
- an
Optionalcontaining the known optimal value, or empty if unknown
-
evaluate
Description copied from interface:ProblemGiven a solution such thatsolution[i]is the value of the variablex_i, returns the value of this solution and checks if the solution respects the problem's constraints.Note: For maximization problems, the returned value is minus the computed value.
- Specified by:
evaluatein interfaceProblem<Integer>- Parameters:
solution- A solution of the problem.- Returns:
- The value of the input solution
- Throws:
InvalidSolutionException- If the solution does not respect problem's constraints.
-