Class KSProblem

java.lang.Object
org.ddolib.examples.nolayer.knapsack.KSProblem
All Implemented Interfaces:
Problem<KSState>

public class KSProblem extends Object implements Problem<KSState>
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
    Modifier and Type
    Field
    Description
    final int
    Total capacity of the knapsack.
    final int[]
    Profit of each item, indexed by item number.
    final int[]
    Weight of each item, indexed by item number.
  • Constructor Summary

    Constructors
    Constructor
    Description
    KSProblem(int[] profit, int[] weight, int capa)
    Creates a new unnamed knapsack instance.
    KSProblem(int[] profit, int[] weight, int capa, String name)
    Creates a new named knapsack instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    domain(KSState state)
    Returns the domain of possible labels/actions from a given state.
    double
    evaluate(List<Integer> solution)
    Given a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.
    static KSProblem
    Reads 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.
    double
    Returns the initial objective value associated with the initial state.
    boolean
    Checks whether the given state is a target (sink) state.
     
    transition(KSState state, int label)
    Applies a label/action to a state, computing the next state according to the problem's transition function.
    double
    transitionCost(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, wait

    Methods inherited from interface org.ddolib.nolayer.modeling.Problem

    hasNonNegativeCosts, optimalValue
  • Field Details

    • profit

      public final int[] profit
      Profit of each item, indexed by item number.
    • weight

      public final int[] weight
      Weight of each item, indexed by item number.
    • capa

      public final int capa
      Total capacity of the knapsack.
  • Constructor Details

    • KSProblem

      public KSProblem(int[] profit, int[] weight, int capa, String name)
      Creates a new named knapsack instance.
      Parameters:
      profit - profit of each item, indexed by item number
      weight - weight of each item, indexed by item number
      capa - total capacity of the knapsack
      name - name of the instance, used in toString()
    • KSProblem

      public KSProblem(int[] profit, int[] weight, int capa)
      Creates a new unnamed knapsack instance.
      Parameters:
      profit - profit of each item, indexed by item number
      weight - weight of each item, indexed by item number
      capa - total capacity of the knapsack
  • Method Details

    • fromFile

      public static KSProblem fromFile(String fname) throws IOException
      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

      public KSState initialState()
      Description copied from interface: Problem
      Returns the initial state of the problem.
      Specified by:
      initialState in interface Problem<KSState>
      Returns:
      the state representing the starting point of the optimization
    • initialValue

      public double initialValue()
      Description copied from interface: Problem
      Returns the initial objective value associated with the initial state.
      Specified by:
      initialValue in interface Problem<KSState>
      Returns:
      the starting value of the objective function
    • isTarget

      public boolean isTarget(KSState state)
      Description copied from interface: Problem
      Checks whether the given state is a target (sink) state.
      Specified by:
      isTarget in interface Problem<KSState>
      Parameters:
      state - the current state
      Returns:
      true if the state is a target state, false otherwise
    • domain

      public Iterator<Integer> domain(KSState state)
      Description copied from interface: Problem
      Returns the domain of possible labels/actions from a given state.
      Specified by:
      domain in interface Problem<KSState>
      Parameters:
      state - the current state
      Returns:
      an iterator over all feasible labels for the given state
    • transition

      public KSState transition(KSState state, int label)
      Description copied from interface: Problem
      Applies a label/action to a state, computing the next state according to the problem's transition function.
      Specified by:
      transition in interface Problem<KSState>
      Parameters:
      state - the state from which the transition originates
      label - the label to apply
      Returns:
      the resulting state after applying the label
    • transitionCost

      public double transitionCost(KSState state, int label)
      Description copied from interface: Problem
      Computes the change in objective value resulting from applying a label to a given state.
      Specified by:
      transitionCost in interface Problem<KSState>
      Parameters:
      state - the state from which the transition originates
      label - the label to apply
      Returns:
      the incremental objective cost/value associated with this label
    • evaluate

      public double evaluate(List<Integer> solution) throws InvalidSolutionException
      Description copied from interface: Problem
      Given a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.
      Specified by:
      evaluate in interface Problem<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

      public String toString()
      Overrides:
      toString in class Object