Class KSProblem
java.lang.Object
org.ddolib.examples.nolayer.knapsack.KSProblem
Knapsack Problem (KS), using the no-layer modeling API.
Models the classic 0/1 knapsack problem: given a capacity and a list of items each with a
weight and a profit, decide which items to take so as to maximize total profit without exceeding
the capacity. States are represented by KSState (current item index and remaining
capacity); since the library minimizes, transition costs are negated profits.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the domain of possible labels/actions from a given state.doubleGiven a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.static KSProblemReads a knapsack instance from a file and returns it with its items sorted by decreasing profit-to-weight ratio.Returns the initial state of the problem.doubleReturns the initial objective value associated with the initial state.booleanChecks whether the given state is a target (sink) state.toString()transition(KSState state, int label) Applies a label/action to a state, computing the next state according to the problem's transition function.doubletransitionCost(KSState state, int label) Computes the change in objective value resulting from applying a label to a given state.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ddolib.nolayer.modeling.Problem
hasNonNegativeCosts, optimalValue
-
Field Details
-
profit
public final int[] profitProfit of each item, indexed by item number. -
weight
public final int[] weightWeight of each item, indexed by item number. -
capa
public final int capaTotal capacity of the knapsack.
-
-
Constructor Details
-
KSProblem
Creates a new named knapsack instance.- Parameters:
profit- profit of each item, indexed by item numberweight- weight of each item, indexed by item numbercapa- total capacity of the knapsackname- name of the instance, used intoString()
-
KSProblem
public KSProblem(int[] profit, int[] weight, int capa) Creates a new unnamed knapsack instance.- Parameters:
profit- profit of each item, indexed by item numberweight- weight of each item, indexed by item numbercapa- total capacity of the knapsack
-
-
Method Details
-
fromFile
Reads a knapsack instance from a file and returns it with its items sorted by decreasing profit-to-weight ratio.- Parameters:
fname- path to the instance file- Returns:
- the knapsack instance described by the file
- Throws:
IOException- if the instance file cannot be read
-
initialState
Description copied from interface:ProblemReturns the initial state of the problem.- Specified by:
initialStatein interfaceProblem<KSState>- 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<KSState>- Returns:
- the starting value of the objective function
-
isTarget
Description copied from interface:ProblemChecks whether the given state is a target (sink) state. -
domain
Description copied from interface:ProblemReturns the domain of possible labels/actions from a given state. -
transition
Description copied from interface:ProblemApplies a label/action to a state, computing the next state according to the problem's transition function.- Specified by:
transitionin interfaceProblem<KSState>- Parameters:
state- the state from which the transition originateslabel- the label to apply- Returns:
- the resulting state after applying the label
-
transitionCost
Description copied from interface:ProblemComputes the change in objective value resulting from applying a label to a given state.- Specified by:
transitionCostin interfaceProblem<KSState>- Parameters:
state- the state from which the transition originateslabel- the label to apply- Returns:
- the incremental objective cost/value associated with this label
-
evaluate
Description copied from interface:ProblemGiven a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.- Specified by:
evaluatein interfaceProblem<KSState>- Parameters:
solution- a solution of the problem (sequence of labels)- Returns:
- the value of the input solution
- Throws:
InvalidSolutionException- if the solution does not respect problem's constraints
-
toString
-