Skip to content

Commit

Permalink
Close #7: toString implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
hdbeukel committed Aug 28, 2015
1 parent b1a9b4f commit 1c9bb41
Show file tree
Hide file tree
Showing 17 changed files with 249 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.jamesframework.core.exceptions.SolutionModificationException;
import org.jamesframework.core.problems.sol.Solution;

Expand Down Expand Up @@ -96,7 +97,7 @@ public List<Integer> getOrder(){
* @return size of this permutation
*/
public int size(){
return order.size();
return getOrder().size();
}

/**
Expand Down Expand Up @@ -129,7 +130,7 @@ public void swap(int i, int j){
@Override
public PermutationSolution copy() {
// skip input checks, we know it's valid
return new PermutationSolution(order, false);
return new PermutationSolution(getOrder(), false);
}

/**
Expand All @@ -148,7 +149,7 @@ public boolean equals(Object other) {
return false;
}
final PermutationSolution otherPerm = (PermutationSolution) other;
return Objects.equals(this.order, otherPerm.order);
return Objects.equals(getOrder(), otherPerm.getOrder());
}

/**
Expand All @@ -159,8 +160,21 @@ public boolean equals(Object other) {
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.order);
hash = 29 * hash + Objects.hashCode(getOrder());
return hash;
}

/**
* Create a string representation of the permutation solution,
* indicating the current order of IDs.
*
* @return string representation
*/
@Override
public String toString(){
return getOrder().stream()
.map(Object::toString)
.collect(Collectors.joining(", ", "Permutation solution: {", "}"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,15 @@ public List<ReverseSubsequenceMove> getAllMoves(PermutationSolution solution) {
}
return moves;
}

/**
* Get string indicating neighbourhood type.
*
* @return string representation
*/
@Override
public String toString(){
return "Reverse subsequence (permutation)";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ public List<SingleSwapMove> getAllMoves(PermutationSolution solution) {
}
return moves;
}

/**
* Get string indicating neighbourhood type.
*
* @return string representation
*/
@Override
public String toString(){
return "Single swap (permutation)";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,15 @@ public int hashCode() {
hash = 41 * hash + this.to;
return hash;
}

/**
* Get string indicating move type and bounds of reversed subsequence.
*
* @return string representation
*/
@Override
public String toString(){
return String.format("Reverse subsequence (%d, %d) in permutation solution", from, to);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class SingleSwapMove implements Move<PermutationSolution>{
private final int i, j;

/**
* Create a single swap move given the two positions in the permutation at which items are to be swapped.
* Create a single swap move given the two positions in the permutation
* at which items are to be swapped.
*
* @param i position of first item to be swapped
* @param j position of second item to be swapped
Expand Down Expand Up @@ -112,4 +113,14 @@ public int hashCode() {
return hash;
}

/**
* Get string indicating move type and positions of swapped items.
*
* @return string representation
*/
@Override
public String toString(){
return String.format("Swap items at positions (%d, %d) in permutation solution", i, j);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.jamesframework.ext.problems.objectives;

import java.util.Locale;
import org.jamesframework.core.exceptions.IncompatibleDeltaEvaluationException;
import org.jamesframework.core.problems.objectives.Objective;
import org.jamesframework.core.problems.objectives.evaluations.Evaluation;
Expand Down Expand Up @@ -152,4 +153,16 @@ public boolean isMinimizing() {
return obj.isMinimizing();
}

/**
* Create string representation of normalized objective, indicating
* the original, unnormalized objective and normalization interval.
*
* @return string representation
*/
@Override
public String toString(){
String interval = "[" + min + ", " + max + "]";
return String.format("\"%s\" normalized from %s to [0, 1]", obj, interval);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.jamesframework.core.problems.objectives.Objective;
import org.jamesframework.core.problems.sol.Solution;
import org.jamesframework.core.problems.objectives.evaluations.Evaluation;
Expand Down Expand Up @@ -157,6 +158,17 @@ public <ActualSolutionType extends SolutionType> WeightedIndexEvaluation evaluat
return newEval;
}


/**
* Create string representation that points out the
* included objectives and corresponding weights.
*
* @return string representation
*/
@Override
public String toString(){
return weights.keySet().stream()
.map(obj -> "("+ obj + ", " + weights.get(obj) + ")")
.collect(Collectors.joining(", ", "Weighted index: ", ""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ public Evaluation getUnnormalizedEvaluation(){
return eval;
}

/**
* Format value as string. Unnormalized value is included between brackets.
*
* @return string version of normalized and original value
*/
@Override
public String toString(){
return String.format("%s (unnormalized: %s)", getValue(), eval.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public double getValue() {
return weightedSum;
}

/**
* Format value as string.
*
* @return string version of value
*/
@Override
public String toString(){
return getValue() + "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,23 @@ public List<Move<? super SolutionType>> getAllMoves(SolutionType solution) {
.flatMap(neigh -> neigh.getAllMoves(solution).stream()) // flatten to one stream of all moves
.collect(Collectors.toList()); // collect in one list
}

/**
* Create string representation that indicates the
* contained neighbourhoods and respective weights.
*
* @return string representation
*/
@Override
public String toString(){
String res = "Composite: ";
for(int i=0; i<neighs.size(); i++){
res += "(" + neighs.get(i) + ", " + weights.get(i) + ")";
if(i < neighs.size()-1){
res += ", ";
}
}
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,15 @@ public void testCopyAndEquals() {
}

}

@Test
public void testToString(){

System.out.println(" - test toString");

PermutationSolution sol = new PermutationSolution(Arrays.asList(1,2,3,4,5));
assertEquals("Permutation solution: {1, 2, 3, 4, 5}", sol.toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,15 @@ public void testEquals(){
assertNotEquals(m1, "abc");

}

@Test
public void testToString(){

System.out.println(" - test toString");

ReverseSubsequenceMove move = new ReverseSubsequenceMove(2, 5);
assertEquals("Reverse subsequence (2, 5) in permutation solution", move.toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,15 @@ public void testEquals(){
assertNotEquals(m1, "Trudy");

}

@Test
public void testToString(){

System.out.println(" - test toString");

SingleSwapMove move = new SingleSwapMove(2, 5);
assertEquals("Swap items at positions (2, 5) in permutation solution", move.toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,22 @@ public void testIsMinimizing() {

}

@Test
public void testToString(){

System.out.println(" - test toString");

double min = 5.0, max = 8.0, val = 7.5;

// note: provided toString implementation becomes obsolete when core v1.2 is released
FixedEvaluationObjectiveStub orig = new FixedEvaluationObjectiveStub(val){
@Override
public String toString(){ return "Fixed evaluation"; }
};
NormalizedObjective<Solution, Object> normalized = new NormalizedObjective<>(orig, min, max);

assertEquals("\"Fixed evaluation\" normalized from [5.0, 8.0] to [0, 1]", normalized.toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.jamesframework.core.subset.SubsetSolution;
import org.jamesframework.core.subset.neigh.moves.SubsetMove;
import org.jamesframework.core.subset.neigh.moves.SwapMove;
import org.jamesframework.test.fakes.ScoredFakeSubsetData;
import org.jamesframework.test.fakes.SumOfIDsFakeSubsetObjective;
import org.jamesframework.test.fakes.SumOfScoresFakeSubsetObjective;
import org.jamesframework.test.stubs.EmptySolutionStub;
import org.jamesframework.test.stubs.FixedEvaluationObjectiveStub;
import org.jamesframework.test.util.TestConstants;
Expand Down Expand Up @@ -288,5 +290,41 @@ public void testDeltaEvaluation() {
assertEquals(eval.getValue(), fullEval.getValue(), TestConstants.DOUBLE_COMPARISON_PRECISION);

}

@Test
public void testToString(){

System.out.println(" - test toString");

// create weighted objective
WeightedIndex<SubsetSolution, ScoredFakeSubsetData> weighted = new WeightedIndex<>();
// create some objectives
// note: provided toString implementations become obsolete when core v1.2 is released
FixedEvaluationObjectiveStub obj1 = new FixedEvaluationObjectiveStub(1.0){
@Override
public String toString(){ return "Fixed evaluation"; }
};
SumOfIDsFakeSubsetObjective obj2 = new SumOfIDsFakeSubsetObjective(){
@Override
public String toString(){ return "Sum of IDs"; }
};;
SumOfScoresFakeSubsetObjective obj3 = new SumOfScoresFakeSubsetObjective(){
@Override
public String toString(){ return "Sum of scores"; }
};;

// add objectives with positive weights
double weight1 = 2.0;
double weight2 = 1.5;
double weight3 = 4.2;
weighted.addObjective(obj1, weight1);
weighted.addObjective(obj2, weight2);
weighted.addObjective(obj3, weight3);

assertEquals("Weighted index: (Fixed evaluation, 2.0), "
+ "(Sum of IDs, 1.5), (Sum of scores, 4.2)",
weighted.toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ public void testGetUnnormalizedEvaluation() {

}

@Test
public void testToString(){

System.out.println(" - test toString");

double min = 5, max = 8, val = 7.5;

Evaluation orig = () -> val;
NormalizedEvaluation normalized = new NormalizedEvaluation(orig, min, max);

assertEquals("0.8333333333333334 (unnormalized: 7.5)", normalized.toString());

}

}

0 comments on commit 1c9bb41

Please sign in to comment.