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