public void TestValidAutomataSingleNodeIsTrue() { BooleanNetworkValidator validator = new BooleanNetworkValidator(); var automata = new GeneNode() { NodeName = "a", }; var booleanNetwork = new List <GeneLink>() { new GeneLink() { From = "A", To = "B", IsPositive = true }, new GeneLink() { From = "A", To = "C", IsPositive = true } } ; var res = validator.IsValidAutomata(automata, null, booleanNetwork); Assert.IsTrue(res); }
public bool IsValidAutomata(GeneNode automata, Condition currentStatus, List <GeneLink> booleanNetwok, HashSet <GeneNode> alreayViewedNodes = null) { if (automata == null || automata.Transitions == null || !automata.Transitions.Any()) { return(true); } if (alreayViewedNodes == null) { alreayViewedNodes = new HashSet <GeneNode>(); } // handle case of loop in first node if (currentStatus != null) { alreayViewedNodes.Add(automata); } var isValid = automata.Transitions.All(t => IsValidTransition(currentStatus, t.Condition, booleanNetwok) && (alreayViewedNodes.Contains(t.Node) || IsValidAutomata(t.Node, t.Condition, booleanNetwok, alreayViewedNodes))); return(isValid); }
public List <GeneNode> GetValidMerges(GeneNode automata1, GeneNode automata2, List <GeneLink> booleanNetwok) { var possibleMerges = GetMerges(automata1, automata2); logger.Info($"All merge (include invalids) is {possibleMerges.Count}"); var validMerges = new List <GeneNode>(); possibleMerges.ForEach(m => { // firstly check simple logic, and only after check with CUDD, because it's expensive if (validator.IsValidAutomata(m, null, booleanNetwok) && IsBddValid(m, booleanNetwok)) { logger.Info($"Merge for {m.NodeName} is valid"); validMerges.Add(m); } else { logger.Info($"Merge for {m.Path()} is not valid"); } }); logger.Info($"Valid merges are {validMerges.Count}"); return(validMerges); }
private static void InitInitialState(GeneNode automata, AutomataBDD systemBDD, List <string> letters) { HashSet <string> handledLetters = new HashSet <string>(); if (automata == null) { return; } int i = 0; //// init is only based on first condition //// that's why here their not "visit" //automata.CurrentCondition.ToList() // .Where(f => f.Value.HasValue).ToList().ForEach(f => // { // Expression value = Value(f); // var formatParameter = Formater.FormatParameter(f.Key, i); // handledLetters.Add(formatParameter); // var primitiveApplication = BddHelper.SetBooleanValue(i, f.Value.Value, f.Key); // systemBDD.initExpression = // new PrimitiveApplication(PrimitiveApplication.AND, // systemBDD.initExpression, // primitiveApplication); // }); //i++; logger.Info("init: " + systemBDD.initExpression); }
// PUBLIC FUNCTIONS public void addNewNodeToLayer(int layer) { // layer must not be input or output layers int layerBefore = layer - 1; int layerAfter = layer + 1; int nodePlace = nodes[layer].Count; GeneNode node = new GeneNode(); nodes[layer].Add(node); // fully connect node // layer before for (int i = 0; i < nodes[layerBefore].Count; i++) { int in_layer = layerBefore; int in_place = i; int out_layer = layer; int out_place = nodePlace; links[layerBefore].Add(new GeneLink(in_layer, in_place, out_layer, out_place, this.initialWeight)); } // layer after for (int i = 0; i < nodes[layerAfter].Count; i++) { int in_layer = layer; int in_place = nodePlace; int out_layer = layerAfter; int out_place = i; links[layer].Add(new GeneLink(in_layer, in_place, out_layer, out_place, this.initialWeight)); } }
private void feedForwardFrom(int fromLayer) { for (int i = 0; i < this.links[fromLayer].Count; i++) { GeneLink link = this.links[fromLayer][i]; int in_layer = link.in_layer; int in_place = link.in_place; int out_layer = link.out_layer; int out_place = link.out_place; GeneNode inNode = this.nodes[in_layer][in_place]; GeneNode outNode = this.nodes[out_layer][out_place]; outNode.value += (inNode.value * link.weight); //inNode.value = 0; // So it doesnt retain value from the last iteration; cause of the activationFunc(0) = 0.5 -> activationFunc(0.5) = 0.62.. -> activationFunc(0.62..) } // run though all nodes and reset value to 0 foreach (GeneNode node in this.nodes[fromLayer]) { node.value = 0; } }
public List <GeneNode> GetMerges(GeneNode automata1, GeneNode automata2) { // check positive and negative merge algorithm return(GetMerges(automata1, automata2, true) //.Union(GetMerges(automata1, automata2, false)) .ToList()); }
private GeneNode ApplyMergeToAllChildrens(GeneNode tt1, GeneNode lastNode, GeneNode t1Head) { // direction is from last node to t1 var t1 = tt1; var t2 = lastNode; while (t1 != null && t2 != null && t1 != lastNode) { var newCondition = CreateMergedCondition(t1.CurrentCondition, t2.CurrentCondition, true); if (newCondition == null || AlreadyMergedNodes(t1, t2)) { return(null); } //t1.Transitions.First().Condition = newCondition; var mergeName = $"{t1.NodeName} ^ {t2.NodeName}"; t1.NodeName = mergeName; t1.CurrentCondition = newCondition; t2.CurrentCondition = newCondition; t1 = t1.Transitions?.First()?.Node; t2 = t2.Transitions?.First()?.Node; } return(t1Head); }
/* * private ArrayList<GeneNode> getNodesOfType(int type){ * ArrayList<GeneNode> nodesOfType = new ArrayList<GeneNode>(); * for (GeneNode node : this.nodes){ * if (node.type == type){ * nodesOfType.add(node); * } * } * * return nodesOfType; * } */ public string toString() { string str = ""; for (int i = 0; i < nrLayers; i++) { for (int j = 0; j < this.nodes[i].Count; j++) { GeneNode node = this.nodes[i][j]; str += "(" + node.value + " : " + node.bias + ") "; } str += "\n"; if (i < nrLayers - 1) { for (int j = 0; j < this.links[i].Count; j++) { GeneLink link = this.links[i][j]; str += "(" + link.in_layer + "," + link.in_place + ")=" + link.weight + "=(" + link.out_layer + "," + link.out_place + ") "; } } str += "\n"; } return(str); }
public Genome copy() { /* * int nrInput = this.nodes[0].Count; * int nrOutput = this.nodes[this.nrLayers - 1].Count; * int[] nrHiddenNodes = new int[this.nrHiddenLayers]; */ int[] layers = new int[nrLayers]; for (int i = 0; i < this.nodes.Count; i++) { layers[i] = this.nodes[i].Count; } /* * for (int i = 0; i < this.nrHiddenLayers; i++) * { * nrHiddenNodes[i] = this.nodes[i + 1].Count; * } */ //Genome copy = new Genome(nrInput, nrHiddenNodes, nrOutput); Genome copy = new Genome(layers); // copy all nodes for (int i = 0; i < this.nrLayers; i++) { for (int j = 0; j < this.nodes[i].Count; j++) { GeneNode nodeCopy = copy.nodes[i][j]; GeneNode nodeOriginal = this.nodes[i][j]; nodeCopy.bias = nodeOriginal.bias; nodeCopy.id = nodeOriginal.id; // value should always be zero. it is only used for calculating the feed through it //nodeCopy.value = nodeOriginal.value; } } // copy all links for (int i = 0; i < this.nrLayers - 1; i++) { for (int j = 0; j < this.links[i].Count; j++) { GeneLink linkCopy = copy.links[i][j]; GeneLink linkOriginal = this.links[i][j]; linkCopy.in_layer = linkOriginal.in_layer; linkCopy.in_place = linkOriginal.in_place; linkCopy.out_layer = linkOriginal.out_layer; linkCopy.out_place = linkOriginal.out_place; linkCopy.weight = linkOriginal.weight; } } return(copy); }
private bool AlreadyMerged(GeneNode first, GeneNode second) { return (!string.IsNullOrEmpty(first.MergeName) && !string.IsNullOrWhiteSpace(second.MergeName) && first.GetAllMergedExperiment().Intersect(second.GetAllMergedExperiment()).Any()); }
public GeneSeq Deserialize(string stream) { id = null; score = -1; string[] lines = stream.Split('\n'); for (int i = 0; i < lines.Length; i++) { string line = lines [i]; if (line.Trim() == "") { continue; } if (line.Trim() == ";") { break; } string[] elems = line.Split(' '); if (id == null) { id = elems [0]; score = Double.Parse(elems [1]); continue; } int[] pos = new int[2]; pos [0] = Int32.Parse(elems [0]); pos [1] = Int32.Parse(elems [1]); GeneNode node = new GeneNode(pos, Int32.Parse(elems [2])); seqList.Add(node); } return(this); }
public void IsLoopFixedPointSimpleNegativeTest() { var node1 = new GeneNode() { CurrentCondition = new Condition() { { "A", true }, { "B", false } }, Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { CurrentCondition = new Condition() { { "A", true }, { "B", true } } } } } }; Assert.IsFalse(node1.IsLoopFixedPoint()); }
public static Dictionary <string, bool> CreateDictBasedOnAutomata(GeneNode automata) { Dictionary <string, bool> result = new Dictionary <string, bool>(); if (automata == null || automata.Transitions == null || !automata.Transitions.Any()) { return(null); } int i = 0; automata.Visit(l => { var tr = BDDSolver.GetTransitions(l); if (tr == null) { return; } tr .ForEach( f => { var key = Formater.FormatParameter(f.Key, i); var value = f.Value.Value; result[key] = value; }); i++; }); return(result); }
public GeneNode CreateMerge(GeneNode node1, GeneNode node2, GeneNode node2Head, bool usePositiveAlgo) { var cloned = CloneHelper.Clone(node1); var t1 = cloned; GeneNode lastT1 = null; var t2 = node2; string mergeName = string.Empty; while (t1 != null && t2 != null) { var newCondition = CreateMergedCondition(t1.CurrentCondition, t2.CurrentCondition, usePositiveAlgo); if (newCondition == null) { return(null); } //t1.Transitions.First().Condition = newCondition; var prefix = usePositiveAlgo ? string.Empty : "!"; mergeName = $"{t1.NodeName} ~ {prefix}{t2.NodeName}"; t1.NodeName = mergeName; t1.CurrentCondition = newCondition; lastT1 = t1; t1 = t1.Transitions?.First()?.Node; t2 = t2.Transitions?.First()?.Node; } // suffix if (t2?.Transitions != null && t2.Transitions.Any()) { lastT1.Transitions = t2.Transitions; } // prefix if (node2 != node2Head) // node2 is not the first node in node2 automata, so we have // to add parents of node2 { var cloned2 = CloneHelper.Clone(node2Head); var temp = cloned2; // find new head while (temp?.Transitions.First().Node.NodeName != node2.NodeName) { temp = temp.Transitions.First().Node; } temp.Transitions.First().Node = cloned; cloned = cloned2; } if (cloned != null) { cloned.MergeName = mergeName; } return(cloned); }
public void TestSCaseWithMultipleParametersAddSomeParamsMissingInSomeNodes() { var solver = NinjectHelper.Get <IBDDSolver>(); var automata = new GeneNode() { CurrentCondition = new Condition() { { "a", true }, { "b", false }, { "c", true } }, NodeName = "n0", Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { // not C here CurrentCondition = new Condition() { { "a", false }, { "b", true } }, NodeName = "n1", Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { NodeName = "n2", CurrentCondition = new Condition() { { "a", true }, { "b", false }, { "c", true } } } } } } } } }; // C not exist here!! var booleanNetwork = new List <GeneLink>() { new GeneLink() { From = "a", To = "a", IsPositive = false }, new GeneLink() { From = "b", To = "b", IsPositive = false }, }; var res = solver.IsValidPath(automata, booleanNetwork); Assert.IsTrue(res); }
public void TestSCaseWithThreeParametersAndThreeRulesBDDSolver() { var solver = NinjectHelper.Get <IBDDSolver>(); var automata = new GeneNode() { CurrentCondition = new Condition() { { "a", true }, { "b", false }, { "c", true } }, NodeName = "n0", Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { CurrentCondition = new Condition() { { "a", false }, { "b", true }, { "c", true } }, NodeName = "n1", //Transitions = new List<GeneTransition>() //{ // new GeneTransition() // { // Node = new GeneNode() // { // NodeName = "n2", // CurrentCondition = new Condition() { { "a", true }, { "b", false }, {"c", false} } // } // } //} } } } }; var booleanNetwork = new List <GeneLink>() { new GeneLink() { From = "a", To = "a", IsPositive = false }, new GeneLink() { From = "b", To = "b", IsPositive = false }, new GeneLink() { From = "a", To = "c", IsPositive = true }, }; var res = solver.IsValidPath(automata, booleanNetwork); Assert.IsTrue(res); }
public void InsertNodeAfterIndex(int index, GeneNode node) { if (index >= Size() || index < 0) { return; } this.seqList.Insert(Size() - index - 1, node); }
public GeneConnection(int Innovation, GeneNode Input, GeneNode Output, float Weight, bool Enabled) : this() { this.Innovation = Innovation; this.Input = Input; this.Output = Output; this.Weight = Weight; this.Enabled = Enabled; }
private static bool AlreadyMergedNodes(GeneNode t1, GeneNode t2) { var firstMerged = t1.NodeName.Split('^'); var secondMerged = t2.NodeName.Split('^'); return(firstMerged.Intersect(secondMerged).Any()); }
public void TestReturnEmptyInCaseOfnull() { var merged = new GeneNode() { MergeName = null }.GetAllMergedExperiment(); Assert.AreEqual(null, merged); }
public void TestMergeWithNullConditionReturnValid() { var automata1 = new GeneNode() { NodeName = "a1", CurrentCondition = new Condition() { { "B", true }, { "C", true } }, // here A is missing Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { NodeName = "b1", CurrentCondition = new Condition() { { "A", false }, { "B", false }, { "C", false } }, } } } }; var automata2 = new GeneNode() { NodeName = "a2", CurrentCondition = new Condition() { { "A", true }, { "B", true } }, // Here C is missing Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { NodeName = "b2", CurrentCondition = new Condition() { { "A", false }, { "B", false }, { "C", false } }, } } } }; var mergeLogic = new AutomataMergeLogic(); var res = mergeLogic.GetMerges(automata1, automata2); Assert.AreEqual(1, res.Count); Assert.AreEqual(res.First().CurrentCondition["A"], true); Assert.AreEqual(res.First().CurrentCondition["B"], true); Assert.AreEqual(res.First().CurrentCondition["C"], true); }
public void TestReturnExperimentNameSingleCase() { var merged = new GeneNode() { MergeName = "Experiment_0" }.GetAllMergedExperiment(); Assert.AreEqual("Experiment", merged.First()); }
private void computeValuesFromLayer(int fromLayer) { for (int i = 0; i < this.nodes[fromLayer].Count; i++) { GeneNode node = this.nodes[fromLayer][i]; //Debug.Log ("value: " + node.value + " bias: " + node.bias); node.value = activationFunc(node.bias + node.value); } }
public void TestValidAutomataWithLoop() { BooleanNetworkValidator validator = new BooleanNetworkValidator(); var automata = new GeneNode() { NodeName = "a", Transitions = new List <GeneTransition>() { new GeneTransition() { Condition = new Condition() { { "A", true }, { "B", true }, { "C", true } }, Node = new GeneNode() { NodeName = "b", Transitions = new List <GeneTransition>() { new GeneTransition() { Condition = new Condition() { { "A", true }, { "B", true }, { "C", true } }, Node = null } } } } } }; // set the loop automata.Transitions.First().Node = automata; var booleanNetwork = new List <GeneLink>() { new GeneLink() { From = "A", To = "B", IsPositive = true }, new GeneLink() { From = "A", To = "C", IsPositive = true } } ; var res = validator.IsValidAutomata(automata, null, booleanNetwork); Assert.IsTrue(res); }
public void TestReturnExperimentNameDistinct() { var merged = new GeneNode() { MergeName = "Experiment_0 ~ Experiment_2" }.GetAllMergedExperiment(); Assert.AreEqual(1, merged.Count); Assert.AreEqual("Experiment", merged.First()); }
private static void LogLoop(GeneNode node, GeneNode current) { var builder = new StringBuilder(); if (current != null) { current.AppendPath(builder); } logger.Info($"Get value of loop for {node.NodeName}, value is {builder}"); }
public void TestValidAutomataSimplePathIsTrue() { BooleanNetworkValidator validator = new BooleanNetworkValidator(); var automata = new GeneNode() { NodeName = "a", Transitions = new List <GeneTransition>() { new GeneTransition() { Condition = new Condition() { { "A", true }, { "B", false }, { "C", false } }, Node = new GeneNode() { NodeName = "b", Transitions = new List <GeneTransition>() { new GeneTransition() { Condition = new Condition() { { "A", true }, { "B", true }, { "C", true } }, Node = new GeneNode() { NodeName = "final" } } } } } } }; var booleanNetwork = new List <GeneLink>() { new GeneLink() { From = "A", To = "B", IsPositive = true }, new GeneLink() { From = "A", To = "C", IsPositive = true } } ; var res = validator.IsValidAutomata(automata, null, booleanNetwork); Assert.IsTrue(res); }
public void TestMergeWithSameConditionReturnMerged() { var automata1 = new GeneNode() { NodeName = "a1", CurrentCondition = new Condition() { { "A", true }, { "B", true }, { "C", true } }, Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { NodeName = "b1", CurrentCondition = new Condition() { { "A", false }, { "B", false }, { "C", false } }, } } } }; var automata2 = new GeneNode() { NodeName = "a2", CurrentCondition = new Condition() { { "A", true }, { "B", true }, { "C", true } }, Transitions = new List <GeneTransition>() { new GeneTransition() { Node = new GeneNode() { NodeName = "b2", CurrentCondition = new Condition() { { "A", false }, { "B", false }, { "C", false } }, } } } }; var mergeLogic = new AutomataMergeLogic(); var res = mergeLogic.GetMerges(automata1, automata2); Assert.AreEqual(1, res.Count); }
public GeneNode CreateValidLoopMerge(GeneNode node, List <GeneLink> booleanNetwok) { var cloned = CloneHelper.Clone(node); var t1 = cloned; var lastNode = FindLastNode(cloned); if (lastNode == null) { return(null); } while (t1 != null && t1 != lastNode) { var newCondition = CreateMergedCondition(lastNode.CurrentCondition, t1.CurrentCondition, true); if (newCondition != null) { var currentNode = CloneHelper.Clone(cloned); var tt1 = t1; var tlastNode = lastNode; t1 = currentNode.Find(t1); lastNode = currentNode.Find(lastNode); // now we can work on cloned // var mergeName = $"{t1.NodeName} ^ {lastNode.NodeName}"; // t1.NodeName = mergeName; //// both sides take new condition //t1.CurrentCondition = newCondition; //lastNode.CurrentCondition = newCondition; cloned = ApplyMergeToAllChildrens(tt1, tlastNode, cloned); if (cloned != null && IsBddValid(cloned, booleanNetwok)) { // set last node to "Looped" to allow more loops with other tlastNode.IsInLoop = true; cloned.MergeName = cloned.NodeName; return(cloned); } cloned = currentNode; } t1 = t1.Transitions.First()?.Node; } return(null); }