/** * Check if the end state is valid. This means check that the difference between the sums of both * states are not greater than total number of items in fridge * maximum consumption of one item. * * <p>Will also check if the difference between the contents of both states for each item is not * greater than the maximum consumption * * @param currentState The current state * @param endState The end state * @return true if the end state is valid. false otherwise */ private boolean IsValidEndState(State currentState, State endState) { /* The sums */ int currentSum = sumOf(currentState.getState()); int endSum = sumOf(endState.getState()); if (Math.abs(currentSum - endSum) > (this.fridge.getMaxTypes() * this.fridge.getMaxItemsPerType())) { return false; } for (int i = 0; i < currentState.getState().size(); ++i) { if (Math.abs(currentSum - endSum) > this.fridge.getMaxItemsPerType()) { return false; } } return true; }
/** * Checks if the difference between the values of the previous policy and the new policy are <= * val. * * <p>Also reassigns the old policy * * @param double val - The difference * @param Map<State, Action> newPolicy - The new policy * @return true if the minimum difference > val. false otherwise */ private boolean CheckDifference(double val, Map<State, Action> newPolicy) { Double currDiff, minDiff = null; // The minimum and current difference if (this.policy.size() != this.possibleStates.size()) { // Previous policy wasn't assigned yet return true; } for (State current : newPolicy.keySet()) { for (State old : this.policy.keySet()) { if (current.equals(old)) { currDiff = Math.abs(current.getTemporaryCost() - old.getCost()); if (minDiff == null || currDiff <= minDiff) { minDiff = currDiff; } break; } } } if (minDiff > val) { return true; } return false; }