/// <summary> /// Gets the current sliders in Input[0] /// </summary> public bool GetSliders(List <GH_NumberSlider> sliders, List <GalapagosGeneListObject> genePools) { bool hasData = false; lock (syncLock) { // synchronize foreach (IGH_Param param in this.Params.Input[0].Sources) { Grasshopper.Kernel.Special.GH_NumberSlider slider = param as Grasshopper.Kernel.Special.GH_NumberSlider; if (slider != null) { sliders.Add(slider); hasData = true; } GalapagosGeneListObject genepool = param as GalapagosGeneListObject; if (genepool != null) { genePools.Add(genepool); hasData = true; } } } cSliders = sliders; cGenePools = genePools; return(hasData); }
//Get Variables public bool SetInputs() { Sliders = new List <GH_NumberSlider>(); _genepools = new List <GalapagosGeneListObject>(); var s = new GH_NumberSlider(); var g = new GalapagosGeneListObject(); foreach (var source in OptimizationComponent.Params.Input[0].Sources) { var guid = source.InstanceGuid; _inputGuids.Add(guid); } if (_inputGuids.Count == 0) { MessageBox.Show("FrOG needs at least one variable input (defined as a number slider or gene pool, of type integer or float", "FrOG Error"); return(false); } foreach (var guid in _inputGuids) { var input = _doc.FindObject(guid, true); if (input == null) { MessageBox.Show( "The variables connected to FrOG are inconsistent. This error typically occurs after removing one or more connected number sliders. Please consider deleting and setting up all variables connections again.", "FrOG Error"); return(false); } if (input.ComponentGuid == s.ComponentGuid) { var slider = (GH_NumberSlider)input; Sliders.Add(slider); } if (input.ComponentGuid == g.ComponentGuid) { var genepool = (GalapagosGeneListObject)input; _genepools.Add(genepool); } } SetVariables(); return(true); }
/// <summary> /// Grasshopper solveinstance /// </summary> /// <param name="DA"></param> protected override void SolveInstance(IGH_DataAccess DA) { // Get Solution and data BiomorpherGoo temp = new BiomorpherGoo(); if (!DA.GetData("Solution", ref temp)) { return; } solutionData = temp.Value; if (!DA.GetData <int>("Branch", ref branch)) { return; } ; if (!DA.GetData <int>("Generation", ref generation)) { return; } ; if (!DA.GetData <int>("Design", ref design)) { return; } ; // Check to see if we have anything at all (population is the lowest possible thing in the hierarchy) if (solutionData.historicData == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Empty biomorpher solution!"); return; } // If we have then display the number of designs in a typical population else { Message = "Population size = " + solutionData.PopCount; } // Only if things have changed do we actually want to change the sliders and expire the solution if (branch != localBranch || localGeneration != generation || localDesign != design || !localSolutionData.Equals(solutionData)) { localBranch = branch; localGeneration = generation; localDesign = design; localSolutionData = solutionData; active = true; } else { active = false; } // Only do things if there is a design at the location if (solutionData.historicData.get_Branch(new GH_Path(branch, generation, design)) != null) { if (active) { //We need a list of genes for the selected design List <double> genes = new List <double>(); // g for (int i = 0; i < solutionData.historicData.get_Branch(new GH_Path(branch, generation, design)).Count; i++) { double myDouble; GH_Convert.ToDouble(solutionData.historicData.get_Branch(new GH_Path(branch, generation, design))[i], out myDouble, GH_Conversion.Primary); genes.Add(myDouble); } // Set up some local sliders and genepools List <GH_NumberSlider> theSliders = new List <GH_NumberSlider>(); List <GalapagosGeneListObject> theGenePools = new List <GalapagosGeneListObject>(); bool flag = false; // Note that the sliders and genepools are stored in two branches of a GH_Structure try { // Get sliders List <GH_Guid> sliderList = new List <GH_Guid>(); foreach (GH_Guid x in solutionData.genoGuids.get_Branch(0)) { GH_NumberSlider slidy = OnPingDocument().FindObject <GH_NumberSlider>(x.Value, true); if (slidy != null) { theSliders.Add(slidy); } } // Get genepools foreach (GH_Guid x in solutionData.genoGuids.get_Branch(1)) { GalapagosGeneListObject pooly = OnPingDocument().FindObject <GalapagosGeneListObject>(x.Value, true); if (pooly != null) { theGenePools.Add(pooly); } } } catch { flag = true; } if (flag) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Getting sliders and/or genepools from the canvas was unsuccesful. Have they been modified?"); return; } canvas.Document.Enabled = false; //this.Locked = true; SetSliders(genes, theSliders, theGenePools); canvas.Document.Enabled = true; canvas.Document.ExpireSolution(); } } else { // Turn the thing back on without setting all the sliders etc. AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "No historic design found at this reference"); } }
/// <summary> /// Grasshopper solveinstance /// </summary> /// <param name="DA"></param> protected override void SolveInstance(IGH_DataAccess DA) { List <string> genoGuids = new List <string>(); GH_Structure <GH_Number> populationData; if (!DA.GetDataList <string>("GenoGuids", genoGuids)) { return; } if (!DA.GetDataTree("Population", out populationData)) { if (populationData == null) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "No population data!"); return; } else { Message = "Popcount = " + populationData.Branches.Count; } } if (!DA.GetData <GH_Integer>("DesignID", ref designID)) { return; } ; // Horrible workaround for genepools. I'm completely lost finding a better way to be honest. // Maybe only trigger this bit IF the input data has changed in some way? That might solve the Embryo problem. if (isActive) { //GH_Path List <double> genes = new List <double>(); for (int i = 0; i < populationData.get_Branch(i).Count; i++) { double myDouble; GH_Convert.ToDouble(populationData.get_Branch(designID.Value)[i], out myDouble, GH_Conversion.Primary); genes.Add(myDouble); } List <GH_NumberSlider> theSliders = new List <GH_NumberSlider>(); List <GalapagosGeneListObject> theGenePools = new List <GalapagosGeneListObject>(); bool flag = false; int counter = 0; foreach (string myGuid in genoGuids) { try { System.Guid me = new Guid(myGuid); // Try for a slider GH_NumberSlider slidy = OnPingDocument().FindObject <GH_NumberSlider>(me, true); if (slidy != null) { theSliders.Add(slidy); counter++; } // Try for a genepool GalapagosGeneListObject pooly = OnPingDocument().FindObject <GalapagosGeneListObject>(me, true); if (pooly != null) { theGenePools.Add(pooly); counter += pooly.Count; } } catch { flag = true; } } if (flag) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No slider or genepool found at specified guid. Has the definition been altered?"); return; } // Catch an unequal amount of sliders and genes/guids if (genes.Count != counter) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Guid count does not equal chromosome gene count"); return; } canvas.Document.Enabled = false; SetSliders(genes, theSliders, theGenePools); canvas.Document.Enabled = true; if (theGenePools.Count > 0) { isActive = false; } } else { // Turn the thing back on without setting all the sliders etc. isActive = true; } }
//Genepool Helper Functions private static decimal GetNormalizedGenepoolValue(decimal unnormalized, GalapagosGeneListObject genepool) { return((unnormalized - genepool.Minimum) / (genepool.Maximum - genepool.Minimum)); }
/// <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) { GH_Structure <GH_Number> X_list = new GH_Structure <GH_Number>(); GH_Structure <GH_Number> F_Tree = new GH_Structure <GH_Number>(); GH_Structure <GH_Number> G_Tree = new GH_Structure <GH_Number>(); //if (!DA.GetDataTree<GH_Number>(0, out X_list)) return; if (!DA.GetDataTree <GH_Number>(1, out F_Tree)) { return; } //List<GH_NumberSlider> sliders = new List<GH_NumberSlider>(); Vars.sliders = new List <GH_NumberSlider>(); Vars.genepools = new List <GalapagosGeneListObject>(); foreach (IGH_Param param in Params.Input[0].Sources) { GH_NumberSlider slider = param as GH_NumberSlider; if (slider != null) { Vars.sliders.Add(slider); } else { GalapagosGeneListObject genepool = param as GalapagosGeneListObject; if (genepool != null) { Vars.genepools.Add(genepool); } } } if (Vars.sliders.Count == 0 && Vars.genepools.Count == 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Connect sliders to the variables (x) input."); return; } if (F_Tree.DataCount == 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Supply a fitness value to minimise/maximise."); return; } double F_x = new double(); if (F_Tree.DataCount > 1) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Only one fitness value should be supplied."); return; } try { if (F_Tree.get_FirstItem(true).CastTo <double>(out F_x)) { if (DA.GetDataTree <GH_Number>(2, out G_Tree)) { if (G_Tree.DataCount > 0) { List <GH_Number> G_List = G_Tree.FlattenData(); List <double> g = new List <double>(); for (int i = 0; i < G_List.Count; i++) { double g_temp = 0; G_List[i].CastTo <double>(out g_temp); g.Add(g_temp); } double G_x = new double(); double max = 0; double f = 0; for (int i = 0; i < g.Count; i++) { if (g[i] > max) { max = g[i]; } } if (max <= 1) { G_x = max; } else { double rho = 1 / (max - 1); for (int i = 0; i < g.Count; i++) { f = f + Math.Exp(rho * g[i]); } f = Math.Log(f) / rho; G_x = f; } Vars.g = G_x; Message = "g = " + String.Format("{0:0.000}", G_x); } else { Vars.g = 0; } } } } catch { Vars.f = double.PositiveInfinity; Vars.g = 0; return; } Vars.f = F_x; }