Commit fc62c362 authored by Sebastian Vollbrecht's avatar Sebastian Vollbrecht

Cleaned up code.

This includes:
- removal of optionals as fields
- removal of unnecessary 'public abstract' modifiers in interfaces
- more lambdas and method reference usage
- simplified dataflow
- removal of faulty integer divisions
- renaming of expected exceptions in JUnit tests
- typo fixes
parent e876a5c2
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package graphgen.datastructures; package graphgen.datastructures;
import java.util.Optional;
import java.util.Random; import java.util.Random;
/** /**
...@@ -28,7 +27,7 @@ import java.util.Random; ...@@ -28,7 +27,7 @@ import java.util.Random;
*/ */
public class SeededRandom { public class SeededRandom {
private Optional<Long> seed; private Long seed;
private final Random rng; private final Random rng;
...@@ -38,7 +37,7 @@ public class SeededRandom { ...@@ -38,7 +37,7 @@ public class SeededRandom {
*/ */
public SeededRandom() { public SeededRandom() {
this.rng = new Random(); this.rng = new Random();
this.seed = Optional.empty(); this.seed = null;
} }
/** /**
...@@ -58,7 +57,7 @@ public class SeededRandom { ...@@ -58,7 +57,7 @@ public class SeededRandom {
* @param seed the seed to pass to the wrapped random instance * @param seed the seed to pass to the wrapped random instance
*/ */
public void setSeed(long seed) { public void setSeed(long seed) {
this.seed = Optional.of(seed); this.seed = seed;
rng.setSeed(seed); rng.setSeed(seed);
} }
...@@ -97,7 +96,7 @@ public class SeededRandom { ...@@ -97,7 +96,7 @@ public class SeededRandom {
} }
private void checkSeededStatus() { private void checkSeededStatus() {
if (!seed.isPresent()) { if (seed == null) {
throw new UnsupportedOperationException("Must set the seed first."); throw new UnsupportedOperationException("Must set the seed first.");
} }
} }
......
...@@ -34,11 +34,11 @@ public interface Initializable { ...@@ -34,11 +34,11 @@ public interface Initializable {
* *
* @param rng the graph generator's random number generator * @param rng the graph generator's random number generator
*/ */
public abstract void init(SeededRandom rng); void init(SeededRandom rng);
/** /**
* Resets the implementing class, preparing it for the generation of a new graph. This method is called every time * Resets the implementing class, preparing it for the generation of a new graph. This method is called every time
* before the generator begins generating a new graph. * before the generator begins generating a new graph.
*/ */
public abstract void reset(); void reset();
} }
...@@ -37,7 +37,7 @@ public interface EdgeCreationComponent extends Initializable { ...@@ -37,7 +37,7 @@ public interface EdgeCreationComponent extends Initializable {
* *
* @param layers the layer structure to use * @param layers the layer structure to use
*/ */
public abstract void setLayerStructure(LayerStructure layers); void setLayerStructure(LayerStructure layers);
/** /**
* Sets the ASAP times map of the implementing class, which can be used optionally for different purposes, such as * Sets the ASAP times map of the implementing class, which can be used optionally for different purposes, such as
...@@ -45,6 +45,6 @@ public interface EdgeCreationComponent extends Initializable { ...@@ -45,6 +45,6 @@ public interface EdgeCreationComponent extends Initializable {
* *
* @param asapTimes the ASAP times map to use * @param asapTimes the ASAP times map to use
*/ */
public abstract void setASAPTimesMap(Map<ResourceNode, Integer> asapTimes); void setASAPTimesMap(Map<ResourceNode, Integer> asapTimes);
} }
...@@ -27,7 +27,6 @@ import modsched.Edge; ...@@ -27,7 +27,6 @@ import modsched.Edge;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
...@@ -38,7 +37,7 @@ import java.util.Set; ...@@ -38,7 +37,7 @@ import java.util.Set;
public class IdenticalValueComputer extends ValueComputer { public class IdenticalValueComputer extends ValueComputer {
private final Map<ResourceNode, Map<ResourceNode, Integer>> edgeValues; private final Map<ResourceNode, Map<ResourceNode, Integer>> edgeValues;
private final Optional<Distribution<Integer>> defaultDistribution; private final Distribution<Integer> defaultDistribution;
/** /**
* Constructs a new identical value computer for the specified {@link EdgeValue}, using the provided set of edges. * Constructs a new identical value computer for the specified {@link EdgeValue}, using the provided set of edges.
...@@ -81,7 +80,7 @@ public class IdenticalValueComputer extends ValueComputer { ...@@ -81,7 +80,7 @@ public class IdenticalValueComputer extends ValueComputer {
} }
this.defaultDistribution = Optional.ofNullable(defaultDistribution); this.defaultDistribution = defaultDistribution;
} }
...@@ -92,8 +91,8 @@ public class IdenticalValueComputer extends ValueComputer { ...@@ -92,8 +91,8 @@ public class IdenticalValueComputer extends ValueComputer {
return edgeValues.get(src).get(dst); return edgeValues.get(src).get(dst);
} }
if (defaultDistribution.isPresent()) { if (defaultDistribution != null) {
return defaultDistribution.get().pick(); return defaultDistribution.pick();
} else { } else {
throw new IllegalArgumentException("No edge value for nodes " + src + " and " + dst + " was given."); throw new IllegalArgumentException("No edge value for nodes " + src + " and " + dst + " was given.");
} }
...@@ -111,12 +110,16 @@ public class IdenticalValueComputer extends ValueComputer { ...@@ -111,12 +110,16 @@ public class IdenticalValueComputer extends ValueComputer {
@Override @Override
public void init(SeededRandom rng) { public void init(SeededRandom rng) {
defaultDistribution.ifPresent(dist -> dist.init(rng)); if (defaultDistribution != null) {
defaultDistribution.init(rng);
}
} }
@Override @Override
protected void implementedReset() { protected void implementedReset() {
defaultDistribution.ifPresent(Distribution::reset); if (defaultDistribution != null) {
defaultDistribution.reset();
}
} }
} }
...@@ -34,6 +34,6 @@ public interface FoldingStrategy<T> extends Initializable { ...@@ -34,6 +34,6 @@ public interface FoldingStrategy<T> extends Initializable {
* @param values the list of values * @param values the list of values
* @return the folded value * @return the folded value
*/ */
public abstract T fold(List<T> values); T fold(List<T> values);
} }
...@@ -22,6 +22,7 @@ import graphgen.datastructures.SeededRandom; ...@@ -22,6 +22,7 @@ import graphgen.datastructures.SeededRandom;
import graphgen.graph.Resource; import graphgen.graph.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
...@@ -66,7 +67,7 @@ public class DepthNodeCreator extends NodeCreator { ...@@ -66,7 +67,7 @@ public class DepthNodeCreator extends NodeCreator {
this.depthsAndNodeCreators = new ArrayList<>(); this.depthsAndNodeCreators = new ArrayList<>();
nodeCreatorsByDepth.forEach((d, c) -> depthsAndNodeCreators.add(Pair.makePair(d, c))); nodeCreatorsByDepth.forEach((d, c) -> depthsAndNodeCreators.add(Pair.makePair(d, c)));
depthsAndNodeCreators.sort((p1, p2) -> p1.first.compareTo(p2.first)); depthsAndNodeCreators.sort(Comparator.comparing(p -> p.first));
} }
......
...@@ -39,7 +39,6 @@ import java.util.Iterator; ...@@ -39,7 +39,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
...@@ -71,7 +70,7 @@ public class FeasibleMinIIProperty extends Property { ...@@ -71,7 +70,7 @@ public class FeasibleMinIIProperty extends Property {
private final Set<ResourceNode> enclosedNodes; private final Set<ResourceNode> enclosedNodes;
private final ModuloSchedulingFormulation formulation; private final ModuloSchedulingFormulation formulation;
private Optional<SCC> recMinIISCC; private SCC recMinIISCC;
/** /**
* Creates a new feasible MinII property which ensures the specified MinII during graph creation. The modulo * Creates a new feasible MinII property which ensures the specified MinII during graph creation. The modulo
...@@ -106,11 +105,13 @@ public class FeasibleMinIIProperty extends Property { ...@@ -106,11 +105,13 @@ public class FeasibleMinIIProperty extends Property {
this.minII = minII; this.minII = minII;
this.backedgeDelay = backedgeDelay; this.backedgeDelay = backedgeDelay;
this.backedgeDistance = backedgeDistance; this.backedgeDistance = backedgeDistance;
// TODO: allow inner delays >= minInnerDelay and <= maxInnerDelay
this.maxInnerDelay = SchedulingUtils.getMaxInnerDelay(minII, backedgeDelay, backedgeDistance); this.maxInnerDelay = SchedulingUtils.getMaxInnerDelay(minII, backedgeDelay, backedgeDistance);
this.enclosedNodes = new HashSet<>(); this.enclosedNodes = new HashSet<>();
this.formulation = formulation; this.formulation = formulation;
this.recMinIISCC = Optional.empty(); this.recMinIISCC = null;
} }
...@@ -123,7 +124,7 @@ public class FeasibleMinIIProperty extends Property { ...@@ -123,7 +124,7 @@ public class FeasibleMinIIProperty extends Property {
* 1) There is no RecMinII-ensuring SCC, i.e. the MinII is ensured through means * 1) There is no RecMinII-ensuring SCC, i.e. the MinII is ensured through means
* of the nodes' ResMinII. * of the nodes' ResMinII.
*/ */
if (!recMinIISCC.isPresent()) if (recMinIISCC == null)
return true; return true;
/* /*
...@@ -132,7 +133,7 @@ public class FeasibleMinIIProperty extends Property { ...@@ -132,7 +133,7 @@ public class FeasibleMinIIProperty extends Property {
* SCC can thus simply be 'pushed away' (ASAP-wise) from the edge-to-be's source * SCC can thus simply be 'pushed away' (ASAP-wise) from the edge-to-be's source
* node during scheduling, without affecting its RecMinII. * node during scheduling, without affecting its RecMinII.
*/ */
if (!recMinIISCC.get().getDelaysFromPreviousDstNode().containsKey(src)) { if (!recMinIISCC.getDelaysFromPreviousDstNode().containsKey(src)) {
return true; return true;
} }
...@@ -142,7 +143,7 @@ public class FeasibleMinIIProperty extends Property { ...@@ -142,7 +143,7 @@ public class FeasibleMinIIProperty extends Property {
* increased with the new edge in any way. The edge-to-be's destination node can * increased with the new edge in any way. The edge-to-be's destination node can
* thus be freely scheduled. * thus be freely scheduled.
*/ */
if (!recMinIISCC.get().getDelaysToNextSrcNode().containsKey(dst)) { if (!recMinIISCC.getDelaysToNextSrcNode().containsKey(dst)) {
return true; return true;
} }
...@@ -161,10 +162,10 @@ public class FeasibleMinIIProperty extends Property { ...@@ -161,10 +162,10 @@ public class FeasibleMinIIProperty extends Property {
* 3) The delay from the edge-to-be's destination node to the backedge's source * 3) The delay from the edge-to-be's destination node to the backedge's source
* node. * node.
*/ */
Edge<ResourceNode> recMinIIBackedge = recMinIISCC.get().getBackedges().get(0); Edge<ResourceNode> recMinIIBackedge = recMinIISCC.getBackedges().get(0);
int delayFromBackedgeDst = recMinIISCC.get().getDelaysFromPreviousDstNode().get(src); int delayFromBackedgeDst = recMinIISCC.getDelaysFromPreviousDstNode().get(src);
int delayInBetween = src.getDelay() + delay; int delayInBetween = src.getDelay() + delay;
int delayToBackedgeSrc = recMinIISCC.get().getDelaysToNextSrcNode().get(dst); int delayToBackedgeSrc = recMinIISCC.getDelaysToNextSrcNode().get(dst);
int resultingPathDelay = delayFromBackedgeDst + delayInBetween + delayToBackedgeSrc; int resultingPathDelay = delayFromBackedgeDst + delayInBetween + delayToBackedgeSrc;
...@@ -284,22 +285,22 @@ public class FeasibleMinIIProperty extends Property { ...@@ -284,22 +285,22 @@ public class FeasibleMinIIProperty extends Property {
Edge<ResourceNode> theBackedge = createMaxInnerDelayPath(initialPairs, nodesByDelay); Edge<ResourceNode> theBackedge = createMaxInnerDelayPath(initialPairs, nodesByDelay);
recMinIISCC = Optional.of(new SCC(Arrays.asList(theBackedge), layers, edgeCreator)); recMinIISCC = new SCC(Arrays.asList(theBackedge), layers, edgeCreator);
enclosedNodes.addAll(recMinIISCC.get().update(minII)); enclosedNodes.addAll(recMinIISCC.update(minII));
} }
@Override @Override
public void reset() { public void reset() {
recMinIISCC = Optional.empty(); recMinIISCC = null;
enclosedNodes.clear(); enclosedNodes.clear();
} }
@Override @Override
public void notify(Edge<ResourceNode> edge) { public void notify(Edge<ResourceNode> edge) {
if (!edge.isBackedge() && recMinIISCC.isPresent()) { if (!edge.isBackedge() && recMinIISCC != null) {
enclosedNodes.addAll(recMinIISCC.get().update(minII)); enclosedNodes.addAll(recMinIISCC.update(minII));
} else if (edge.isBackedge()) { } else if (edge.isBackedge()) {
enclosedNodes.addAll(findAllEnclosedNodes(edgeCreator.edgeView(), edgeCreator.incomingEdgesView())); enclosedNodes.addAll(findAllEnclosedNodes(edgeCreator.edgeView(), edgeCreator.incomingEdgesView()));
} }
......
...@@ -26,7 +26,7 @@ import java.util.Objects; ...@@ -26,7 +26,7 @@ import java.util.Objects;
/** /**
* The start configuration represents the very start of an infeasible cycle. It contains the <i>destination node</i> and * The start configuration represents the very start of an infeasible cycle. It contains the <i>destination node</i> and
* the layer it is located in. The destination node is the single node which all backedges of the infasible cycle must * the layer it is located in. The destination node is the single node which all backedges of the infeasible cycle must
* later connect to (hence the name <i>destination</i> node). * later connect to (hence the name <i>destination</i> node).
* *
* @author Sebastian Vollbrecht * @author Sebastian Vollbrecht
......
...@@ -333,10 +333,11 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob ...@@ -333,10 +333,11 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob
Set<Integer> preds = incomingEdges.get(secondID); Set<Integer> preds = incomingEdges.get(secondID);
int maxPredDepth = preds.stream().mapToInt(depthsByID::get).max().getAsInt(); int maxPredDepth = preds.stream().mapToInt(depthsByID::get).max().orElse(0);
if (maxPredDepth >= firstDepth) if (maxPredDepth >= firstDepth) {
return false; return false;
}
} }
...@@ -344,10 +345,9 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob ...@@ -344,10 +345,9 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob
Set<Integer> succs = outgoingEdges.get(firstID); Set<Integer> succs = outgoingEdges.get(firstID);
int minSuccDepth = succs.stream().mapToInt(depthsByID::get).min().getAsInt(); int minSuccDepth = succs.stream().mapToInt(depthsByID::get).min().orElse(0);
if (minSuccDepth <= secondDepth) return minSuccDepth > secondDepth;
return false;
} }
......
...@@ -31,6 +31,6 @@ public interface Observer<T> { ...@@ -31,6 +31,6 @@ public interface Observer<T> {
* *
* @param t the change of state * @param t the change of state
*/ */
public abstract void notify(T t); void notify(T t);
} }
...@@ -54,7 +54,7 @@ public class DistributionUtils { ...@@ -54,7 +54,7 @@ public class DistributionUtils {
* @return the probabilities * @return the probabilities
*/ */
public static double[] normalDistribution(double mean, double standardDeviation, int times) { public static double[] normalDistribution(double mean, double standardDeviation, int times) {
return normalDistribution(mean, standardDeviation, times, -times / 2); return normalDistribution(mean, standardDeviation, times, -times / (double) 2);
} }
/** /**
......
...@@ -38,56 +38,6 @@ public class MathUtils { ...@@ -38,56 +38,6 @@ public class MathUtils {
return c.stream().mapToInt(Integer::intValue).sum(); return c.stream().mapToInt(Integer::intValue).sum();
} }
/**
* Calculates the sum of the given collection of doubles.
*
* @param c the collection to sum
* @return the sum of its elements
*/
public static double sumDouble(Collection<Double> c) {
return c.stream().mapToDouble(Double::doubleValue).sum();
}
/**
* Calculates the sum of the given collection of longs.
*
* @param c the collection to sum
* @return the sum of its elements
*/
public static double sumLong(Collection<Long> c) {
return c.stream().mapToLong(Long::longValue).sum();
}
/**
* Calculates the arithmetic mean of the given collection of integers.
*
* @param c the collection to average
* @return the arithmetic mean of its elements
*/
public static double meanInteger(Collection<Integer> c) {
return c.stream().mapToInt(Integer::intValue).average().getAsDouble();
}
/**
* Calculates the arithmetic mean of the given collection of doubles.
*
* @param c the collection to average
* @return the arithmetic mean of its elements
*/
public static double meanDouble(Collection<Double> c) {
return c.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
}
/**
* Calculates the arithmetic mean of the given collection of longs.
*
* @param c the collection to average
* @return the arithmetic mean of its elements
*/
public static double meanLong(Collection<Long> c) {
return c.stream().mapToLong(Long::longValue).average().getAsDouble();
}
/** /**
* Randomly picks a number from the inclusive [min, max] range. * Randomly picks a number from the inclusive [min, max] range.
* *
......
...@@ -34,7 +34,6 @@ import modsched.MoovacFormulation; ...@@ -34,7 +34,6 @@ import modsched.MoovacFormulation;
import modsched.Node; import modsched.Node;
import modsched.Schedule; import modsched.Schedule;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -158,7 +157,7 @@ public class SchedulingUtils { ...@@ -158,7 +157,7 @@ public class SchedulingUtils {
System.setOut(new PrintStream(new OutputStream() { System.setOut(new PrintStream(new OutputStream() {
@Override @Override
public void write(int b) throws IOException { public void write(int b) {
// Do nothing. // Do nothing.
} }
})); }));
...@@ -173,13 +172,8 @@ public class SchedulingUtils { ...@@ -173,13 +172,8 @@ public class SchedulingUtils {
if (maxTotalTime == null) { if (maxTotalTime == null) {
s = f.schedule(minII, maxII, r -> r.limit, Resource::isUnlimited, TIMELIMIT_CANDIDATE); s = f.schedule(minII, maxII, r -> r.limit, Resource::isUnlimited, TIMELIMIT_CANDIDATE);
} else { } else {
Callable<Schedule> callable = new Callable<Schedule>() { Callable<Schedule> callable = () -> f
.schedule(minII, maxII, r -> r.limit, Resource::isUnlimited, TIMELIMIT_CANDIDATE);
@Override
public Schedule call() throws Exception {
return f.schedule(minII, maxII, r -> r.limit, Resource::isUnlimited, TIMELIMIT_CANDIDATE);
}
};
RunnableFuture<Schedule> future = new FutureTask<>(callable); RunnableFuture<Schedule> future = new FutureTask<>(callable);
ExecutorService service = Executors.newSingleThreadExecutor(); ExecutorService service = Executors.newSingleThreadExecutor();
...@@ -364,7 +358,7 @@ public class SchedulingUtils { ...@@ -364,7 +358,7 @@ public class SchedulingUtils {
* bounds should be returned. * bounds should be returned.
*/ */
int maxII = layers.getLastLayer().stream().map(graph::getNode) int maxII = layers.getLastLayer().stream().map(graph::getNode)
.mapToInt(n -> earliestPossibleTimes.get(n) + n.getDelay()).max().getAsInt(); .mapToInt(n -> earliestPossibleTimes.get(n) + n.getDelay()).max().orElse(0);
return Math.max(maxII, getMinII(graph)); return Math.max(maxII, getMinII(graph));
......
...@@ -103,11 +103,10 @@ public class Edge<N extends Node> implements Comparable<Edge<N>>, Serializable { ...@@ -103,11 +103,10 @@ public class Edge<N extends Node> implements Comparable<Edge<N>>, Serializable {
} else if (!dst.equals(other.dst)) } else if (!dst.equals(other.dst))
return false; return false;
if (src == null) { if (src == null) {
if (other.src != null) return other.src == null;
return false; } else {
} else if (!src.equals(other.src)) return src.equals(other.src);
return false; }
return true;
} }
} }
...@@ -92,9 +92,7 @@ public class Node implements Comparable<Node>, Serializable { ...@@ -92,9 +92,7 @@ public class Node implements Comparable<Node>, Serializable {
Node other = (Node) obj; Node other = (Node) obj;
if (delay != other.delay)