/// <summary> /// Match the password against a single pattern /// </summary> /// <param name="graph">Adjacency graph for this key layout</param> /// <param name="password">Password to match</param> /// <returns>List of matching patterns</returns> private static List <Match> SpatialMatch(SpatialGraph graph, string password) { var matches = new List <Match>(); var i = 0; while (i < password.Length - 1) { int turns = 0, shiftedCount = 0; var lastDirection = -1; var j = i + 1; for (; j < password.Length; ++j) { var foundDirection = graph.GetAdjacentCharDirection(password[j - 1], password[j], out bool shifted); if (foundDirection != -1) { // Spatial match continues if (shifted) { shiftedCount++; } if (lastDirection != foundDirection) { turns++; lastDirection = foundDirection; } } else { break; // This character not a spatial match } } // Only consider runs of greater than two if (j - i > 2) { matches.Add(new SpatialMatch() { Pattern = SpatialPattern, i = i, j = j - 1, Token = password.Substring(i, j - i), Graph = graph.Name, Entropy = graph.CalculateEntropy(j - i, turns, shiftedCount), Turns = turns, ShiftedCount = shiftedCount }); } i = j; } return(matches); }
private static double CalculateAverageDegree(SpatialGraph graph) { var average = 0.0; foreach (var key in graph.AdjacencyGraph.Keys) { average += graph.AdjacencyGraph[key].Count(s => s != null); } average /= graph.AdjacencyGraph.Keys.Count; return(average); }
/// <summary> /// Match the password against a single pattern /// </summary> /// <param name="graph">Adjacency graph for this key layout</param> /// <param name="password">Password to match</param> /// <returns>List of matching patterns</returns> private List<Match> SpatialMatch(SpatialGraph graph, string password) { var matches = new List<Match>(); var i = 0; while (i < password.Length - 1) { int turns = 0, shiftedCount = 0; var lastDirection = -1; var j = i + 1; for (; j < password.Length; ++j) { bool shifted; var foundDirection = graph.GetAdjacentCharDirection(password[j - 1], password[j], out shifted); if (foundDirection != -1) { // Spatial match continues if (shifted) shiftedCount++; if (lastDirection != foundDirection) { turns++; lastDirection = foundDirection; } } else break; // This character not a spatial match } // Only consider runs of greater than two if (j - i > 2) { matches.Add(new SpatialMatch() { Pattern = SpatialPattern, i = i, j = j - 1, Token = password.Substring(i, j - i), Graph = graph.Name, Entropy = graph.CalculateEntropy(j - i, turns, shiftedCount), Turns = turns, ShiftedCount = shiftedCount }); } i = j; } return matches; }
protected override void SolveInstance(IGH_DataAccess DA) { // Since this component does not operate on the AWorld, we can grab it this way instead of creating a copy //AWorld wrld = new AWorld(); //if (!DA.GetData(0, ref wrld) || !wrld.IsValid) return; SpatialGraph gph = new SpatialGraph(); if (!DA.GetData(0, ref gph)) return; List<Line> lines = gph.EdgesToLines(); List<GH_Point> ghPoints = new List<GH_Point>(); foreach (Point3d pt in gph.nodes) ghPoints.Add(new GH_Point(pt)); DA.SetDataList(0, ghPoints); // Put this in another component //Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Number> valueTreeOut = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Number>(); //for (int g = 0; g < wrld.GenCount; g++){ // Grasshopper.Kernel.Data.GH_Path key_path = new Grasshopper.Kernel.Data.GH_Path(g); // List<Grasshopper.Kernel.Types.GH_Number> gh_vals = new List<Grasshopper.Kernel.Types.GH_Number>(); // foreach (double d in wrld.gens[g]) gh_vals.Add(new Grasshopper.Kernel.Types.GH_Number(d)); // valueTreeOut.AppendRange(gh_vals, key_path); //} //DA.SetDataTree(1, valueTreeOut); }
protected override void SolveInstance(IGH_DataAccess DA) { SpatialGraph gph = new SpatialGraph(); //if (!DA.GetData(0, ref gph) || !gph.IsValid) return; if (!DA.GetData(0, ref gph)) return; List<Line> lines = gph.EdgesToLines(); List<GH_Line> ghLines = new List<GH_Line>(); foreach (Line ln in lines) ghLines.Add(new GH_Line(ln)); DA.SetDataList(0, ghLines); }
protected override void SolveInstance(IGH_DataAccess DA) { SpatialGraph gph = new SpatialGraph(); int indx = 0; if (!DA.GetData(0, ref gph)) return; if (!DA.GetData(1, ref indx)) return; if (indx > gph.nodes.Count - 1) indx = 0; GH_Point ghPoint = new GH_Point(gph.nodes[indx]); int[] neighbors = gph.NeighboringIndexesOf(indx); double[] weights = gph.NeighboringWeightsOf(indx); List<Line> lines = gph.EdgesToLines(); List<GH_Line> ghLines = new List<GH_Line>(); foreach (int neighbor in neighbors) ghLines.Add(new GH_Line(new Line(gph.nodes[indx], gph.nodes[neighbor]))); DA.SetData(0, ghPoint); DA.SetDataList(1, neighbors); DA.SetDataList(2, ghLines); DA.SetDataList(3, weights); }
protected override void SolveInstance(IGH_DataAccess DA) { AWorld refwrld = new AWorld(); List<object> v_list = new List<object>(); GH_ObjectWrapper gh_dict = new GH_ObjectWrapper(); IDictionary<string, string> dict = new Dictionary<string,string>(); string k = ""; //GH_Dict test = GH_Dict.create("a", 1.0); //if (!DA.GetData(0, ref refwrld) || !refwrld.IsValid) return; //AWorld wrld = new AWorld(refwrld); SpatialGraph gph = new SpatialGraph(); if (!DA.GetData(0, ref gph)) return; int nGen = 0; string pyString = ""; if (!DA.GetData(1, ref pyString)) return; if (!DA.GetDataList(2, v_list)) return; if (!DA.GetData(3, ref nGen)) return; if (!DA.GetData(4, ref gh_dict)) return; if (!DA.GetData(5, ref k)) return; //dict = (IDictionary<string, string>)gh_dict.Value; //object d = gh_dict.Value; var t = Type.GetType("IronPython.Runtime.PythonDictionary,IronPython"); IDictionary i_dict = Activator.CreateInstance(t) as IDictionary; //IronPython.Runtime.PythonDictionary d = gh_dict.Value; //string v = d.get(k); string v = "oops"; // Sets the initial Generation by using the input v_list // if it runs out of values, it starts over (wraps) //object[] val_list = new object[gph.nodes.Count]; //int v_i = 0; //for (int i = 0; i < gph.nodes.Count; i++) //{ // if (v_i == v_list.Count) v_i = 0; // val_list[i] = v_list[v_i]; // v_i++; //} //AWorld wrld = new AWorld(gph, val_list); //_py = PythonScript.Create(); //_py.Output = this.m_py_output.Write; //_compiled_py = _py.Compile(pyString); //// console out Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String>(); //// Main evaluation cycle //// Should move code into the Antsworld Class //for (int g = 0; g < nGen; g++) //{ // // console out // this.m_py_output.Reset(); // double[] new_vals = new double[wrld.NodeCount]; // for (int i = 0; i < wrld.NodeCount; i++) // { // int[] neighboring_indices = wrld.gph.NeighboringIndexesOf(i); // // build list of neighboring values // List<double> neighboring_vals = new List<double>(); // for (int k = 0; k < neighboring_indices.Length; k++) neighboring_vals.Add(wrld.LatestGen[neighboring_indices[k]]); // double d = EvaluateCell(i, wrld.LatestGen[i], neighboring_vals); // //double d = g + i + 0.0; // new_vals[i] = d; // } // wrld.AddGen(new_vals); // // console out // Grasshopper.Kernel.Data.GH_Path key_path = new Grasshopper.Kernel.Data.GH_Path(g); // List<Grasshopper.Kernel.Types.GH_String> gh_strs = new List<Grasshopper.Kernel.Types.GH_String>(); // foreach (String str in this.m_py_output.Result) gh_strs.Add(new Grasshopper.Kernel.Types.GH_String(str)); // consoleOut.AppendRange(gh_strs, key_path); //} DA.SetDataTree(0, consoleOut); //DA.SetData(1, wrld); DA.SetData(2, v); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { bool test_bool = false; List<GH_ObjectWrapper> vals = new List<GH_ObjectWrapper>(); List<GH_ObjectWrapper> output = new List<GH_ObjectWrapper>(); GH_ObjectWrapper f = new GH_ObjectWrapper(); StringList m_py_output = new StringList(); // python output stream is piped to m_py_output Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String>(); GH_ObjectWrapper key_str = new GH_ObjectWrapper(); //if (!DA.GetData(0, ref key_str)) return; if (!DA.GetDataList(0, vals)) return; if (!DA.GetData(1, ref f)) return; var ret = key_str; Type t =ret.GetType(); ScriptEngine pyEngine = Python.CreateEngine(); //var outputStream = m_py_output.Write; var outputStream = new System.IO.MemoryStream(); var outputStreamWriter = new System.IO.StreamReader(outputStream); //pyEngine.Runtime.IO.SetOutput(outputStream, outputStreamWriter); //pyEngine.Runtime.IO.SetOutput(m_py_output.Write); //pyEngine. = m_py_output.Write; PythonScript _py = PythonScript.Create(); //_py.Output = m_py_output.Write; Rhino.RhinoApp.WriteLine("Testing, testong"); //var textWriter = new System.IO.TextWriter; //pyEngine.Runtime.IO.RedirectToConsole(); //Console.SetOut(TextWriter.Synchronized(textWriter)); //System.IO.Stream out_string = pyEngine.Runtime.IO.OutputStream; //pyEngine.Runtime.IO.RedirectToConsole(); //pyEngine.Runtime.IO.SetOutput //System.IO.TextWriter test = Console.Out; //Console.SetOut(test); //pyEngine. = this.m_py_output.Write; //_py = PythonScript.Create(); //pyEngine.Operations.Output = this.m_py_output.Write; for (int i = 0; i < vals.Count; i++) { //ret = vals[i]; Type the_type = vals[i].Value.GetType(); dynamic result = pyEngine.Operations.Invoke(f.Value, vals[i].Value); object return_object = (object)result; GH_ObjectWrapper temp = new GH_ObjectWrapper(return_object); var temp2 = temp.Value; output.Add(new GH_ObjectWrapper(return_object)); } //string out_str = return_object.GetType().ToString(); SpatialGraph gph = new SpatialGraph(); DA.SetData(0, test_bool); DA.SetDataList(1, output); //DA.SetData(2, return_object); //DA.SetData(3, out_str); }
protected override void SolveInstance(IGH_DataAccess DA) { AWorld refwrld = new AWorld(); List<double> v_list = new List<double>(); //GH_Dict test = GH_Dict.create("a", 1.0); //if (!DA.GetData(0, ref refwrld) || !refwrld.IsValid) return; //AWorld wrld = new AWorld(refwrld); SpatialGraph gph = new SpatialGraph(); if (!DA.GetData(0, ref gph)) return; int nGen = 0; string pyString = ""; if (!DA.GetData(1, ref pyString)) return; if (!DA.GetDataList(2, v_list)) return; if (!DA.GetData(3, ref nGen)) return; // Sets the initial Generation by using the input v_list // if it runs out of values, it starts over (wraps) double[] val_list = new double[gph.nodes.Count]; int v_i = 0; for (int i = 0; i < gph.nodes.Count; i++) { if (v_i == v_list.Count) v_i = 0; val_list[i] = v_list[v_i]; v_i++; } AWorld wrld = new AWorld(gph, val_list); _py = PythonScript.Create(); _py.Output = this.m_py_output.Write; _compiled_py = _py.Compile(pyString); // console out Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String>(); // Main evaluation cycle // Should move code into the Antsworld Class for (int g = 0; g < nGen; g++) { // console out this.m_py_output.Reset(); double[] new_vals = new double[wrld.NodeCount]; for (int i = 0; i < wrld.NodeCount; i++) { int[] neighboring_indices = wrld.gph.NeighboringIndexesOf(i); // build list of neighboring values List<double> neighboring_vals = new List<double>(); for (int k = 0; k < neighboring_indices.Length; k++) neighboring_vals.Add(wrld.LatestGen[neighboring_indices[k]]); double d = EvaluateCell(i, wrld.LatestGen[i], neighboring_vals); //double d = g + i + 0.0; new_vals[i] = d; } wrld.AddGen(new_vals); // console out Grasshopper.Kernel.Data.GH_Path key_path = new Grasshopper.Kernel.Data.GH_Path(g); List<Grasshopper.Kernel.Types.GH_String> gh_strs = new List<Grasshopper.Kernel.Types.GH_String>(); foreach (String str in this.m_py_output.Result) gh_strs.Add(new Grasshopper.Kernel.Types.GH_String(str)); consoleOut.AppendRange(gh_strs, key_path); } DA.SetDataTree(0, consoleOut); DA.SetData(1, wrld); }
protected override void SolveInstance(IGH_DataAccess DA) { AWorld refwrld = new AWorld(); bool SelectType = false; List<double> v_list = new List<double>(); Random rnd = new Random(); //if (!DA.GetData(0, ref refwrld) || !refwrld.IsValid) return; //AWorld wrld = new AWorld(refwrld); SpatialGraph gph = new SpatialGraph(); if (!DA.GetData(0, ref gph)) return; int nGen = 0; string pyString = ""; string spyString = ""; if (!DA.GetData(1, ref spyString)) return; if (!DA.GetData(2, ref SelectType)) return; if (!DA.GetData(3, ref pyString)) return; if (!DA.GetDataList(4, v_list)) return; if (!DA.GetData(5, ref nGen)) return; // Sets the initial Generation by using the input v_list // if it runs out of values, it starts over (wraps) double[] val_list = new double[gph.nodes.Count]; int v_i = 0; for (int i = 0; i < gph.nodes.Count; i++) { if (v_i == v_list.Count) v_i = 0; val_list[i] = v_list[v_i]; v_i++; } AWorld wrld = new AWorld(gph, val_list); // evaluation function _py = PythonScript.Create(); _py.Output = this.m_py_output.Write; _compiled_py = _py.Compile(pyString); // selection function _spy = PythonScript.Create(); _py.Output = this.m_py_output.Write; _compiled_spy = _py.Compile(spyString); // console out Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_String>(); // Main evaluation cycle // Should move code into the Antsworld Class for (int g = 0; g < nGen; g++) { // console out this.m_py_output.Reset(); double[] new_vals = new double[wrld.NodeCount]; // build list to test List<int> nodes_to_test = new List<int>(); for (int i = 0; i < wrld.NodeCount; i++) { // build this now since we will only change a few of them later new_vals[i] = wrld.LatestGen[i]; int[] neighboring_indices = wrld.gph.NeighboringIndexesOf(i); double[] n_wts = wrld.gph.NeighboringWeightsOf(i); // build list of neighboring values List<double> neighboring_vals = new List<double>(); for (int k = 0; k < neighboring_indices.Length; k++) neighboring_vals.Add(wrld.LatestGen[neighboring_indices[k]]); bool test = SelectCell(i, wrld.LatestGen[i], neighboring_vals, n_wts); if (test) nodes_to_test.Add(i); } if (SelectType) { int trial = rnd.Next(nodes_to_test.Count); int new_index = nodes_to_test[trial]; nodes_to_test[0] = new_index; nodes_to_test.RemoveRange(1, nodes_to_test.Count - 1); } // evaluate list of cells for (int j = 0; j < nodes_to_test.Count; j++) { int i = nodes_to_test[j]; int[] neighboring_indices = wrld.gph.NeighboringIndexesOf(i); // build list of neighboring values List<double> neighboring_vals = new List<double>(); for (int k = 0; k < neighboring_indices.Length; k++) neighboring_vals.Add(wrld.LatestGen[neighboring_indices[k]]); double d = EvaluateCell(i, wrld.LatestGen[i], neighboring_vals, wrld.gph.NeighboringWeightsOf(i)); //double d = g + i + 0.0; new_vals[i] = d; } wrld.AddGen(new_vals); // console out Grasshopper.Kernel.Data.GH_Path key_path = new Grasshopper.Kernel.Data.GH_Path(g); List<Grasshopper.Kernel.Types.GH_String> gh_strs = new List<Grasshopper.Kernel.Types.GH_String>(); foreach (String str in this.m_py_output.Result) gh_strs.Add(new Grasshopper.Kernel.Types.GH_String(str)); consoleOut.AppendRange(gh_strs, key_path); } DA.SetDataTree(0, consoleOut); DA.SetData(1, wrld); }
protected override void SolveInstance(IGH_DataAccess DA) { AWorld refwrld = new AWorld(); List <object> v_list = new List <object>(); GH_ObjectWrapper gh_dict = new GH_ObjectWrapper(); IDictionary <string, string> dict = new Dictionary <string, string>(); string k = ""; //GH_Dict test = GH_Dict.create("a", 1.0); //if (!DA.GetData(0, ref refwrld) || !refwrld.IsValid) return; //AWorld wrld = new AWorld(refwrld); SpatialGraph gph = new SpatialGraph(); if (!DA.GetData(0, ref gph)) { return; } int nGen = 0; string pyString = ""; if (!DA.GetData(1, ref pyString)) { return; } if (!DA.GetDataList(2, v_list)) { return; } if (!DA.GetData(3, ref nGen)) { return; } if (!DA.GetData(4, ref gh_dict)) { return; } if (!DA.GetData(5, ref k)) { return; } //dict = (IDictionary<string, string>)gh_dict.Value; //object d = gh_dict.Value; var t = Type.GetType("IronPython.Runtime.PythonDictionary,IronPython"); IDictionary i_dict = Activator.CreateInstance(t) as IDictionary; //IronPython.Runtime.PythonDictionary d = gh_dict.Value; //string v = d.get(k); string v = "oops"; // Sets the initial Generation by using the input v_list // if it runs out of values, it starts over (wraps) //object[] val_list = new object[gph.nodes.Count]; //int v_i = 0; //for (int i = 0; i < gph.nodes.Count; i++) //{ // if (v_i == v_list.Count) v_i = 0; // val_list[i] = v_list[v_i]; // v_i++; //} //AWorld wrld = new AWorld(gph, val_list); //_py = PythonScript.Create(); //_py.Output = this.m_py_output.Write; //_compiled_py = _py.Compile(pyString); //// console out Grasshopper.Kernel.Data.GH_Structure <Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure <Grasshopper.Kernel.Types.GH_String>(); //// Main evaluation cycle //// Should move code into the Antsworld Class //for (int g = 0; g < nGen; g++) //{ // // console out // this.m_py_output.Reset(); // double[] new_vals = new double[wrld.NodeCount]; // for (int i = 0; i < wrld.NodeCount; i++) // { // int[] neighboring_indices = wrld.gph.NeighboringIndexesOf(i); // // build list of neighboring values // List<double> neighboring_vals = new List<double>(); // for (int k = 0; k < neighboring_indices.Length; k++) neighboring_vals.Add(wrld.LatestGen[neighboring_indices[k]]); // double d = EvaluateCell(i, wrld.LatestGen[i], neighboring_vals); // //double d = g + i + 0.0; // new_vals[i] = d; // } // wrld.AddGen(new_vals); // // console out // Grasshopper.Kernel.Data.GH_Path key_path = new Grasshopper.Kernel.Data.GH_Path(g); // List<Grasshopper.Kernel.Types.GH_String> gh_strs = new List<Grasshopper.Kernel.Types.GH_String>(); // foreach (String str in this.m_py_output.Result) gh_strs.Add(new Grasshopper.Kernel.Types.GH_String(str)); // consoleOut.AppendRange(gh_strs, key_path); //} DA.SetDataTree(0, consoleOut); //DA.SetData(1, wrld); DA.SetData(2, v); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { bool test_bool = false; List <GH_ObjectWrapper> vals = new List <GH_ObjectWrapper>(); List <GH_ObjectWrapper> output = new List <GH_ObjectWrapper>(); GH_ObjectWrapper f = new GH_ObjectWrapper(); StringList m_py_output = new StringList(); // python output stream is piped to m_py_output Grasshopper.Kernel.Data.GH_Structure <Grasshopper.Kernel.Types.GH_String> consoleOut = new Grasshopper.Kernel.Data.GH_Structure <Grasshopper.Kernel.Types.GH_String>(); GH_ObjectWrapper key_str = new GH_ObjectWrapper(); //if (!DA.GetData(0, ref key_str)) return; if (!DA.GetDataList(0, vals)) { return; } if (!DA.GetData(1, ref f)) { return; } var ret = key_str; Type t = ret.GetType(); ScriptEngine pyEngine = Python.CreateEngine(); //var outputStream = m_py_output.Write; var outputStream = new System.IO.MemoryStream(); var outputStreamWriter = new System.IO.StreamReader(outputStream); //pyEngine.Runtime.IO.SetOutput(outputStream, outputStreamWriter); //pyEngine.Runtime.IO.SetOutput(m_py_output.Write); //pyEngine. = m_py_output.Write; PythonScript _py = PythonScript.Create(); //_py.Output = m_py_output.Write; Rhino.RhinoApp.WriteLine("Testing, testong"); //var textWriter = new System.IO.TextWriter; //pyEngine.Runtime.IO.RedirectToConsole(); //Console.SetOut(TextWriter.Synchronized(textWriter)); //System.IO.Stream out_string = pyEngine.Runtime.IO.OutputStream; //pyEngine.Runtime.IO.RedirectToConsole(); //pyEngine.Runtime.IO.SetOutput //System.IO.TextWriter test = Console.Out; //Console.SetOut(test); //pyEngine. = this.m_py_output.Write; //_py = PythonScript.Create(); //pyEngine.Operations.Output = this.m_py_output.Write; for (int i = 0; i < vals.Count; i++) { //ret = vals[i]; Type the_type = vals[i].Value.GetType(); dynamic result = pyEngine.Operations.Invoke(f.Value, vals[i].Value); object return_object = (object)result; GH_ObjectWrapper temp = new GH_ObjectWrapper(return_object); var temp2 = temp.Value; output.Add(new GH_ObjectWrapper(return_object)); } //string out_str = return_object.GetType().ToString(); SpatialGraph gph = new SpatialGraph(); DA.SetData(0, test_bool); DA.SetDataList(1, output); //DA.SetData(2, return_object); //DA.SetData(3, out_str); }