private void btnDelete_Click(object sender, EventArgs e) { if (lvFiles.SelectedIndices.Count <= 0) { return; } // // Ask the user if they are really sure about this if (MessageBox.Show("Confirm File Delete?", "GP Studio Data Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { // // Delete the current selection int ModelingFileID = Convert.ToInt32(lvFiles.Items[lvFiles.SelectedIndices[0]].Tag.ToString()); GPModelingData.DeleteData(ModelingFileID); // // Remove the entry from the list of files m_DeletingItem = true; lvFiles.SelectedItems[0].Remove(); m_DeletingItem = false; // // Select a new data file, if'n one exists if (lvFiles.Items.Count > 0) { lvFiles.Items[0].Selected = true; } } }
/// <summary> /// Prepare the graph to accept the plotted data /// </summary> /// <param name="gpData"></param> private void InitialzeDataGraph(GPModelingData gpData) { ZedGraph.GraphPane pane = new ZedGraph.GraphPane( new Rectangle(0, 0, graphData.Width, graphData.Height), "Modeling Data", cbXAxis.Items[cbXAxis.SelectedIndex].ToString(), gpData.Header[m_gpData.Header.Length - 1]); pane.Fill = new ZedGraph.Fill(Color.AliceBlue, Color.WhiteSmoke, 0F); pane.Chart.Fill = new ZedGraph.Fill(Color.Silver, Color.White, 45.0f); // // IF this is a time series, the x-axis is NOT whatever the combo box is // it is just a count if (gpData.TimeSeries) { pane.XAxis.Title.Text = "Count"; } // // No need for a legend here pane.Legend.IsVisible = false; // // If we are in Bar plot mode, set up accordingly if (rdChartTypeBar.Checked) { // // Use a value or count bar axis string[] XLabels = new string[gpData.Rows]; if (chkCount.Checked) { // // Build a set of lables using a row count for (int nRow = 0; nRow < gpData.Rows; nRow++) { XLabels[nRow] = ((int)(nRow + 1)).ToString(); } pane.XAxis.Title.Text = "Count"; } else { // // Build a set of labels from the common X-Axis for (int nRow = 0; nRow < gpData.Rows; nRow++) { XLabels[nRow] = gpData[nRow, 0].ToString(); } } pane.XAxis.Type = ZedGraph.AxisType.Text; pane.XAxis.Scale.TextLabels = XLabels; pane.XAxis.MajorTic.IsBetweenLabels = true; } graphData.GraphPane = pane; }
/// <summary> /// Fill the graph with the Training set /// </summary> /// <param name="Training">Reference to the Training object to be plotted</param> private void DisplayDataGraph(GPModelingData gpData) { // // If nothing is selected, get the hell out of here if (lvFiles.SelectedItems.Count == 0) { return; } // // Initialize the graph pane InitialzeDataGraph(gpData); // // The the X & Y Values double[] XValues = null; double[] YValues = null; if (gpData.TimeSeries) { // // If time series, use a count as the X-Axis XValues = new double[gpData.Rows]; YValues = new double[gpData.Rows]; for (int Value = 0; Value < gpData.Rows; Value++) { XValues[Value] = Value + 1; YValues[Value] = gpData[Value, 0]; } } else { XValues = gpData.InputColumn(cbXAxis.SelectedIndex); YValues = gpData.ObjectiveColumn(0); } if (rdChartTypeXY.Checked || rdChartTypeScatter.Checked) { ZedGraph.LineItem line = graphData.GraphPane.AddCurve("Series 1", XValues, YValues, Color.Blue, ZedGraph.SymbolType.None); if (rdChartTypeScatter.Checked) { line.Line.IsVisible = false; line.Symbol.Type = ZedGraph.SymbolType.XCross; line.Symbol.Size = 3; } } else if (rdChartTypeBar.Checked) { graphData.GraphPane.AddBar("Series 1", XValues, YValues, Color.Blue); } // // Force the graph to visually update graphData.GraphPane.AxisChange(this.CreateGraphics()); graphData.Invalidate(); }
private void menuCSV_Click(object sender, EventArgs e) { if (dlgFileOpen.ShowDialog() == DialogResult.OK) { // // Set a busy cursor tsLabel.Text = "Importing To Database..."; Cursor PrevCursor = this.Cursor; this.Cursor = Cursors.WaitCursor; GPModelingData gpData = new GPModelingData(); bool ValidFile = false; if (sender == menuCSV) { ValidFile = gpData.LoadCSV( dlgFileOpen.FileName, new GPModelingData.DELInitProgress(InitProgress), new GPModelingData.DELIncrementProgress(IncrementProgress)); } else if (sender == menuSSV) { ValidFile = gpData.LoadSSV( dlgFileOpen.FileName, new GPModelingData.DELInitProgress(InitProgress), new GPModelingData.DELIncrementProgress(IncrementProgress)); } // // Replace the busy cursor tsLabel.Text = "Done"; this.Cursor = PrevCursor; tsProgress.Value = 0; if (ValidFile) { // // Add the item to the list and select it. ListViewItem lviNew = lvFiles.Items.Add(gpData.Name); lviNew.Tag = gpData.ModelingFileID; lviNew.Group = lvFiles.Groups[0]; lviNew.ImageIndex = 0; lviNew.ToolTipText = gpData.Description; lviNew.Selected = true; } else { MessageBox.Show("Invalid File Format", "GP Studio Data Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
/// <summary> /// Setup and fill the grid with the data /// </summary> /// <param name="gpData"></param> private void DisplayDataGrid(GPModelingData gpData) { // // Set a busy cursor Cursor PrevCursor = this.Cursor; this.Cursor = Cursors.WaitCursor; tsLabel.Text = "Filling Grid"; Application.DoEvents(); // // Reset the grid dgView.Rows.Clear(); // // Get the number of columns, this is the input + objective count dgView.ColumnCount = gpData.Columns + gpData.Objectives; // // Set the view style for the cells for (int Column = 0; Column < dgView.ColumnCount; Column++) { dgView.Columns[Column].DefaultCellStyle.Format = GPEnums.NUMERIC_DISPLAY_FORMAT; dgView.Columns[Column].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; } dgView.Rows.Add(gpData.Rows); for (int Row = 0; Row < gpData.Rows; Row++) { for (int Input = 0; Input < gpData.Columns; Input++) { dgView.Rows[Row].Cells[Input].Value = gpData[Row, Input]; } for (int Objective = 0; Objective < gpData.Objectives; Objective++) { dgView.Rows[Row].Cells[gpData.Columns + Objective].Value = gpData.ObjectiveRow(Row)[Objective]; } } // // Replace the cursor this.Cursor = PrevCursor; tsLabel.Text = ""; }
/// <summary> /// Public method that goes about getting the program modeled. This is /// intended to be called asynchronously... /// </summary> public void RequestProgram() { // // Load up the distributed servers if (m_DELReportStatus != null) { m_DELReportStatus("Validating distributed server connections..."); } fmMain.ServerManager.RegisterServersFromXml(Application.StartupPath + "\\Servers.xml"); // // Get the distributed servers initialized...training data, function sets, etc. List <IGPModeler> Modelers = null; GPModelingData Training = null; InitializeServers(fmMain.ServerManager, ref Modelers, ref Training); m_PossibleHits = Training.Rows; // // Do the modeling ExecuteSimulation(Modelers, Training); }
/// <summary> /// Gets the settings for an individual modeler prepared. This function is /// intended to be called asynchronously. /// </summary> /// <param name="iServer"></param> /// <param name="iModeler"></param> /// <param name="Training"></param> /// <returns></returns> private bool InitializeModeler(IGPServer iServer, IGPModeler iModeler, GPModelingData Training) { // // The data we want to send is data appropriate for modeling. Time Series // data needs to be transformed for the modeler. if (m_DELReportStatus != null) { m_DELReportStatus("Loading training data..."); } iModeler.Training = Training.TrainingForModeling( m_Profile.ModelType.InputDimension, m_Profile.ModelType.PredictionDistance); // // Prepare the function set for each server if (m_DELReportStatus != null) { m_DELReportStatus("Transmitting user defined functions..."); } IGPFunctionSet iFunctionSet = iServer.FunctionSet; // // Use a client side sponsor for this function set // TODO: This sponsor should really come from the server we obtained // the function set interface from. The reason it currently isn't, is // because the whole "lease lifetime" thing isn't fully thought out yet // in this application. ILease LeaseInfo = (ILease)((MarshalByRefObject)iFunctionSet).GetLifetimeService(); LeaseInfo.Register(m_FunctionSetSponsor); // // Let's go ahead and assign the same lease sponsor to the modeler LeaseInfo = (ILease)((MarshalByRefObject)iModeler).GetLifetimeService(); LeaseInfo.Register(m_ModelerSponsor); #if GPLOG GPLog.ReportLine("Loading UDFs...", true); #endif foreach (string FunctionName in m_Profile.FunctionSet) { #if GPLOG GPLog.ReportLine(" UDF: " + FunctionName, false); #endif // // Load the Function from the database short FunctionArity = 0; bool TerminalParameters = false; String FunctionCode = ""; if (GPDatabaseUtils.LoadFunctionFromDB(FunctionName, ref FunctionArity, ref TerminalParameters, ref FunctionCode, GPEnums.LANGUAGEID_CSHARP)) { // // Add it to the function set iFunctionSet.AddFunction(FunctionName, FunctionArity, TerminalParameters, FunctionCode); } } iFunctionSet.UseMemory = m_Profile.ModelingProfile.UseMemory; // // Assign it to the modeler iModeler.FunctionSet = iFunctionSet; #if GPLOG GPLog.Report("Transmitting fitness function...", true); #endif // // Pass the fitness function to each modeler if (m_DELReportStatus != null) { m_DELReportStatus("Transmitting fitness function..."); } String FitnessFunction = GPDatabaseUtils.FieldValue(m_Profile.FitnessFunctionID, "tblFitnessFunction", "Code"); iModeler.FitnessFunction = FitnessFunction; #if GPLOG GPLog.ReportLine("...Complete", false); #endif // // Transmit the profile/modeling settings if (m_DELReportStatus != null) { m_DELReportStatus("Transmitting modeling parameters..."); } iModeler.Profile = m_Profile.ModelingProfile; // // Send over which ADF and ADLs to evolve foreach (byte adf in m_Profile.m_ADFSet) { iModeler.AddADF(adf); } foreach (byte adl in m_Profile.m_ADLSet) { iModeler.AddADL(adl); } foreach (byte adr in m_Profile.m_ADRSet) { iModeler.AddADR(adr); } return(true); }
/// <summary> /// Prepares the configuration settings at each server. /// *Training Data /// *UDFs /// *Fitness Function /// *Everything else /// This method builds a list of modeler interfaces at each server. Remember, /// each time the Modeler property is used from IGPServer, a NEW modeler is /// created...so only want to do this once for each modeling session; hence, /// have to access it and keep track of it on the client side. /// </summary> /// <param name="ServerManager"></param> /// <param name="Modelers"></param> /// <param name="Training"></param> /// <returns>True/False upon success or failure</returns> private bool InitializeServers(ServerManagerSingleton ServerManager, ref List <IGPModeler> Modelers, ref GPModelingData Training) { #if GPLOG GPLog.ReportLine("Initializing Distributed Servers...", true); #endif // // Create a modeler sponsor m_FunctionSetSponsor = fmMain.SponsorServer.Create("FunctionSet"); m_ModelerSponsor = fmMain.SponsorServer.Create("Modeler"); // // Build a list of modeler interfaces #if GPLOG GPLog.ReportLine("Number of GPServers: " + ServerManager.Count, true); #endif if (m_DELReportStatus != null) { m_DELReportStatus("Enumerating distributed servers..."); } List <IGPServer> Servers = new List <IGPServer>(ServerManager.Count); Modelers = new List <IGPModeler>(ServerManager.Count); foreach (IGPServer iServer in ServerManager) { Modelers.Add(iServer.Modeler); // // We also keep track of the servers so we set the right function set // with the right server. Servers.Add(iServer); } m_Modelers = Modelers; // // Based upon the selected topology, send interfaces to the connected // servers. switch (m_Profile.ModelingProfile.DistributedTopology) { case GPEnums.DistributedTopology.Ring: InitializeServersTopologyRing(Modelers); break; case GPEnums.DistributedTopology.Star: InitializeServersTopologyStar(Modelers); break; } // // Report the list of servers to the UI - they're ordered the same as the interfaces, // so the foreach is a safe way to do it. foreach (String Description in ServerManager.Descriptions.Values) { #if GPLOG GPLog.ReportLine("Server Description: " + Description, true); #endif // // Report back to the UI we have a new valid server to record data for if (m_DELValidatedServer != null) { m_DELValidatedServer(Description); } } // // Transimit the training data #if GPLOG GPLog.Report("Loading training data...", true); #endif if (m_DELReportStatus != null) { m_DELReportStatus("Loading training data..."); } Training = new GPModelingData(); Training.LoadFromDB(m_TrainingID, null, null); #if GPLOG GPLog.ReportLine("...Complete", false); #endif // // Asynchronously call initialization on each modeler List <DEL_InitializeModeler> DelegateList = new List <DEL_InitializeModeler>(Servers.Count); List <IAsyncResult> ResultList = new List <IAsyncResult>(Servers.Count); for (int ServerIndex = 0; ServerIndex < Servers.Count; ServerIndex++) { // // Make an asynchronous call to initialize DEL_InitializeModeler delInit = new DEL_InitializeModeler(InitializeModeler); IAsyncResult ar = delInit.BeginInvoke(Servers[ServerIndex], Modelers[ServerIndex], Training, null, null); DelegateList.Add(delInit); ResultList.Add(ar); } // // Go into a loop to wait for all the method calls to complete for (int Delegate = 0; Delegate < DelegateList.Count; Delegate++) { DelegateList[Delegate].EndInvoke(ResultList[Delegate]); } return(true); }
/// <summary> /// Manages the modeling simulation. /// </summary> /// <param name="Modelers">Collection of distributed modeler interfaces</param> /// <param name="Training">Data set being used for the modeling</param> private void ExecuteSimulation(List <IGPModeler> Modelers, GPModelingData Training) { m_AbortSession = false; m_HaveBestProgram = false; m_BestProgram = ""; // // Go through all the runs...but stop if a perfect program is found bool DoneSimulation = false; // // Create the initial population if (m_DELReportStatus != null) { m_DELReportStatus("Initializing Population..."); } InitializePopulations(Modelers, m_Profile.ModelingProfile.PopulationSize); // // Go through all generations...but stop if a perfect program is found int Generation = 0; while (!DoneSimulation) { #if GPLOG GPLog.ReportLine("Computing Fitness for Generation: " + Generation, true); #endif if (m_DELReportStatus != null) { m_DELReportStatus("Computing Fitness for Generation " + (Generation + 1) + "..."); } // // Let's block (for now) as each generation is computed EvaluateFitness(Modelers, Generation); List <String> BestPrograms = null; if (!m_AbortSession) { // // Grab the current best program and stats m_HaveBestProgram = true; UpdateGenerationResults(Generation, Modelers, ref BestPrograms); } // // find out if we are done DoneSimulation = IsModelRunDone(m_BestFitness, m_BestHits, Generation, Training.Training); if (!DoneSimulation) { // // Instruct the server to create the next population if (m_DELReportStatus != null) { m_DELReportStatus("Updating Population..."); } UpdatePopulations(Modelers, Generation); } Generation++; } #if GPLOG GPLog.Report("Simulation Complete, cleaning up modelers...", true); #endif // // Instruct each modeler to clean up as much as possible foreach (IGPModeler iModeler in Modelers) { iModeler.ForceCleanup(); // // Unregister it for the function set associated with the modeler ILease LeaseInfo = (ILease)((MarshalByRefObject)iModeler.FunctionSet).GetLifetimeService(); LeaseInfo.Unregister(m_FunctionSetSponsor); // // Unregister the lease sponsor for the modeler LeaseInfo = (ILease)((MarshalByRefObject)iModeler).GetLifetimeService(); LeaseInfo.Unregister(m_ModelerSponsor); } #if GPLOG GPLog.ReportLine("...Complete", false); #endif m_Modelers = null; m_ModelerSponsor = null; m_FunctionSetSponsor = null; System.GC.Collect(); }
private void lvFiles_SelectedIndexChanged(object sender, EventArgs e) { if (m_DeletingItem) { return; } if ((lvFiles.SelectedIndices.Count <= 0) || (m_bLoadFile == false)) { return; } // // Grab the DBCode int ModelingFileID = Convert.ToInt32(lvFiles.Items[lvFiles.SelectedIndices[0]].Tag.ToString()); // // Ge the Training loaded from the database m_gpData = new GPModelingData(); tsLabel.Text = "Loading From Database"; Cursor PrevCursor = this.Cursor; this.Cursor = Cursors.WaitCursor; Application.DoEvents(); m_gpData.LoadFromDB( ModelingFileID, new GPModelingData.DELInitProgress(InitProgress), new GPModelingData.DELIncrementProgress(IncrementProgress)); this.Cursor = PrevCursor; tsLabel.Text = ""; // // Setup the X-Axis selection combo box cbXAxis.Items.Clear(); // // If we have time series data, handle it separately for (int Column = 0; Column < m_gpData.Columns; Column++) { cbXAxis.Items.Add(m_gpData.Header[Column]); } cbXAxis.SelectedItem = cbXAxis.Items[0]; // // If this is a time series, autocheck the count box chkCount.Checked = m_gpData.TimeSeries; chkCount.Enabled = !m_gpData.TimeSeries; // // Place this Training into the grid DisplayDataGrid(m_gpData); NameColumns(); // // Place this Training into the graph DisplayDataGraph(m_gpData); // // If this file is in use, disable the Delete Button btnDelete.Enabled = !GPModelingData.FileInUse(ModelingFileID); tsProgress.Value = 0; }