Instance -> Data.

This commit is contained in:
Mikael Capelle 2018-03-02 15:30:55 +01:00
parent e111b5f0e6
commit 4017577269
12 changed files with 288 additions and 276 deletions

View File

@ -1,4 +1,4 @@
package org.insa.algo ;
package org.insa.algo;
import java.time.Duration;
import java.time.Instant;
@ -6,17 +6,25 @@ import java.util.ArrayList;
public abstract class AbstractAlgorithm<Observer> {
protected AbstractData instance;
protected AbstractInputData data;
protected ArrayList<Observer> observers;
protected AbstractAlgorithm(AbstractData instance) {
this.instance = instance;
this.observers = new ArrayList<Observer>();
/**
* @param data
*/
protected AbstractAlgorithm(AbstractInputData data) {
this.data = data;
this.observers = new ArrayList<Observer>();
}
protected AbstractAlgorithm(AbstractData instance, ArrayList<Observer> observers) {
this.instance = instance;
this.observers = observers;;
/**
* @param data
* @param observers
*/
protected AbstractAlgorithm(AbstractInputData data, ArrayList<Observer> observers) {
this.data = data;
this.observers = observers;
;
}
/**
@ -25,20 +33,22 @@ public abstract class AbstractAlgorithm<Observer> {
* @param observer
*/
public void addObserver(Observer observer) {
observers.add(observer);
observers.add(observer);
}
/**
* @return The list of observers for this algorithm.
*/
public ArrayList<Observer> 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.
@ -46,10 +56,10 @@ public abstract class AbstractAlgorithm<Observer> {
* @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;
}
/**

View File

@ -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; }
}

View File

@ -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;
}
}

View File

@ -4,71 +4,74 @@ import java.time.Duration;
public abstract class AbstractSolution {
/**
* Possible status for a solution.
*
*/
public enum Status {
UNKNOWN,
INFEASIBLE,
FEASIBLE,
OPTIMAL,
};
/**
* Possible status for a solution.
*
*/
public enum Status {
UNKNOWN, INFEASIBLE, FEASIBLE, OPTIMAL,
};
// Status of the solution.
Status status;
// Status of the solution.
Status status;
// Solving time for the solution
Duration solvingTime;
// Solving time for the solution.
Duration solvingTime;
// Original instance of the solution
AbstractData instance;
// Original input of the solution.
AbstractInputData data;
/**
* 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;
}
/**
* 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(AbstractData instance, Status status) {
this.instance = instance;
this.status = status;
}
protected AbstractSolution(AbstractInputData data, Status status) {
this.data = data;
this.status = status;
}
/**
* @return Original instance for this solution.
*/
public AbstractData getInputData() { return instance; }
/**
* @return Original input for this solution.
*/
public AbstractInputData getInputData() {
return data;
}
/**
* @return Status of this solution.
*/
public Status getStatus() { return status; }
/**
* @return Status of this solution.
*/
public Status getStatus() {
return status;
}
/**
* @return Solving time of this solution.
*/
public Duration getSolvingTime() { return solvingTime; }
/**
* @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;
}
/**
* 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;
}
/**
* @return true if the solution is feasible or optimal.
*/
public boolean isFeasible() {
return status == Status.FEASIBLE || status == Status.OPTIMAL;
}
}

View File

@ -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<StronglyConnectedComponentObserver> {
public abstract class StronglyConnectedComponentsAlgorithm
extends AbstractAlgorithm<StronglyConnectedComponentObserver> {
/**
*
* @param instance
* @param logOutput
*/
public StronglyConnectedComponentsAlgorithm(StronglyConnectedComponentsData instance) {
super(instance);
}
/**
* @param data
*/
public StronglyConnectedComponentsAlgorithm(StronglyConnectedComponentsData data) {
super(data);
}
@Override
public StronglyConnectedComponentsSolution run() {
return (StronglyConnectedComponentsSolution)super.run();
}
@Override
public StronglyConnectedComponentsSolution run() {
return (StronglyConnectedComponentsSolution) super.run();
}
@Override
public StronglyConnectedComponentsData getInputData() {
return (StronglyConnectedComponentsData)super.getInputData();
}
@Override
public StronglyConnectedComponentsData getInputData() {
return (StronglyConnectedComponentsData) super.getInputData();
}
}

View File

@ -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 {
/**
*

View File

@ -10,13 +10,13 @@ public class StronglyConnectedComponentsSolution extends AbstractSolution {
// Components
private ArrayList<ArrayList<Node>> components;
protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData instance) {
super(instance);
protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData data) {
super(data);
}
protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData instance, Status status,
ArrayList<ArrayList<Node>> components) {
super(instance, status);
protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsData data,
Status status, ArrayList<ArrayList<Node>> components) {
super(data, status);
this.components = components;
}

View File

@ -11,134 +11,134 @@ import org.insa.graph.Node;
public class TarjanAlgorithm extends StronglyConnectedComponentsAlgorithm {
private final static int UNDEFINED = -1;
private final static int UNDEFINED = -1;
// Stack of nodes and flags.
private Stack<Node> stack;
private boolean[] inStack;
// Stack of nodes and flags.
private Stack<Node> stack;
private boolean[] inStack;
// Current index.
private int index;
// Current index.
private int index;
// Information of nodes
private int[] indexes;
private int[] lowlink;
// Information of nodes
private int[] indexes;
private int[] lowlink;
// Array of strongly connected components
ArrayList<ArrayList<Node>> components;
// Array of strongly connected components
ArrayList<ArrayList<Node>> components;
public TarjanAlgorithm(StronglyConnectedComponentsData instance) {
super(instance);
}
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;
}
/**
* 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;
}
/**
* 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()];
}
/**
* 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) {
/**
* 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);
// 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()]);
}
}
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<Node> component = new ArrayList<Node>();
Node w;
do {
w = popNode();
component.add(w);
} while (!w.equals(v));
components.add(component);
System.out.println("Size of the stack: " + stack.size());
}
// Compute the component (if any)
if (lowlink[v.getId()] == indexes[v.getId()]) {
ArrayList<Node> component = new ArrayList<Node>();
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;
}
/**
* 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();
@Override
protected StronglyConnectedComponentsSolution doRun() {
Graph graph = getInputData().getGraph();
components = new ArrayList<ArrayList<Node>>();
components = new ArrayList<ArrayList<Node>>();
// Initialize everything
final int nbNodes = graph.getNodes().size();
stack = new Stack<Node>();
inStack = new boolean[nbNodes];
// Initialize everything
final int nbNodes = graph.getNodes().size();
stack = new Stack<Node>();
inStack = new boolean[nbNodes];
// Current index.
index = 0;
// Current index.
index = 0;
// Information of nodes
indexes = new int[nbNodes];
Arrays.fill(indexes, UNDEFINED);
lowlink = new int[nbNodes];
// 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);
}
}
// Find components
for (Node node: graph.getNodes()) {
if (!hasBeenVisited(node)) {
findAndAddStrongComponent(node);
}
}
return new StronglyConnectedComponentsSolution(getInputData(), Status.OPTIMAL, components);
}
return new StronglyConnectedComponentsSolution(getInputData(), Status.OPTIMAL, components);
}
}

View File

@ -13,15 +13,14 @@ import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Node;
public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<WeaklyConnectedComponentObserver> {
public class WeaklyConnectedComponentsAlgorithm
extends AbstractAlgorithm<WeaklyConnectedComponentObserver> {
/**
*
* @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<Weakly
/**
* @return An adjacency list for the undirected graph equivalent to the stored
* graph.
* graph.
*/
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
int nNodes = getInputData().getGraph().getNodes().size();

View File

@ -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 {
/**
*

View File

@ -10,13 +10,13 @@ public class WeaklyConnectedComponentsSolution extends AbstractSolution {
// Components
private ArrayList<ArrayList<Node>> 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<ArrayList<Node>> components) {
super(instance, status);
super(data, status);
this.components = components;
}

View File

@ -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() {