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); }