From 4017577269f8d060ce4da0330ccb0868d836784e Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 2 Mar 2018 15:30:55 +0100 Subject: [PATCH] Instance -> Data. --- src/main/org/insa/algo/AbstractAlgorithm.java | 52 ++-- src/main/org/insa/algo/AbstractData.java | 20 -- src/main/org/insa/algo/AbstractInputData.java | 22 ++ src/main/org/insa/algo/AbstractSolution.java | 135 +++++----- .../StronglyConnectedComponentsAlgorithm.java | 39 ++- .../StronglyConnectedComponentsData.java | 4 +- .../StronglyConnectedComponentsSolution.java | 10 +- .../strongconnectivity/TarjanAlgorithm.java | 254 +++++++++--------- .../WeaklyConnectedComponentsAlgorithm.java | 13 +- .../WeaklyConnectedComponentsData.java | 4 +- .../WeaklyConnectedComponentsSolution.java | 8 +- src/main/org/insa/graphics/MainWindow.java | 3 +- 12 files changed, 288 insertions(+), 276 deletions(-) delete mode 100644 src/main/org/insa/algo/AbstractData.java create mode 100644 src/main/org/insa/algo/AbstractInputData.java diff --git a/src/main/org/insa/algo/AbstractAlgorithm.java b/src/main/org/insa/algo/AbstractAlgorithm.java index cdc748a..8836c91 100644 --- a/src/main/org/insa/algo/AbstractAlgorithm.java +++ b/src/main/org/insa/algo/AbstractAlgorithm.java @@ -1,4 +1,4 @@ -package org.insa.algo ; +package org.insa.algo; import java.time.Duration; import java.time.Instant; @@ -6,52 +6,62 @@ import java.util.ArrayList; public abstract class AbstractAlgorithm { - protected AbstractData instance; + protected AbstractInputData data; protected ArrayList observers; - - protected AbstractAlgorithm(AbstractData instance) { - this.instance = instance; - this.observers = new ArrayList(); + + /** + * @param data + */ + protected AbstractAlgorithm(AbstractInputData data) { + this.data = data; + this.observers = new ArrayList(); } - protected AbstractAlgorithm(AbstractData instance, ArrayList observers) { - this.instance = instance; - this.observers = observers;; + /** + * @param data + * @param observers + */ + protected AbstractAlgorithm(AbstractInputData data, ArrayList observers) { + this.data = data; + this.observers = observers; + ; } - + /** * Add an observer to this algorithm. * * @param observer */ public void addObserver(Observer observer) { - observers.add(observer); + observers.add(observer); } - + /** * @return The list of observers for this algorithm. */ public ArrayList getObservers() { - return observers; + return observers; } - + /** * @return Instance corresponding to this algorithm. */ - public AbstractData getInputData() { return instance; } - + public AbstractInputData getInputData() { + return data; + } + /** * Run the algorithm and update the current solution. * * @return true if a feasible solution was found (even non-optimal). */ public AbstractSolution run() { - Instant start = Instant.now(); - AbstractSolution solution = this.doRun(); - solution.setSolvingTime(Duration.between(start, Instant.now())); - return solution; + Instant start = Instant.now(); + AbstractSolution solution = this.doRun(); + solution.setSolvingTime(Duration.between(start, Instant.now())); + return solution; } - + /** * Abstract method that should be implemented by child class. * diff --git a/src/main/org/insa/algo/AbstractData.java b/src/main/org/insa/algo/AbstractData.java deleted file mode 100644 index 0409aed..0000000 --- a/src/main/org/insa/algo/AbstractData.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.insa.algo; - -import org.insa.graph.Graph; - -public abstract class AbstractData { - - protected Graph graph; - - /** - * Create a new abstract instance with the given graph. - * - * @param graph - */ - protected AbstractData(Graph graph) { - this.graph = graph; - } - - public Graph getGraph() { return graph; } - -} diff --git a/src/main/org/insa/algo/AbstractInputData.java b/src/main/org/insa/algo/AbstractInputData.java new file mode 100644 index 0000000..9fba4bd --- /dev/null +++ b/src/main/org/insa/algo/AbstractInputData.java @@ -0,0 +1,22 @@ +package org.insa.algo; + +import org.insa.graph.Graph; + +public abstract class AbstractInputData { + + protected Graph graph; + + /** + * Create a new AbstractInputData instance with the given graph. + * + * @param graph + */ + protected AbstractInputData(Graph graph) { + this.graph = graph; + } + + public Graph getGraph() { + return graph; + } + +} diff --git a/src/main/org/insa/algo/AbstractSolution.java b/src/main/org/insa/algo/AbstractSolution.java index 2069c51..af9b89c 100644 --- a/src/main/org/insa/algo/AbstractSolution.java +++ b/src/main/org/insa/algo/AbstractSolution.java @@ -4,71 +4,74 @@ import java.time.Duration; public abstract class AbstractSolution { - /** - * Possible status for a solution. - * - */ - public enum Status { - UNKNOWN, - INFEASIBLE, - FEASIBLE, - OPTIMAL, - }; - - // Status of the solution. - Status status; - - // Solving time for the solution - Duration solvingTime; - - // Original instance of the solution - AbstractData instance; + /** + * Possible status for a solution. + * + */ + public enum Status { + UNKNOWN, INFEASIBLE, FEASIBLE, OPTIMAL, + }; + + // Status of the solution. + Status status; + + // Solving time for the solution. + Duration solvingTime; + + // Original input of the solution. + AbstractInputData data; + + /** + * Create a new abstract solution with unknown status. + * + * @param data + */ + protected AbstractSolution(AbstractInputData data) { + this.data = data; + this.solvingTime = Duration.ZERO; + this.status = Status.UNKNOWN; + } + + protected AbstractSolution(AbstractInputData data, Status status) { + this.data = data; + this.status = status; + } + + /** + * @return Original input for this solution. + */ + public AbstractInputData getInputData() { + return data; + } + + /** + * @return Status of this solution. + */ + public Status getStatus() { + return status; + } + + /** + * @return Solving time of this solution. + */ + public Duration getSolvingTime() { + return solvingTime; + } + + /** + * Set the solving time of this solution. + * + * @param solvingTime Solving time for the solution. + */ + protected void setSolvingTime(Duration solvingTime) { + this.solvingTime = solvingTime; + } + + /** + * @return true if the solution is feasible or optimal. + */ + public boolean isFeasible() { + return status == Status.FEASIBLE || status == Status.OPTIMAL; + } - /** - * Create a new abstract solution with unknown status. - * - * @param instance - */ - protected AbstractSolution(AbstractData instance) { - this.instance = instance; - this.solvingTime = Duration.ZERO; - this.status = Status.UNKNOWN; - } - - protected AbstractSolution(AbstractData instance, Status status) { - this.instance = instance; - this.status = status; - } - - /** - * @return Original instance for this solution. - */ - public AbstractData getInputData() { return instance; } - - /** - * @return Status of this solution. - */ - public Status getStatus() { return status; } - - /** - * @return Solving time of this solution. - */ - public Duration getSolvingTime() { return solvingTime; } - - /** - * Set the solving time of this solution. - * - * @param solvingTime Solving time for the solution. - */ - protected void setSolvingTime(Duration solvingTime) { - this.solvingTime = solvingTime; - } - - /** - * @return true if the solution is feasible or optimal. - */ - public boolean isFeasible() { - return status == Status.FEASIBLE || status == Status.OPTIMAL; - } - } diff --git a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsAlgorithm.java b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsAlgorithm.java index f0cea71..2ddfcd1 100644 --- a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsAlgorithm.java +++ b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsAlgorithm.java @@ -1,26 +1,25 @@ -package org.insa.algo.strongconnectivity ; +package org.insa.algo.strongconnectivity; import org.insa.algo.AbstractAlgorithm; -public abstract class StronglyConnectedComponentsAlgorithm extends AbstractAlgorithm { +public abstract class StronglyConnectedComponentsAlgorithm + extends AbstractAlgorithm { - /** - * - * @param instance - * @param logOutput - */ - public StronglyConnectedComponentsAlgorithm(StronglyConnectedComponentsData instance) { - super(instance); - } - - @Override - public StronglyConnectedComponentsSolution run() { - return (StronglyConnectedComponentsSolution)super.run(); - } - - @Override - public StronglyConnectedComponentsData getInputData() { - return (StronglyConnectedComponentsData)super.getInputData(); - } + /** + * @param data + */ + public StronglyConnectedComponentsAlgorithm(StronglyConnectedComponentsData data) { + super(data); + } + + @Override + public StronglyConnectedComponentsSolution run() { + return (StronglyConnectedComponentsSolution) super.run(); + } + + @Override + public StronglyConnectedComponentsData getInputData() { + return (StronglyConnectedComponentsData) super.getInputData(); + } } diff --git a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsData.java b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsData.java index 3ba899e..5c99af4 100644 --- a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsData.java +++ b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsData.java @@ -1,9 +1,9 @@ package org.insa.algo.strongconnectivity; -import org.insa.algo.AbstractData; +import org.insa.algo.AbstractInputData; import org.insa.graph.Graph; -public class StronglyConnectedComponentsData extends AbstractData { +public class StronglyConnectedComponentsData extends AbstractInputData { /** * diff --git a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsSolution.java b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsSolution.java index 753a6cb..cde68f2 100644 --- a/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsSolution.java +++ b/src/main/org/insa/algo/strongconnectivity/StronglyConnectedComponentsSolution.java @@ -10,13 +10,13 @@ public class StronglyConnectedComponentsSolution extends AbstractSolution { // Components private ArrayList> components; - protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData instance) { - super(instance); + protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData data) { + super(data); } - protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData instance, Status status, - ArrayList> components) { - super(instance, status); + protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData data, + Status status, ArrayList> components) { + super(data, status); this.components = components; } diff --git a/src/main/org/insa/algo/strongconnectivity/TarjanAlgorithm.java b/src/main/org/insa/algo/strongconnectivity/TarjanAlgorithm.java index ccff6aa..fc2363b 100644 --- a/src/main/org/insa/algo/strongconnectivity/TarjanAlgorithm.java +++ b/src/main/org/insa/algo/strongconnectivity/TarjanAlgorithm.java @@ -10,135 +10,135 @@ import org.insa.graph.Graph; import org.insa.graph.Node; public class TarjanAlgorithm extends StronglyConnectedComponentsAlgorithm { - - private final static int UNDEFINED = -1; - // Stack of nodes and flags. - private Stack stack; - private boolean[] inStack; - - // Current index. - private int index; - - // Information of nodes - private int[] indexes; - private int[] lowlink; - - // Array of strongly connected components - ArrayList> components; + private final static int UNDEFINED = -1; - public TarjanAlgorithm(StronglyConnectedComponentsData instance) { - super(instance); - } - - /** - * Push the given node to the stack. - * - * @param node - */ - protected void pushNode(Node node) { - stack.push(node); - inStack[node.getId()] = true; - } - - /** - * Pop and return a node from the stack. - * - * @return Node popped from the stack - */ - protected Node popNode() { - Node top = stack.pop(); - inStack[top.getId()] = false; - return top; - } - - /** - * Check if the given node is in the stack. - * - * @param node - * - * @return true if the given node is in the stack, false otherwize. - */ - protected boolean isInStack(Node node) { - return inStack[node.getId()]; - } - - /** - * Find the strong component containing the given node. - * - * @param node - * - * @return The strong component containing the given node. - */ - protected void findAndAddStrongComponent(Node v) { - - // Update node info, index and push the node. - indexes[v.getId()] = index; - lowlink[v.getId()] = index; - index += 1; - pushNode(v); - - for (Arc a: v.getSuccessors()) { - Node w = a.getDestination(); - if (!hasBeenVisited(w)) { - findAndAddStrongComponent(w); - lowlink[v.getId()] = Math.min(lowlink[v.getId()], lowlink[w.getId()]); - } - else if (isInStack(w)) { - lowlink[v.getId()] = Math.min(lowlink[v.getId()], indexes[w.getId()]); - } - } - - // Compute the component (if any) - if (lowlink[v.getId()] == indexes[v.getId()]) { - ArrayList component = new ArrayList(); - Node w; - do { - w = popNode(); - component.add(w); - } while (!w.equals(v)); - components.add(component); - System.out.println("Size of the stack: " + stack.size()); - } - - } - - /** - * Check if the given node has not been visited yet. - * - * @return true if the node has been visited. - */ - protected boolean hasBeenVisited(Node node) { - return this.indexes[node.getId()] != UNDEFINED; - } + // Stack of nodes and flags. + private Stack stack; + private boolean[] inStack; - @Override - protected StronglyConnectedComponentsSolution doRun() { - Graph graph = getInputData().getGraph(); - - components = new ArrayList>(); - - // Initialize everything - final int nbNodes = graph.getNodes().size(); - stack = new Stack(); - inStack = new boolean[nbNodes]; - - // Current index. - index = 0; - - // Information of nodes - indexes = new int[nbNodes]; - Arrays.fill(indexes, UNDEFINED); - lowlink = new int[nbNodes]; - - // Find components - for (Node node: graph.getNodes()) { - if (!hasBeenVisited(node)) { - findAndAddStrongComponent(node); - } - } - - return new StronglyConnectedComponentsSolution(getInputData(), Status.OPTIMAL, components); - } + // Current index. + private int index; + + // Information of nodes + private int[] indexes; + private int[] lowlink; + + // Array of strongly connected components + ArrayList> components; + + public TarjanAlgorithm(StronglyConnectedComponentsData data) { + super(data); + } + + /** + * Push the given node to the stack. + * + * @param node + */ + protected void pushNode(Node node) { + stack.push(node); + inStack[node.getId()] = true; + } + + /** + * Pop and return a node from the stack. + * + * @return Node popped from the stack + */ + protected Node popNode() { + Node top = stack.pop(); + inStack[top.getId()] = false; + return top; + } + + /** + * Check if the given node is in the stack. + * + * @param node + * + * @return true if the given node is in the stack, false otherwize. + */ + protected boolean isInStack(Node node) { + return inStack[node.getId()]; + } + + /** + * Find the strong component containing the given node. + * + * @param node + * + * @return The strong component containing the given node. + */ + protected void findAndAddStrongComponent(Node v) { + + // Update node info, index and push the node. + indexes[v.getId()] = index; + lowlink[v.getId()] = index; + index += 1; + pushNode(v); + + for (Arc a: v.getSuccessors()) { + Node w = a.getDestination(); + if (!hasBeenVisited(w)) { + findAndAddStrongComponent(w); + lowlink[v.getId()] = Math.min(lowlink[v.getId()], lowlink[w.getId()]); + } + else if (isInStack(w)) { + lowlink[v.getId()] = Math.min(lowlink[v.getId()], indexes[w.getId()]); + } + } + + // Compute the component (if any) + if (lowlink[v.getId()] == indexes[v.getId()]) { + ArrayList component = new ArrayList(); + Node w; + do { + w = popNode(); + component.add(w); + } while (!w.equals(v)); + components.add(component); + System.out.println("Size of the stack: " + stack.size()); + } + + } + + /** + * Check if the given node has not been visited yet. + * + * @return true if the node has been visited. + */ + protected boolean hasBeenVisited(Node node) { + return this.indexes[node.getId()] != UNDEFINED; + } + + @Override + protected StronglyConnectedComponentsSolution doRun() { + Graph graph = getInputData().getGraph(); + + components = new ArrayList>(); + + // Initialize everything + final int nbNodes = graph.getNodes().size(); + stack = new Stack(); + inStack = new boolean[nbNodes]; + + // Current index. + index = 0; + + // Information of nodes + indexes = new int[nbNodes]; + Arrays.fill(indexes, UNDEFINED); + lowlink = new int[nbNodes]; + + // Find components + for (Node node: graph.getNodes()) { + if (!hasBeenVisited(node)) { + findAndAddStrongComponent(node); + } + } + + return new StronglyConnectedComponentsSolution(getInputData(), Status.OPTIMAL, components); + } } diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java index 13eed13..932b928 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java @@ -13,15 +13,14 @@ import org.insa.graph.Arc; import org.insa.graph.Graph; import org.insa.graph.Node; -public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm { +public class WeaklyConnectedComponentsAlgorithm + extends AbstractAlgorithm { /** - * - * @param instance - * @param logOutput + * @param data */ - public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData instance) { - super(instance); + public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData data) { + super(data); } @Override @@ -70,7 +69,7 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm> createUndirectedGraph() { int nNodes = getInputData().getGraph().getNodes().size(); diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java index dea2b56..7124782 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java @@ -1,9 +1,9 @@ package org.insa.algo.weakconnectivity; -import org.insa.algo.AbstractData; +import org.insa.algo.AbstractInputData; import org.insa.graph.Graph; -public class WeaklyConnectedComponentsData extends AbstractData { +public class WeaklyConnectedComponentsData extends AbstractInputData { /** * diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsSolution.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsSolution.java index 7fdcdcd..03843f7 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsSolution.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsSolution.java @@ -10,13 +10,13 @@ public class WeaklyConnectedComponentsSolution extends AbstractSolution { // Components private ArrayList> components; - protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData instance) { - super(instance); + protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData data) { + super(data); } - protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData instance, Status status, + protected WeaklyConnectedComponentsSolution(WeaklyConnectedComponentsData data, Status status, ArrayList> components) { - super(instance, status); + super(data, status); this.components = components; } diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index 5027833..0534b5d 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -617,9 +617,8 @@ public class MainWindow extends JFrame { wccItem.addActionListener(baf.createBlockingAction(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - WeaklyConnectedComponentsData instance = new WeaklyConnectedComponentsData(graph); WeaklyConnectedComponentsAlgorithm algo = new WeaklyConnectedComponentsAlgorithm( - instance); + new WeaklyConnectedComponentsData(graph)); algo.addObserver(new WeaklyConnectedComponentGraphicObserver(drawing)); algo.addObserver(new WeaklyConnectedComponentTextObserver(printStream)); launchThread(new Runnable() {