Class KnapSack
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<OptProbElement>,Collection<OptProbElement>,Set<OptProbElement>
This class implements a KnapSack problem
- Author:
- Sebastian Franke
- See Also:
-
Field Summary
Fields inherited from class org.tweetyproject.math.opt.problem.CombinatoricsProblem
elements, MAXIMIZE, MINIMIZE -
Constructor Summary
ConstructorsConstructorDescriptionKnapSack(ArrayList<ElementOfCombinatoricsProb> elements, Term maxWeight) constructor -
Method Summary
Modifier and TypeMethodDescriptioncreate a solution that changes the solution currSol a little bit (i.e.: for TSP: swap 2 cities; for KnapSack: add a random element) for currSol == null: create a random solutiondoubleevaluates the solutiongetHeuristicValue(ElementOfCombinatoricsProb solutionComponent, Integer getCurrentIndex, ElementOfCombinatoricsProb initialReference, ElementOfCombinatoricsProb[] sol) since this class is not used with ant optimization, we do not need this methoddouble[][]since this class is not used with ant optimization, we do not need this methodbooleanchecks if a given solution is valid under all constraintsdoublecalculates sum of valuesdoubleReturn if the solution sol is valid under the constraints of the problemMethods inherited from class org.tweetyproject.math.opt.problem.CombinatoricsProblem
createDifference, formNeighborhood, getGraphrepresentationMethods inherited from class java.util.HashSet
add, clear, clone, contains, isEmpty, iterator, newHashSet, remove, size, spliterator, toArray, toArrayMethods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toStringMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Constructor Details
-
KnapSack
constructor- Parameters:
elements- elements in sackmaxWeight- max weight of sack
-
-
Method Details
-
sumOfWeights
Description copied from class:CombinatoricsProblemReturn if the solution sol is valid under the constraints of the problem- Specified by:
sumOfWeightsin classCombinatoricsProblem- Parameters:
sol- is the solution to be viewd- Returns:
- if the solution sol is valid under the constraints of the problem
-
sumOfValues
calculates sum of values- Parameters:
sol- values- Returns:
- sum
-
createRandomNewSolution
public ArrayList<ElementOfCombinatoricsProb> createRandomNewSolution(ArrayList<ElementOfCombinatoricsProb> currSol) Description copied from class:CombinatoricsProblemcreate a solution that changes the solution currSol a little bit (i.e.: for TSP: swap 2 cities; for KnapSack: add a random element) for currSol == null: create a random solution- Specified by:
createRandomNewSolutionin classCombinatoricsProblem- Parameters:
currSol- the current solution- Returns:
- the solution
-
isValid
Description copied from class:CombinatoricsProblemchecks if a given solution is valid under all constraints- Specified by:
isValidin classCombinatoricsProblem- Parameters:
sol- some solution- Returns:
- true iff the solution is valid
-
evaluate
Description copied from class:CombinatoricsProblemevaluates the solution- Specified by:
evaluatein classCombinatoricsProblem- Parameters:
sol- some solution- Returns:
- the target function for sol
-
getHeuristicValue
public Double getHeuristicValue(ElementOfCombinatoricsProb solutionComponent, Integer getCurrentIndex, ElementOfCombinatoricsProb initialReference, ElementOfCombinatoricsProb[] sol) since this class is not used with ant optimization, we do not need this method- Specified by:
getHeuristicValuein classCombinatoricsProblem- Parameters:
solutionComponent- : Element to be checked *getCurrentIndex- : the length of the solutioninitialReference- : default starting point for the solution (the first Element in the solution)sol- : array representation of a solution (needed for Ant optimization)- Returns:
- the heuristic value
-
getRepresentation
public double[][] getRepresentation()since this class is not used with ant optimization, we do not need this method- Specified by:
getRepresentationin classCombinatoricsProblem- Returns:
- representation
-