public GrasshopperInOut(OptimizationComponent component) { OptimizationComponent = component; ComponentFolder = Path.GetDirectoryName(Grasshopper.Instances.ComponentServer.FindAssemblyByObject(OptimizationComponent).Location); _doc = OptimizationComponent.OnPingDocument(); _inputGuids = new List <Guid>(); }
public OptimizationWindow(OptimizationComponent component) { //Refers to the FrOG Window: Do not rename!!! InitializeComponent(); //Referes to the FrOG component on the Grasshopper canvas _frogComponent = component; //Fill and set Combobox foreach (var preset in SolverList.PresetNames) { comboBoxPresets.Items.Add(preset); } comboBoxPresets.SelectedIndex = 0; //Log Default File Name textBoxLogName.Text = String.Format("{0}_log", DateTime.Now.ToString("yyMMdd")); //Hide Save State Tab //Tabs.TabPages.Remove(Tabs.TabPages[3]); //Disable Chart Axis bestValueChart.ChartAreas[0].AxisX.Enabled = AxisEnabled.False; bestValueChart.ChartAreas[0].AxisX2.Enabled = AxisEnabled.False; bestValueChart.ChartAreas[0].AxisY.Enabled = AxisEnabled.False; bestValueChart.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False; //Setting Border for the Chart Area bestValueChart.ChartAreas[0].BorderWidth = 0; //Lock Stop Button buttonStop.Enabled = false; //Hide Labels labelIteration.Visible = false; labelBestValue.Visible = false; //Initilize Backgroundworker //http://www.codeproject.com/Articles/634146/Background-Thread-Let-me-count-the-ways backgroundWorkerSolver.DoWork += OptimizationLoop.RunOptimizationLoopMultiple; backgroundWorkerSolver.ProgressChanged += ProgressChangedHandler; backgroundWorkerSolver.RunWorkerCompleted += ReleaseButtons; backgroundWorkerSolver.WorkerReportsProgress = true; backgroundWorkerSolver.WorkerSupportsCancellation = true; }
//Run MultipleOptimizationRuns(Entry point: Run RunOptimizationLoop (more than) once) public static void RunOptimizationLoopMultiple(object sender, DoWorkEventArgs e) { var logBaseName = LogName; var bestResult = new OptimizationResult(BolMaximize ? double.NegativeInfinity : double.PositiveInfinity, new List <decimal>(), 0, OptimizationResult.ResultType.Unknown); //Get worker and component _worker = sender as BackgroundWorker; _component = (OptimizationComponent)e.Argument; if (_component == null) { MessageBox.Show("FrOG Component not set to an object", "FrOG Error"); return; } //Setup Variables _component.GhInOut_Instantiate(); if (!_component.GhInOut.SetInputs() || !_component.GhInOut.SetOutput()) { MessageBox.Show("Getting Variables and/or Objective failed", "Opossum Error"); return; } //MessageBox.Show(_component.GhInOut.VariablesStr, "FrOG Variables"); //Main Loop var finishedRuns = 0; while (finishedRuns < Runs) { //MessageBox.Show(finishedRuns.ToString()); if (_worker == null) { //MessageBox.Show("worker is null"); break; } if (_worker.CancellationPending) { //MessageBox.Show("worker cancellation pending"); break; } //Log if (BolLog) { LogName = logBaseName; } if (BolRuns) { LogName += String.Format("_{0}", finishedRuns + 1); } //Run RBFOpt var result = RunOptimizationLoop(_worker, PresetIndex); //Exit if there is no result if (result == null) { //MessageBox.Show("result is null"); break; } //Check is there is a better result if ((!BolMaximize && result.Value < bestResult.Value) || (BolMaximize && result.Value > bestResult.Value)) { bestResult = result; } //Very important to keep FrOG from crashing (probably needed to dispose the process in Run) System.Threading.Thread.Sleep(1000); finishedRuns++; } //Exit when there is no result if (double.IsPositiveInfinity(bestResult.Value) || double.IsNegativeInfinity(bestResult.Value)) { return; } //Set Grasshopper model to best value _component.OptimizationWindow.GrasshopperStatus = OptimizationWindow.GrasshopperStates.RequestSent; _worker.ReportProgress(0, bestResult.Parameters); //possible tight looping below... perhaps time limit should be added while (_component.OptimizationWindow.GrasshopperStatus != OptimizationWindow.GrasshopperStates.RequestProcessed) { //just wait until the cows come home } //Show Result Message Box if (!BolRuns) { MessageBox.Show(Log.GetResultString(bestResult, MaxIter, MaxIterNoProgress, MaxDuration), "FrOG Result"); } else { MessageBox.Show(String.Format("Finished {0} runs" + Environment.NewLine + "Overall best value {1}", finishedRuns, bestResult.Value), "FrOG Result"); } if (_worker != null) { _worker.CancelAsync(); } }