Skip to content

Commit

Permalink
added Energy minimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
PtrMan committed Nov 21, 2014
1 parent 17935a5 commit bcbc6dd
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 23 deletions.
110 changes: 109 additions & 1 deletion DecoratedCausalGraph.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package causalreasoningsystem;

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

public class DecoratedCausalGraph
{
Expand Down Expand Up @@ -88,19 +88,51 @@ public OutgoingEdge getRandomOutgoingEdgeWithRemainingOutgoingEdgeBounced(Random
}
}
}*/

public Anotation clone()
{
Anotation cloned;

cloned = new Anotation();
cloned.outputIndex = outputIndex;
cloned.incommingEdgesRedFlagsCounter = incommingEdgesRedFlagsCounter;
cloned.incommingEdgesRedFlags = incommingEdgesRedFlags;
cloned.incommingEdgesRedFlags = Arrays.copyOf(incommingEdgesRedFlags, incommingEdgesRedFlags.length);
cloned.isOrWasInWorkingSet = isOrWasInWorkingSet;

return cloned;
}
}

public Anotation anotation = new Anotation();

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



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

public Node clone()
{
Node cloned;

cloned = new Node();

cloned.outgoingEdgeElementIndices = Arrays.copyOf(outgoingEdgeElementIndices, outgoingEdgeElementIndices.length);
cloned.incommingEdgeElementIndices = Arrays.copyOf(incommingEdgeElementIndices, outgoingEdgeElementIndices.length);
cloned.anotation = anotation.clone();

return cloned;
}

}

public int energy = Integer.MAX_VALUE;

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

//public int[] rootIndices;
Expand All @@ -117,4 +149,80 @@ public void resetAnnotation()
nodes.get(i).anotation.incommingEdgesRedFlags = new boolean[incommingEdgesArrayLength];
}
}

public void updateEnergy() throws Exception
{
/*
* for each Node
* * calculate the energy(distance) to all outgoing nodes and add it to energy
*
*/

energy = 0;

for( Node iterationNode : nodes )
{
int currentNodeIndex;

currentNodeIndex = iterationNode.anotation.outputIndex;

for( int iterationOutgoingElementIndex : iterationNode.outgoingEdgeElementIndices )
{
int outputNodeIndex;

outputNodeIndex = nodes.get(iterationOutgoingElementIndex).anotation.outputIndex;

// assert
if( currentNodeIndex > outputNodeIndex )
{
throw new Exception();
}

energy += (outputNodeIndex - currentNodeIndex - 1);
}
}
}

public ArrayList<Integer> getRootIndices()
{
ArrayList<Integer> result;
int i;

result = new ArrayList<Integer>();

i = 0;
for( DecoratedCausalGraph.Node iterationNode : nodes )
{
if( iterationNode.isRoot() )
{
result.add(new Integer(i));
}

i++;
}

return result;
}

/** does make a deep copy
*
*/
public DecoratedCausalGraph clone()
{
DecoratedCausalGraph cloned;

cloned = new DecoratedCausalGraph();
cloned.energy = energy;

for( int i = 0; i < nodes.size(); i++ )
{
Node clonedNode;

clonedNode = nodes.get(i).clone();

cloned.nodes.add(clonedNode);
}

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

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

public class EnergyMinimizer
{
public static class State
{
public DecoratedCausalGraph workingGraph; // graph which is being modified
public DecoratedCausalGraph graphWithMinimalEnergy;
public int minimalEnergy = Integer.MAX_VALUE;
public ArrayList<Integer> minimalSequence;
}

public static void minimize(Random random, int numberOfSteps, State state) throws Exception
{
int step;

for( step = 0; step < numberOfSteps; step++ )
{
minimizeSingleStep(random, state);
}
}

private static void minimizeSingleStep(Random random, State state) throws Exception
{
ArrayList<Integer> potentialMinimalSequence;

potentialMinimalSequence = TrackbackGenerator.generate(random, state.workingGraph);
state.workingGraph.updateEnergy();

if( state.workingGraph.energy < state.minimalEnergy )
{
state.minimalEnergy = state.workingGraph.energy;
state.graphWithMinimalEnergy = state.workingGraph.clone();
state.minimalSequence = potentialMinimalSequence;
}
}
}
23 changes: 1 addition & 22 deletions TrackbackGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static public ArrayList<Integer> generate(Random random, DecoratedCausalGraph ca
causalGraph.resetAnnotation();

// add all root indices
workingNodeIndices = getRootIndices(causalGraph);
workingNodeIndices = causalGraph.getRootIndices();

for(;;)
{
Expand Down Expand Up @@ -211,27 +211,6 @@ static private void bounceFrom(int nodeIndex, DecoratedCausalGraph causalGraph)
}
}*/

static private ArrayList<Integer> getRootIndices(DecoratedCausalGraph causalGraph)
{
ArrayList<Integer> result;
int i;

result = new ArrayList<Integer>();

i = 0;
for( DecoratedCausalGraph.Node iterationNode : causalGraph.nodes )
{
if( iterationNode.isRoot() )
{
result.add(new Integer(i));
}

i++;
}

return result;
}

static private int getIndexOfElementInArray(int[] array, int element)
{
int i;
Expand Down

0 comments on commit bcbc6dd

Please sign in to comment.