public void InterpreterTest() { // // TODO: Add test logic here // byte[] program = { 1, 2, 9 }; // byte[] program = { 1,8,8,5,2,9,5,6,6,4,4,4,9,9,6,5,8,3,3,3,9,8,3,9,9,5,6,6,4,9,9,9,5,3,9,9,9,5,4,9,9,9,8,8,2,2,9,9,3,9 }; GeneticProgramming gp = new GeneticProgramming(); String Expected = "RootNode( <Punch> ) )"; IMyBehaviourTreeNode test_tree = gp.GetInterpTree(program); MyTreeBuilder test = gp.interp_tree_builder; String[] list; Stack <IMyParentBehaviourTreeNode> nodes = new Stack <IMyParentBehaviourTreeNode>(); //String Actual = test_tree.ToString(); foreach (IMyParentBehaviourTreeNode i in test.parentNodeStack) { nodes.Push(i); } Stack <IMyParentBehaviourTreeNode> Actual = nodes; Assert.AreEqual(Expected, Actual, " Error incorrect result"); }
public override void call(List<Datastructures.Variadic> parameters, List<Datastructures.Variadic> globals, out Datastructures.Variadic result, out GeneticProgramming.TypeRestrictedOperator.EnumResult resultCode) { Datastructures.TreeNode resultGraph; Datastructures.TreeNode grid0; Datastructures.TreeNode grid1; // this are arrays wih the indices of the elements in the graph from the elements in the Grid // it has the size of gridSizeX * gridsizeY // is -1 if the grid element wasn't saved allready inside the graph int[] groupsGraphElementIndicesForGrid0; int[] groupsGraphElementIndicesForGrid1; groupsGraphElementIndicesForGrid0 = null; groupsGraphElementIndicesForGrid1 = null; resultCode = GeneticProgramming.TypeRestrictedOperator.EnumResult.FAILED; result = null; // we accept at the current time only two inputs if( parameters.Count != 2 || parameters[0].type != Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE || parameters[1].type != Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE ) { return; } grid0 = parameters[0].valueTree; grid1 = parameters[1].valueTree; // initialize result graph resultGraph = new Datastructures.TreeNode(); resultGraph.childNodes.Add(new Datastructures.TreeNode()); resultGraph.childNodes.Add(new Datastructures.TreeNode()); resultGraph.childNodes.Add(new Datastructures.TreeNode()); result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE); result.valueTree = resultGraph; initializeIndicesOfGrid(ref groupsGraphElementIndicesForGrid0); initializeIndicesOfGrid(ref groupsGraphElementIndicesForGrid1); storeGridElementsIntoGraph(ref groupsGraphElementIndicesForGrid0, grid0, resultGraph.childNodes[GRAPHINDEXVERTICES]); storeGridElementsIntoGraph(ref groupsGraphElementIndicesForGrid1, grid1, resultGraph.childNodes[GRAPHINDEXVERTICES]); // go through each element in grid0 and search for new possible connections to grid1 searchForNewPossibleConnectionsBetweenGrids(ref groupsGraphElementIndicesForGrid0, ref groupsGraphElementIndicesForGrid1, grid0, grid1, resultGraph.childNodes[GRAPHINDEXEDGES], resultGraph.childNodes[GRAPHINDEXVERTICES]); // go through each element in grid1 and search for new possible connections to grid0 searchForNewPossibleConnectionsBetweenGrids(ref groupsGraphElementIndicesForGrid1, ref groupsGraphElementIndicesForGrid0, grid1, grid0, resultGraph.childNodes[GRAPHINDEXEDGES], resultGraph.childNodes[GRAPHINDEXVERTICES]); resultCode = GeneticProgramming.TypeRestrictedOperator.EnumResult.OK; }
public void Init(string path, Random gen) { functionA = new FunctionApproximation(); functionA.LoadKnownValuesFromFile(path); int headSize = 5; gp = new GeneticProgramming(gen, headSize, new string[] { "x" }); gp.PopSize = 1000; gp.ElitismCount = 10; gp.MateCount = 50; gp.MutationCount = 1000; gp.Inputs = functionA.knownValues; }
/// <summary> /// Set up fighters for fight. /// </summary> /// <param name="isBlocking">if set to <c>true</c> [is blocking].</param> /// <param name="isCrouched">if set to <c>true</c> [is crouched].</param> /// <param name="torn_size"></param> /// <param name="pop_size"></param> public void SetUp(bool isBlocking, bool isCrouched, int torn_size, int pop_size) { //reset the counters treeCounter = 0; fighterCounter = 0; roundNum = 0; ListEventsAlgorithm.Clear(); ListOfEvents.Clear(); listOfEvents.Add(":: SET-UP FIGHTERS ::"); roundNum++; listOfEvents.Add("Round NO: " + roundNum); //CREATE new NON_AI_FIGHTER NoAiFighter = new NonAIFighter(roundNum, isBlocking, isCrouched); //set relevent values for NON_AI_FIGHTER //NoAiFighter.Blocking = isBlocking; //NoAiFighter.Crouching = isCrouched; //add new fighter o event list listOfEvents.Add(NoAiFighter.ToString()); //CREATE new AI_FIGHTER AiFighter = new AIFighter(roundNum); //create a new connection to Genetic programming class gp = new GeneticProgramming(this, AiFighter, NoAiFighter,torn_size,pop_size); gp.Crouched = Crouched; gp.Close = Close; gp.Medium = Medium; gp.Far = Far; //set up the Genetic programming class gp.setup(); //set tree to first in generation list treeCounter = 0; AiFighter.Tree = gp.Generation[treeCounter]; //add AI fighter to list listOfEvents.Add(AiFighter.ToString()); }
public void Test() { GeneticProgramming gp = new GeneticProgramming(); IMyBehaviourTreeNode test_tree; String Expected = "Test"; gp.Grow(); byte[] program = gp.GrownProgram; //MyTreeBuilder test = gp.interp_tree_builder; String Actual = ""; for (int i = 0; i != program.Length; i++) { Actual += program[i].ToString() + ", "; } Assert.AreEqual(Expected, Actual, " Error incorrect result"); }
public override void call(List<Datastructures.Variadic> parameters, List<Datastructures.Variadic> globals, out Datastructures.Variadic result, out GeneticProgramming.TypeRestrictedOperator.EnumResult resultCode) { Datastructures.TreeNode graphTreeNode; int edgesCount; int i; // list for the indices of the edges which aren't covered by the scaffold List<int> remainingEdgesIndices = new List<int>(); bool scaffoldAppliedSuccessfull; List<int> scaffoldEdgesIndices; int tryCounter; const int MAXTRIES = 100; resultCode = EnumResult.FAILED; result = null; // BREAK this breaks genericity lines.Clear(); if( parameters.Count != 1 ) { return; } graphTreeNode = parameters[0].valueTree; edgesCount = graphTreeNode.childNodes[GRAPHINDEXEDGES].childNodes.Count; // put all edges into the remainingEdgesIndices for (i = 0; i < edgesCount; i++ ) { remainingEdgesIndices.Add(i); } // try to apply the scaffold to a series of connected edges tryCounter = 0; for(;;) { Scaffolds.Graph.GraphScaffoldInstanceData instanceData; scaffoldEdgesIndices = new List<int>(); tryToApplyScaffoldToRemainingEdges(graphTreeNode, remainingEdgesIndices, out scaffoldAppliedSuccessfull, scaffoldEdgesIndices, out instanceData); if( scaffoldAppliedSuccessfull ) { tryCounter = 0; // remove all remainingEdgesIndices which are mentioned in scaffoldEdgesIndices foreach( int iterationEdgeIndex in scaffoldEdgesIndices ) { remainingEdgesIndices.Remove(iterationEdgeIndex); } // store scaffold edge list away // TODO /* // for now we just rpint it string debugString = ""; foreach( int iterationScaffoldEdgeIndex in scaffoldEdgesIndices ) { debugString += iterationScaffoldEdgeIndex.ToString() + " "; } Console.WriteLine(debugString); */ // BREAK this breaks genericity Line newLine; newLine = new Line(); newLine.a = getVertexPositionByIndexDelegate(graphTreeNode,((Scaffolds.Graph.ExtractLineScaffoldInstanceData)instanceData).lastVertexIndex).valueVector2Float; newLine.b = ((Scaffolds.Graph.ExtractLineScaffoldInstanceData)instanceData).getFirstPosition(); lines.Add(newLine); } // we give up if the maximal try count is reached if( tryCounter > MAXTRIES ) { break; } tryCounter++; } resultCode = EnumResult.OK; }
public override void addOperatorAsParameter(GeneticProgramming.TypeRestrictedOperator parameter) { // TODO throw new NotImplementedException(); }
public void call(List<Datastructures.Variadic> parameters, out Datastructures.Variadic result, out GeneticProgramming.TypeRestrictedOperator.EnumResult resultCode) { int i; List<Vector2<float>>[] groups; int groupI; Datastructures.TreeNode groupsTreeNode; resultCode = GeneticProgramming.TypeRestrictedOperator.EnumResult.FAILED; groupsTreeNode = new Datastructures.TreeNode(); result = new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE); result.valueTree = groupsTreeNode; // initialize groups groups = new List<Vector2<float>>[cachedGroupArraySize.x * cachedGroupArraySize.y]; for( i = 0; i < cachedGroupArraySize.x * cachedGroupArraySize.y; i++ ) { groups[i] = new List<Vector2<float>>(); } if( parameters.Count < 1 || parameters[0].type != Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE ) { // TODO< set error > return; } // go through each point foreach( Datastructures.TreeNode iterationPointNode in parameters[0].valueTree.childNodes ) { Vector2<float> iterationPoint; int groupIndex; if( iterationPointNode.value.type != Datastructures.Variadic.EnumType.VECTOR2FLOAT ) { // TODO< set error > return; } iterationPoint = iterationPointNode.value.valueVector2Float; groupIndex = getGroupIndexOfCoordinate(iterationPoint); groups[groupIndex].Add(iterationPoint); } // build the result for( groupI = 0; groupI < groups.Length; groupI++ ) { Datastructures.TreeNode pointsTreeNode; Datastructures.TreeNode outputElementTreeNode; Vector2<float> center; // middle/average of all points center = new Vector2<float>(); if( groups[groupI].Count == 0 ) { continue; } // we are here if a element is in the group pointsTreeNode = new Datastructures.TreeNode(); foreach( Vector2<float> iterationPoint in groups[groupI] ) { Datastructures.TreeNode treeNodeForPoint; treeNodeForPoint = new Datastructures.TreeNode(); treeNodeForPoint.value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.VECTOR2FLOAT); treeNodeForPoint.value.valueVector2Float = iterationPoint; pointsTreeNode.childNodes.Add(treeNodeForPoint); center += iterationPoint; } center.scale(1.0f / (float)groups[groupI].Count); // compose tree for the group { Datastructures.TreeNode coordinatesTreeNode; Datastructures.TreeNode centerTreeNode; centerTreeNode = new Datastructures.TreeNode(); centerTreeNode.value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.VECTOR2FLOAT); centerTreeNode.value.valueVector2Float = center; coordinatesTreeNode = new Datastructures.TreeNode(); coordinatesTreeNode.childNodes.Add(new Datastructures.TreeNode()); coordinatesTreeNode.childNodes.Add(new Datastructures.TreeNode()); coordinatesTreeNode.childNodes[0].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT); coordinatesTreeNode.childNodes[1].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT); coordinatesTreeNode.childNodes[0].value.valueInt = groupI % cachedGroupArraySize.x; coordinatesTreeNode.childNodes[1].value.valueInt = groupI / cachedGroupArraySize.x; outputElementTreeNode = new Datastructures.TreeNode(); outputElementTreeNode.childNodes.Add(pointsTreeNode); outputElementTreeNode.childNodes.Add(coordinatesTreeNode); outputElementTreeNode.childNodes.Add(centerTreeNode); } groupsTreeNode.childNodes.Add(outputElementTreeNode); } resultCode = GeneticProgramming.TypeRestrictedOperator.EnumResult.OK; }