/// <summary> /// Instructs each modeler to update its population /// </summary> /// <param name="Modelers"></param> /// <param name="Generation"></param> private void UpdatePopulations(List <IGPModeler> Modelers, int Generation) { #if GPLOG GPLog.Report("Distributing Programs...", true); #endif // // Tell each server to distribute its programs, use async calls so everybody // works asynchronously. List <DEL_IDistributePopulation> DelegatesDistribute = new List <DEL_IDistributePopulation>(Modelers.Count); List <IAsyncResult> ResultsDistribute = new List <IAsyncResult>(Modelers.Count); for (int ModelerIndex = 0; ModelerIndex < Modelers.Count; ModelerIndex++) { DEL_IDistributePopulation del = new DEL_IDistributePopulation(Modelers[ModelerIndex].DistributePopulation); IAsyncResult ar = del.BeginInvoke(null, null); DelegatesDistribute.Add(del); ResultsDistribute.Add(ar); } // // Go into a loop to wait for all the method calls to complete for (int Delegate = 0; Delegate < DelegatesDistribute.Count; Delegate++) { DelegatesDistribute[Delegate].EndInvoke(ResultsDistribute[Delegate]); } #if GPLOG GPLog.ReportLine("...Complete", false); #endif #if GPLOG GPLog.Report("Computing next populations...", true); #endif // // Tell each server to compute its next population generation // Make all calls asynchronously List <DEL_IComputeNextGeneration> DelegateNext = new List <DEL_IComputeNextGeneration>(Modelers.Count); List <IAsyncResult> ResultNext = new List <IAsyncResult>(Modelers.Count); for (int ModelerIndex = 0; ModelerIndex < Modelers.Count; ModelerIndex++) { DEL_IComputeNextGeneration del = new DEL_IComputeNextGeneration(Modelers[ModelerIndex].ComputeNextGeneration); IAsyncResult ar = del.BeginInvoke(Generation, null, null); DelegateNext.Add(del); ResultNext.Add(ar); } // // Go into a loop to wait for all the method calls to complete for (int Delegate = 0; Delegate < DelegateNext.Count; Delegate++) { DelegateNext[Delegate].EndInvoke(ResultNext[Delegate]); } #if GPLOG GPLog.ReportLine("...Complete", false); #endif }
/// <summary> /// Manages the modeling for a batch process. /// </summary> /// <param name="Process"></param> /// <returns>true if model was created, false otherwise</returns> private bool BatchModel(BatchProcess Process) { #if GPLOG GPLog.ReportLine("Batch Processing: Requesting a program.", true); #endif // // Create the modeler object - the parameter to this thread is // the model profile. m_Modeler = new GPModeler( Process.Profile, Process.TrainingDataID, null, //new GPModeler.DEL_ValidatedServer(AddValidatedServer), new GPModeler.DEL_ReportStatus(ReceiveStatus), new GPModeler.DEL_ReportFitness(ReceiveFitness), null); //new GPModeler.DEL_GenerationComplete(GenerationComplete)); // // Make an asynchronous call that gets the modeling started MethodInvoker miModeler = new MethodInvoker(m_Modeler.RequestProgram); IAsyncResult ar = miModeler.BeginInvoke(null, null); // // Wait for the modeling to complete miModeler.EndInvoke(ar); // // Record the time of completion Process.TimeCompleted = DateTime.Now; // // Check to see if we were canceled if (m_CancelSimulation) { #if GPLOG GPLog.ReportLine("Batch Processing: Simulation Canceled", true); #endif Process.Canceled = true; // // Reset the cancel flag m_CancelSimulation = false; return(false); } #if GPLOG GPLog.ReportLine("Batch Processing: Program request complete.", true); #endif // // Save the best program to the database int ProgramID = 0; // A dummy variable m_Modeler.SaveBestToDB(Process.ProjectID, ref ProgramID); return(true); }
/// <summary> /// The entry point for a modeling thread. This connects to the server, /// sends the modeling profile, training Training and then requests the model /// to be written. /// </summary> /// <param name="arg">Reference to the modeling profile</param> private void ModelThread(Object arg) { #if GPLOG GPLog.ReportLine("Requesting a program.", true); #endif // // Create the modeler object - the parameter to this thread is // the model profile. m_Modeler = new GPModeler( (GPProjectProfile)arg, m_Project.DataTrainingID, new GPModeler.DEL_ValidatedServer(AddValidatedServer), new GPModeler.DEL_ReportStatus(UpdateModelStatus), new GPModeler.DEL_ReportFitness(ReportFitness), new GPModeler.DEL_GenerationComplete(GenerationComplete)); // // Make an asynchronous call that gets the modeling started MethodInvoker miModeler = new MethodInvoker(m_Modeler.RequestProgram); IAsyncResult ar = miModeler.BeginInvoke(null, null); // // Now we can go ahead and enable the cancel button EnableCancel(); // // Wait for the modeling to complete miModeler.EndInvoke(ar); #if GPLOG GPLog.ReportLine("Program request complete.", true); #endif // // Save the best program to the database int ProgramID = 0; if (m_Modeler.SaveBestToDB(m_Project.DBCode, ref ProgramID)) { // // Update the results display and select this program UpdateModelStatus("Saving best program..."); UpdateModelResults(ProgramID); } // // Do some cleanup, now that the modeling is complete EnableModelButton(); ProgressReset(); UpdateModelStatus(""); EnableStatusMarquee(false); }
/// <summary> /// Load a data set in csv (commad separated values) from a disk file and import it into the database /// </summary> /// <param name="Filename">Name of file to load</param> /// <param name="DelInit">Delegate to call to indicate the initialization params</param> /// <param name="DelIncrement">Delete to call for each new row of data imported</param> /// <returns></returns> public bool LoadCSV(String Filename, DELInitProgress DelInit, DELIncrementProgress DelIncrement) { try { m_Header = null; List <List <double> > TempData = new List <List <double> >(); using (StreamReader reader = new StreamReader(new FileStream(Filename, FileMode.Open))) { // // Read the Training in int RowCount = 0; while (reader.EndOfStream == false) { String[] sFields = reader.ReadLine().Split(','); // // If this is the first row, perform an automatic detection // of header values. bool IsHeader = false; if (RowCount == 0) { IsHeader = ParseFileHeader(sFields); } if (!IsHeader) { // // Add a row TempData.Add(new List <double>()); // // Add the values foreach (String Value in sFields) { double Number = Convert.ToDouble(Value, GPUtilities.NumericFormat); // Enforce US "." format TempData[RowCount].Add(Number); } RowCount++; } } } return(ImportToDB(Filename, DelInit, DelIncrement, TempData)); } catch { #if GPLOG GPLog.ReportLine("csv file read error", true); #endif return(false); } }
/// <summary> /// Specifies the renewal time for the remote object /// </summary> /// <param name="LeaseInfo"></param> /// <returns></returns> public TimeSpan Renewal(ILease LeaseInfo) { // // Log the time the renewal was called #if GPLOG GPLog.ReportLine("Sponsor Renewal - " + m_Name, true); #endif if (Renew) { return(TimeSpan.FromMinutes(GPEnums.REMOTING_RENEWAL_MINUTES)); } return(TimeSpan.Zero); }
/// <summary> /// Instructs each server to build their initial populations. Each call to /// the server is async, but this method blocks until all populations are /// constructed. /// </summary> /// <param name="Modelers"></param> /// <param name="TotalPopulation"></param> private void InitializePopulations(List <IGPModeler> Modelers, int TotalPopulation) { // // Evenly split up the population, according to the following rules // 1. All populations must be even numbered in size // 3. Granularity of 100 // 3. Minimum of 100 int PopulationEach = TotalPopulation / Modelers.Count; PopulationEach = (PopulationEach / 100) * 100; PopulationEach = Math.Max(PopulationEach, 100); #if GPLOG GPLog.ReportLine("Initializing Populations. Size @ each server: " + PopulationEach, true); #endif // // Make all calls asynchronously List <DEL_IInitializePopulation> DelegateList = new List <DEL_IInitializePopulation>(Modelers.Count); List <IAsyncResult> ResultList = new List <IAsyncResult>(Modelers.Count); for (int ModelerIndex = 0; ModelerIndex < Modelers.Count; ModelerIndex++) { DEL_IInitializePopulation delInit = new DEL_IInitializePopulation(Modelers[ModelerIndex].InitializePopulation); IAsyncResult ar = delInit.BeginInvoke(PopulationEach, 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]); } #if GPLOG GPLog.ReportLine("Population Initialization Complete", true); #endif }
/// <summary> /// Copys all nondominated programs from ArchivePopulation and PopCurrent into /// the next archive...which is returned from the method /// </summary> /// <param name="ArchivePopulation"></param> /// <param name="Population"></param> private List <SPEA2ProgramStats> UpdateArchive(GPPopulation Population) { SPEA2ProgramStats[] SPEA2Population = new SPEA2ProgramStats[Population.Count]; // // Grab the complexity of each program. We do this here, rather than getting it // from the Population object because it saves some computation time due to the // complexity values being reused several times. int[] Complexity = PrepareComplexity(Population); // // Init the min/max objective values - arbitrarily choose the first program's complexity value m_MinObjectiveFitness = m_MaxObjectiveFitness = m_RawFitness[0]; m_MinObjectiveComplexity = m_MaxObjectiveComplexity = Complexity[0]; // // Compute how many solutions each program in the current Population is dominated by for (int ProgramA = 0; ProgramA < Population.Count; ProgramA++) { SPEA2Population[ProgramA] = new SPEA2ProgramStats(); SPEA2Population[ProgramA].Program = Population.Programs[ProgramA]; SPEA2Population[ProgramA].RawFitness = m_RawFitness[ProgramA]; SPEA2Population[ProgramA].Complexity = Complexity[ProgramA]; SPEA2Population[ProgramA].Strength = 0; // // Test ProgramA against all programs in the current Population for (int ProgramB = 0; ProgramB < Population.Count; ProgramB++) { if ((m_RawFitness[ProgramB] > SPEA2Population[ProgramA].RawFitness) && (Complexity[ProgramB] > SPEA2Population[ProgramA].Complexity)) { SPEA2Population[ProgramA].Strength++; } } // // Test ProgramA against all programs in the current archive for (int ProgramB = 0; ProgramB < Archive.Count; ProgramB++) { if (Archive[ProgramB].RawFitness > SPEA2Population[ProgramA].RawFitness && Archive[ProgramB].Complexity > SPEA2Population[ProgramA].Complexity) { SPEA2Population[ProgramA].Strength++; } } // // Maintain the min/max objective values from the current Population m_MinObjectiveFitness = Math.Min(SPEA2Population[ProgramA].RawFitness, m_MinObjectiveFitness); m_MinObjectiveComplexity = Math.Min(SPEA2Population[ProgramA].Complexity, m_MinObjectiveComplexity); m_MaxObjectiveFitness = Math.Max(SPEA2Population[ProgramA].RawFitness, m_MaxObjectiveFitness); m_MaxObjectiveComplexity = Math.Max(SPEA2Population[ProgramA].Complexity, m_MaxObjectiveComplexity); } // // Count how many solutions each program in the archive is dominated by for (int ProgramA = 0; ProgramA < Archive.Count; ProgramA++) { SPEA2ProgramStats ProgramAStats = Archive[ProgramA]; ProgramAStats.Strength = 0; // // Test ProgramA against all programs in the current Population for (int ProgramB = 0; ProgramB < Population.Count; ProgramB++) { if (m_RawFitness[ProgramB] > ProgramAStats.RawFitness && Complexity[ProgramB] > ProgramAStats.Complexity) { ProgramAStats.Strength++; } } // // Test ProgramA against all programs in the current archive for (int ProgramB = 0; ProgramB < Archive.Count; ProgramB++) { if (Archive[ProgramB].RawFitness > ProgramAStats.RawFitness && Archive[ProgramB].Complexity > ProgramAStats.Complexity) { ProgramAStats.Strength++; } } Archive[ProgramA] = ProgramAStats; // // Maintain the min/max objective values from the current archive m_MinObjectiveFitness = Math.Min(ProgramAStats.RawFitness, m_MinObjectiveFitness); m_MinObjectiveComplexity = Math.Min(ProgramAStats.Complexity, m_MinObjectiveComplexity); m_MaxObjectiveFitness = Math.Max(ProgramAStats.RawFitness, m_MaxObjectiveFitness); m_MaxObjectiveComplexity = Math.Max(ProgramAStats.Complexity, m_MaxObjectiveComplexity); } if (m_MinObjectiveComplexity == m_MaxObjectiveComplexity) { m_MaxObjectiveComplexity += 1.0; } // // Now, we can compute the SPEA2 fitness for each individual by summing // the strenth values for each program it is dominated by. for (int ProgramA = 0; ProgramA < Population.Count; ProgramA++) { SPEA2Population[ProgramA].SPEA2Fitness = 0.0; // // Test ProgramA against all programs in the current Population for (int ProgramB = 0; ProgramB < Population.Count; ProgramB++) { if (m_RawFitness[ProgramB] < SPEA2Population[ProgramA].RawFitness && Complexity[ProgramB] < SPEA2Population[ProgramA].Complexity) { SPEA2Population[ProgramA].SPEA2Fitness += SPEA2Population[ProgramB].Strength; } } // // Test ProgramA against all programs in the current archive for (int ProgramB = 0; ProgramB < Archive.Count; ProgramB++) { if (Archive[ProgramB].RawFitness < SPEA2Population[ProgramA].RawFitness && Archive[ProgramB].Complexity < SPEA2Population[ProgramA].Complexity) { SPEA2Population[ProgramA].SPEA2Fitness += Archive[ProgramB].Strength; } } } // // Now, do it for the current archive for (int ProgramA = 0; ProgramA < Archive.Count; ProgramA++) { SPEA2ProgramStats ProgramAStats = Archive[ProgramA]; ProgramAStats.SPEA2Fitness = 0.0; // // Test ProgramA against all programs in the current Population for (int ProgramB = 0; ProgramB < Population.Count; ProgramB++) { if (SPEA2Population[ProgramB].RawFitness < ProgramAStats.RawFitness && SPEA2Population[ProgramB].Complexity < ProgramAStats.Complexity) { ProgramAStats.SPEA2Fitness += SPEA2Population[ProgramB].Strength; } } // // Test ProgramA against all programs in the current archive for (int ProgramB = 0; ProgramB < Archive.Count; ProgramB++) { if (Archive[ProgramB].RawFitness < ProgramAStats.RawFitness && Archive[ProgramB].Complexity < ProgramAStats.Complexity) { ProgramAStats.SPEA2Fitness += Archive[ProgramB].Strength; } } Archive[ProgramA] = ProgramAStats; } // // Compute the Density estimate for each program in the current Population for (int ProgramA = 0; ProgramA < Population.Count; ProgramA++) { SPEA2Population[ProgramA].Density = ComputeDensity(ref SPEA2Population[ProgramA], SPEA2Population, Archive); } // // Compute the Density estimate for each program in the current archive for (int ProgramA = 0; ProgramA < Archive.Count; ProgramA++) { SPEA2ProgramStats ProgramAStats = Archive[ProgramA]; ProgramAStats.Density = ComputeDensity(ref ProgramAStats, SPEA2Population, Archive); Archive[ProgramA] = ProgramAStats; } // // Final SPEA2 fitness calculation: F(i)=R(i)+D(i) for (int ProgramA = 0; ProgramA < Population.Count; ProgramA++) { SPEA2Population[ProgramA].SPEA2Fitness = SPEA2Population[ProgramA].SPEA2Fitness + SPEA2Population[ProgramA].Density; } for (int ProgramA = 0; ProgramA < Archive.Count; ProgramA++) { SPEA2ProgramStats ProgramAStats = Archive[ProgramA]; ProgramAStats.SPEA2Fitness = ProgramAStats.SPEA2Fitness + ProgramAStats.Density; Archive[ProgramA] = ProgramAStats; } // // Add all nondominated programs from the current Population into the new archive // The max complexity program allowed in the archive is 10,000. There are two reasons for this... // *The List container has an "short" sized counter, so programs bigger than 16 bits can't be stored // in the compressed array representation. Setting a value of 10,000 keeps programs from // getting too out of hand. // *A program size of 10,000 is ridiculous in size, so don't allow it. List <SPEA2ProgramStats> New_Archive = new List <SPEA2ProgramStats>(); SortedDictionary <int, int> New_ArchivePrograms = new SortedDictionary <int, int>(); for (int Program = 0; Program < Population.Programs.Count; Program++) { if (SPEA2Population[Program].SPEA2Fitness < 1.0 && SPEA2Population[Program].Complexity > 2 && SPEA2Population[Program].Complexity < 10000) { New_Archive.Add(SPEA2Population[Program]); New_ArchivePrograms.Add(Program, Program); } } // // Add all nondominated programs for the current archive into the new archive for (int Program = 0; Program < Archive.Count; Program++) { if (Archive[Program].SPEA2Fitness < 1.0 && Archive[Program].Complexity > 2) { New_Archive.Add(Archive[Program]); } } #if GPLOG GPLog.ReportLine("Archive Size: " + New_Archive.Count, true); #endif // // Get the archive to the right size int ArchiveSize = Population.Count / ARCHIVESCALE; if (New_Archive.Count < ArchiveSize) { ExpandArchive(New_Archive, ArchiveSize, SPEA2Population, New_ArchivePrograms); } else if (New_Archive.Count > ArchiveSize) { ShrinkArchive(New_Archive, ArchiveSize); } return(New_Archive); }
/// <summary> /// Save the best program to the database /// </summary> /// <returns>True/False depending upon success or failure</returns> public bool SaveBestToDB(int ProjectID, ref int ProgramID) { #if GPLOG GPLog.ReportLine("Saving best program to DB...", true); #endif // // Make sure we have something to save if (!m_HaveBestProgram) { #if GPLOG GPLog.ReportLine("No best program exists, nothing to save", true); #endif return(false); } // // Save the program text to a memory stream and then read back from // it to get the bytes...pretty slick! #if GPLOG GPLog.Report("Converting to a memory stream...", true); #endif byte[] blobData = null; using (MemoryStream msProgram = new MemoryStream()) { StreamWriter strmProgram = new StreamWriter(msProgram); strmProgram.Write(m_BestProgram); strmProgram.Flush(); // // Reposition the memory stream to the beginning and now read back out of it :) msProgram.Seek(0, SeekOrigin.Begin); blobData = new byte[msProgram.Length]; msProgram.Read(blobData, 0, System.Convert.ToInt32(msProgram.Length)); } #if GPLOG GPLog.ReportLine("...Complete", false); #endif #if GPLOG GPLog.Report("Creating the insert parameter...", true); #endif OleDbParameter param = new OleDbParameter( "@Profile", OleDbType.VarBinary, blobData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blobData); #if GPLOG GPLog.ReportLine("...Complete", false); #endif using (OleDbConnection con = GPDatabaseUtils.Connect()) { // // Build the command to add the blob to the database String sSQL = "INSERT INTO tblProgram(ProjectID,Fitness,Hits,MaxHits,Complexity,ModelProfileID,[Date],Program) VALUES "; sSQL += "(" + ProjectID; sSQL += "," + Convert.ToString(m_BestFitness, GPUtilities.NumericFormat); sSQL += "," + m_BestHits; sSQL += "," + m_PossibleHits; sSQL += "," + m_BestComplexity; sSQL += "," + m_Profile.ProfileID; sSQL += "," + DateTime.Now.ToString("#yyyy-MM-dd HH:mm:ss#"); sSQL += ",?)"; OleDbCommand cmd = new OleDbCommand(sSQL, con); #if GPLOG GPLog.ReportLine("SQL Write Command: " + sSQL, true); #endif cmd.Parameters.Add(param); // // Execute the command ProgramID = 0; try { #if GPLOG GPLog.Report("Attempting to write to the database...", true); #endif cmd.ExecuteNonQuery(); #if GPLOG GPLog.ReportLine("Successful DB write!", false); #endif // // Obtain back the DBCode for this model cmd.CommandText = "SELECT @@IDENTITY"; OleDbDataReader reader = cmd.ExecuteReader(); reader.Read(); ProgramID = Convert.ToInt32(reader[0].ToString());; reader.Close(); #if GPLOG GPLog.ReportLine("Successfully obtained the project ID", true); #endif } catch (OleDbException ex) { #if GPLOG GPLog.ReportLine("...Failed!", false); GPLog.ReportLine("Exception during database write: " + ex.Message.ToString(), true); #endif string sError = ex.Message.ToString(); return(false); } } return(true); }
/// <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> /// Works through the set of distributed servers, gets the best program /// and records the statistics generated at each server. /// </summary> private void UpdateGenerationResults(int Generation, List <IGPModeler> Modelers, ref List <String> BestPrograms) { // // Collect best program from each modeler BestPrograms = new List <string>(Modelers.Count); for (int ModelerIndex = 0; ModelerIndex < Modelers.Count; ModelerIndex++) { BestPrograms.Add(Modelers[ModelerIndex].BestProgram); } #if GPLOG GPLog.ReportLine("Number of best programs returned: " + BestPrograms.Count, true); #endif m_BestFitness = 1000000000.0; // Some crazy big value m_BestComplexity = 10000000; // Some crazy big value m_BestHits = 0; // Worst possible value #if GPLOG GPLog.ReportLine("Finding Best program among servers...", true); #endif // // Collect the best program and population stats from each server for (int ModelerIndex = 0; ModelerIndex < Modelers.Count; ModelerIndex++) { ModelingResults.ServerData Stats = new ModelingResults.ServerData(); // // Best program Modelers[ModelerIndex].GetBestProgramStats( out Stats.BestFitness, out Stats.BestHits, out Stats.BestComplexity); // // Population stats Modelers[ModelerIndex].GetPopulationStats( out Stats.FitnessMaximum, out Stats.FitnessAverage, out Stats.ComplexityMinimum, out Stats.ComplexityMaximum, out Stats.ComplexityAverage); Stats.FitnessMinimum = Stats.BestFitness; // // Reports this back to the UI if (m_DELReportFitness != null) { m_DELReportFitness(Generation, ModelerIndex, Stats); } #if GPLOG GPLog.ReportLine(" Testing Program - Fitness: " + Stats.BestFitness + " Hits: " + Stats.BestHits + " Complexity: " + Stats.BestComplexity, false); #endif // // Track the best program string if (Stats.BestFitness < m_BestFitness) { m_BestProgram = BestPrograms[ModelerIndex]; m_BestFitness = Stats.BestFitness; m_BestHits = Stats.BestHits; m_BestComplexity = Stats.BestComplexity; } else if (Stats.BestFitness == m_BestFitness && Stats.BestComplexity < m_BestComplexity) { m_BestProgram = BestPrograms[ModelerIndex]; m_BestFitness = Stats.BestFitness; m_BestHits = Stats.BestHits; m_BestComplexity = Stats.BestComplexity; } } #if GPLOG if (BestUpdated) { GPLog.ReportLine(" A best program was found", false); } else { GPLog.ReportLine(" No best program was updated!!", false); GPLog.ReportLine(" Modeler Count: " + Modelers.Count, false); } #endif if (m_DELGenerationComplete != null) { m_DELGenerationComplete(); } }
/// <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(); }
/// <summary> /// Sets the transport protocol, according to the server configuration file /// </summary> private static bool InitializeProtocol() { try { Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location); // // Prepare the listening port int ServerPort = Convert.ToInt32(config.AppSettings.Settings[SETTINGS_GPSERVERPORT_STR].Value); IDictionary props = new Hashtable(); props["port"] = ServerPort; // // Register the transport channel, either TCP or HTTP if (config.AppSettings.Settings[SETTINGS_GPSERVERPROTOCOL_STR].Value.ToUpper() == GPEnums.CHANNEL_TCP) { #if GPLOG GPLog.Report("Attempting to register the TCP channel...", true); #endif BinaryServerFormatterSinkProvider prov = new BinaryServerFormatterSinkProvider(); prov.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; TcpChannel Channel = new TcpChannel(props, null, prov); ChannelServices.RegisterChannel(Channel, false); #if GPLOG GPLog.ReportLine("succeeded!", false); #endif } else if (config.AppSettings.Settings[SETTINGS_GPSERVERPROTOCOL_STR].Value.ToUpper() == GPEnums.CHANNEL_HTTP) { #if GPLOG GPLog.Report("Attempting to register the HTTP channel...", true); #endif SoapServerFormatterSinkProvider prov = new SoapServerFormatterSinkProvider(); prov.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; HttpChannel Channel = new HttpChannel(props, null, prov); ChannelServices.RegisterChannel(Channel, false); #if GPLOG GPLog.ReportLine("succeeded!", false); #endif } else { return(false); } #if GPLOG GPLog.Report("Attempting to register well known service type...", true); #endif // // Activate the listening object! RemotingConfiguration.RegisterWellKnownServiceType( typeof(GPServer), GPEnums.SERVER_SOAPNAME, WellKnownObjectMode.Singleton); #if GPLOG GPLog.ReportLine("succeeded!", false); #endif } catch (Exception) { #if GPLOG GPLog.ReportLine("failure!", false); #endif return(false); } return(true); }