Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
PtrMan committed Sep 27, 2014
1 parent 64facc7 commit 17935a5
Show file tree
Hide file tree
Showing 5 changed files with 506 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CausalReasoningSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package causalreasoningsystem;

import java.util.ArrayList;
import java.util.Random;

public class CausalReasoningSystem
{
public static void main(String[] args)
{
InputGraph inputGraph = new InputGraph();
inputGraph.numberOfNodes = 8;
inputGraph.connections.add(new InputGraph.Connection(0, 2));
inputGraph.connections.add(new InputGraph.Connection(1, 2));
inputGraph.connections.add(new InputGraph.Connection(1, 3));
inputGraph.connections.add(new InputGraph.Connection(4, 3));
inputGraph.connections.add(new InputGraph.Connection(5, 3));
inputGraph.connections.add(new InputGraph.Connection(7, 3));
inputGraph.connections.add(new InputGraph.Connection(4, 6));


/* works
InputGraph inputGraph = new InputGraph();
inputGraph.numberOfNodes = 4;
inputGraph.connections.add(new InputGraph.Connection(0, 3));
inputGraph.connections.add(new InputGraph.Connection(1, 3));
inputGraph.connections.add(new InputGraph.Connection(2, 3));
*/

// TODO< cristal example >

DecoratedCausalGraph causalGraph = ConvertInputGraphToCausalGraph.convert(inputGraph);

ArrayList<Integer> result = TrackbackGenerator.generate(new Random(), causalGraph);

int x = 0;
}

}
77 changes: 77 additions & 0 deletions ConvertInputGraphToCausalGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package causalreasoningsystem;

import java.util.ArrayList;

public class ConvertInputGraphToCausalGraph
{
public static DecoratedCausalGraph convert(InputGraph input)
{
DecoratedCausalGraph resultGraph;
int[] numberOfInputEdges;
int[] numberOfOutputEdges;
int nodeIndex;

// indices for the index into the arrays of the nodes
int[] incommingEdgesIndices;
int[] outgoingEdgesIndices;

numberOfInputEdges = new int[input.numberOfNodes];
numberOfOutputEdges = new int[input.numberOfNodes];

resultGraph = new DecoratedCausalGraph();
resultGraph.nodes = createNodes(input.numberOfNodes);

// count all input/output edges
for( InputGraph.Connection iterationConnection : input.connections )
{
numberOfOutputEdges[iterationConnection.sourceIndex]++;
numberOfInputEdges[iterationConnection.destinationIndex]++;
}

// allocate all input/output edges
nodeIndex = 0;
for( DecoratedCausalGraph.Node iterationNode : resultGraph.nodes )
{
iterationNode.incommingEdgeElementIndices = new int[numberOfInputEdges[nodeIndex]];
iterationNode.outgoingEdgeElementIndices = new int[numberOfOutputEdges[nodeIndex]];

nodeIndex++;
}

// fill incomming/outgoing edge arrays
incommingEdgesIndices = new int[input.numberOfNodes];
outgoingEdgesIndices = new int[input.numberOfNodes];

for( InputGraph.Connection iterationConnection : input.connections )
{
int incommingArrayIndex;
int outgoingArrayIndex;

incommingArrayIndex = incommingEdgesIndices[iterationConnection.destinationIndex];
outgoingArrayIndex = outgoingEdgesIndices[iterationConnection.sourceIndex];

resultGraph.nodes.get(iterationConnection.destinationIndex).incommingEdgeElementIndices[incommingArrayIndex] = iterationConnection.sourceIndex;
resultGraph.nodes.get(iterationConnection.sourceIndex).outgoingEdgeElementIndices[outgoingArrayIndex] = iterationConnection.destinationIndex;

incommingEdgesIndices[iterationConnection.destinationIndex]++;
outgoingEdgesIndices[iterationConnection.sourceIndex]++;
}

return resultGraph;
}

private static ArrayList<DecoratedCausalGraph.Node> createNodes(int count)
{
ArrayList<DecoratedCausalGraph.Node> result;
int i;

result = new ArrayList<DecoratedCausalGraph.Node>();

for( i = 0; i < count; i++ )
{
result.add(new DecoratedCausalGraph.Node());
}

return result;
}
}
120 changes: 120 additions & 0 deletions DecoratedCausalGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package causalreasoningsystem;

import java.util.ArrayList;
import java.util.Random;

public class DecoratedCausalGraph
{
public static class Node
{
public static class Anotation
{
/*
public class OutgoingEdge
{
public boolean isRed;
public ArrayList<Integer> outgoingEdgeBounced = new ArrayList<Integer>();
// invariant: elements in that list are not allready in the output
// elements get removed as elements are taken out
public boolean areRemainingOutgoingEdgeBounced()
{
return outgoingEdgeBounced.size() > 0;
}
}
public OutgoingEdge[] outgoingEdges;
*/

public boolean isOrWasInWorkingSet; // is/was this node in the working set for ttraversal?

// flags used to indicate that the node of the incomming edge must occur before that node
// flag is not set for elements which are allready in the output
public boolean[] incommingEdgesRedFlags;

public int incommingEdgesRedFlagsCounter;

public void recountIncommingRedFlags()
{
int incommingEdgeI;

incommingEdgesRedFlagsCounter = 0;

for( incommingEdgeI = 0; incommingEdgeI < incommingEdgesRedFlags.length; incommingEdgeI++ )
{
if( incommingEdgesRedFlags[incommingEdgeI] )
{
incommingEdgesRedFlagsCounter++;
}
}
}

public int outputIndex = -1;

public boolean isInOutput()
{
return outputIndex != -1;
}

/*
public boolean existAnyRemainingOutgoingEdgeBounced()
{
int i;
for( i = 0; i < outgoingEdges.length; i++ )
{
if( outgoingEdges[i].areRemainingOutgoingEdgeBounced() )
{
return true;
}
}
return false;
}
// INVARIANT< only callable if existAnyRemainingOutgoingEdgeBounced() returns true, else it goes into an infinite loop >
public OutgoingEdge getRandomOutgoingEdgeWithRemainingOutgoingEdgeBounced(Random random)
{
for(;;)
{
int currentOutgoingEdgeIndex;
currentOutgoingEdgeIndex = random.nextInt(outgoingEdges.length);
if( outgoingEdges[currentOutgoingEdgeIndex].areRemainingOutgoingEdgeBounced() )
{
return outgoingEdges[currentOutgoingEdgeIndex];
}
}
}*/
}

public Anotation anotation = new Anotation();

public int[] outgoingEdgeElementIndices;
public int[] incommingEdgeElementIndices;

public boolean isRoot()
{
return incommingEdgeElementIndices.length == 0;
}
}

public ArrayList<Node> nodes = new ArrayList<Node>();

//public int[] rootIndices;

public void resetAnnotation()
{
int i;

for( i = 0; i < nodes.size(); i++ )
{
nodes.get(i).anotation = new Node.Anotation();

int incommingEdgesArrayLength = nodes.get(i).incommingEdgeElementIndices.length;
nodes.get(i).anotation.incommingEdgesRedFlags = new boolean[incommingEdgesArrayLength];
}
}
}
22 changes: 22 additions & 0 deletions InputGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package causalreasoningsystem;

import java.util.ArrayList;

public class InputGraph
{
public static class Connection
{
public int sourceIndex;
public int destinationIndex;

public Connection(int sourceIndex, int destinationIndex)
{
this.sourceIndex = sourceIndex;
this.destinationIndex = destinationIndex;
}
}

public ArrayList<Connection> connections = new ArrayList<InputGraph.Connection>();

public int numberOfNodes;
}
Loading

0 comments on commit 17935a5

Please sign in to comment.