Skip to content

Commit

Permalink
Refactoring and Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-CS committed Sep 15, 2021
1 parent 135f4fb commit 286219b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 0 additions & 50 deletions Equations.txt

This file was deleted.

24 changes: 15 additions & 9 deletions src/io/github/ethankelly/ODESystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.ethankelly.graph.GraphGenerator;
import io.github.ethankelly.graph.Vertex;
import io.github.ethankelly.symbols.Greek;
import io.github.ethankelly.symbols.Maths;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MaxCountExceededException;
import org.apache.commons.math3.ode.FirstOrderDifferentialEquations;
Expand Down Expand Up @@ -71,24 +72,29 @@ public static void main(String[] args) {

// Print the initial state
System.out.println("Initial state:\n" + Arrays.toString(y0));
// Specify the initial condition used in this test
System.out.println("I.e. " + Maths.L_ANGLE.uni() + "S0_I1_S2" + Maths.R_ANGLE.uni());
// Print relevant system information
System.out.println(triangle);
// Empty array to contain derivative values
double[] yDot = Arrays.copyOf(y0, y0.length);
integrator.integrate(triangle, 0, y0, 2, yDot);

// Integrate up to specified t value
integrator.integrate(triangle, 0, y0, 10, yDot);
// Compute derivatives based on equations generation
triangle.computeDerivatives(0, y0, yDot);

// Print found values to 2 d.p. for clarity of system output
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);

System.out.println("Final state:\n");
System.out.println("Final state at t=10:\n");
for (Tuple t : triangle.requiredTuples.getTuples()) {
System.out.print(t + " = ");
double result = yDot[triangle.getIndicesMapping().get(t)];
if (result > 1) System.out.print("1.00");
else if (result > 0) System.out.print(df.format(result));
else if (result <= 0) System.out.print("0.00");
System.out.print(t + Maths.PRIME.uni() + " = ");
double resultPrime = yDot[triangle.getIndicesMapping().get(t)];
System.out.print(df.format(resultPrime));
System.out.println();
}
System.out.println(triangle);

}

/**
Expand Down
56 changes: 50 additions & 6 deletions src/io/github/ethankelly/RequiredTuples.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,57 @@ public RequiredTuples(Graph graph, char[] states, boolean closures) {
* @param args command-line arguments (ignored).
*/
public static void main(String[] args) {
RequiredTuples t1 = new RequiredTuples(GraphGenerator.getTriangle(), new char[] {'S', 'I', 'R'}, false);
System.out.println(t1.findSingles());
System.out.println(t1.getTuples() + "\nSize: " + t1.getTuples().size());
// RequiredTuples t1 = new RequiredTuples(GraphGenerator.getTriangle(), new char[] {'S', 'I', 'R'}, false);
// System.out.println(t1.findSingles());
// System.out.println(t1.getTuples() + "\nSize: " + t1.getTuples().size());
//
// RequiredTuples t2 = new RequiredTuples(GraphGenerator.getTriangle(), new char[] {'S', 'I', 'R'}, true);
// System.out.println(t2.findSingles());
// System.out.println(t2.getTuples() + "\nSize: " + t2.getTuples().size());

RequiredTuples complete5 = new RequiredTuples(GraphGenerator.complete(5), new char[]{'S', 'I', 'R'}, false);
RequiredTuples cycle5 = new RequiredTuples(GraphGenerator.cycle(5), new char[]{'S', 'I', 'R'}, false);
RequiredTuples complete3 = new RequiredTuples(GraphGenerator.complete(3), new char[]{'S', 'I', 'R'}, false);
RequiredTuples cycle3 = new RequiredTuples(GraphGenerator.cycle(3), new char[]{'S', 'I', 'R'}, false);
RequiredTuples complete4 = new RequiredTuples(GraphGenerator.complete(4), new char[]{'S', 'I', 'R'}, false);
RequiredTuples cycle4 = new RequiredTuples(GraphGenerator.cycle(4), new char[]{'S', 'I', 'R'}, false);

System.out.println("CYCLE 3");
// System.out.println(cycle3.findSingles());
System.out.println(
// cycle3.getTuples() +
"Size: " + cycle3.getTuples().size());

System.out.println("\nCOMPLETE 3");
// System.out.println(complete3.findSingles());
System.out.println(
// complete3.getTuples() +
"Size: " + complete3.getTuples().size());

System.out.println("\nCYCLE 4");
// System.out.println(cycle4.findSingles());
System.out.println(
cycle4.getTuples() +
"Size: " + cycle4.getTuples().size());

System.out.println("\nCOMPLETE 4");
// System.out.println(complete4.findSingles());
System.out.println(
// complete4.getTuples() +
"Size: " + complete4.getTuples().size());

System.out.println("\nCYCLE 5");
// System.out.println(cycle5.findSingles());
System.out.println(
cycle5.getTuples() +
"Size: " + cycle5.getTuples().size());

System.out.println("\nCOMPLETE 5");
// System.out.println(complete5.findSingles());
System.out.println(
// complete5.getTuples() +
"Size: " + complete5.getTuples().size());

RequiredTuples t2 = new RequiredTuples(GraphGenerator.getTriangle(), new char[] {'S', 'I', 'R'}, true);
System.out.println(t2.findSingles());
System.out.println(t2.getTuples() + "\nSize: " + t2.getTuples().size());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/io/github/ethankelly/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean areStatesDifferent(boolean reqClosures) {
/**
* Given some list of vertices (some tuple), this method checks whether the index locations of every element of the
* tuple are all distinct. If even two of the vertices have the same location, it is not a valid tuple for our
* purposes and we will not hve to consider the associated equation in the final system of equations that describes
* purposes, and we will not have to consider the associated equation in the final system of equations that describes
* our compartmental model.
*
* @return true if no index location is repeated in the vertices, false otherwise.
Expand All @@ -81,7 +81,7 @@ public boolean areLocationsDifferent() {

/**
* Given a potential tuple, this method checks that the states of each probability are not all the same, that each
* probability corresponds to a different vertex and that all of the vertices involved are in fact connected. If
* probability corresponds to a different vertex and that all the vertices involved are in fact connected. If
* these three conditions are met, then it is a tuple that we are required to express in the total set of equations
* describing the system dynamics.
*
Expand Down
11 changes: 8 additions & 3 deletions src/io/github/ethankelly/graph/GraphGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,13 @@ public static Graph erdosRenyi(int numVertices, double probability) {
* @return the complete graph on the given number of vertices.
*/
public static Graph complete(int numVertices) {
return erdosRenyi(numVertices, 1.0).setName("Complete");
Graph complete = new Graph(numVertices, "Complete");
for (int i = 0; i < complete.getNumVertices(); i++) {
for (int j = 0; j < complete.getNumVertices(); j++) {
if (i != j && !complete.hasEdge(i, j)) complete.addEdge(i, j);
}
}
return complete;
}

/**
Expand Down Expand Up @@ -541,9 +547,8 @@ public static Graph binaryTree(int numVertices) {
*/
public static Graph cycle(int numVertices) {
Graph g = new Graph(numVertices, "Cycle");
// Generate an array: [0, 1, ..., numVertices] and randomly shuffle it.
// Generate an array: [0, 1, ..., numVertices]
int[] vertices = IntStream.range(0, numVertices).toArray();
Rand.shuffle(vertices);
// Connect vertices from 0 to 2 less than the number of vertices consecutively
for (int i = 0; i < numVertices - 1; i++) {
g.addEdge(vertices[i], vertices[i + 1]);
Expand Down
5 changes: 3 additions & 2 deletions src/io/github/ethankelly/symbols/Maths.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public enum Maths {

L_ANGLE("\u3008"),
R_ANGLE("\u3009");
R_ANGLE("\u3009"),
PRIME("\u2032");

private final String uni;
private final String uni;

Maths(String uni) {
this.uni = uni;
Expand Down

0 comments on commit 286219b

Please sign in to comment.