Beispiel #1
0
        /// <summary>
        /// Exports the generation composition to the corresponding files.
        /// </summary>
        /// <typeparam name="TInstance">The instance type.</typeparam>
        /// <param name="listOfCurrentInstances"> The list of current instances.</param>
        /// <param name="listOfCurrentGenomes"> The list of current genomes.</param>
        /// <param name="generationInstanceCompositionFile"> The file to write the generation instance composition to.</param>
        /// <param name="generationGenomeCompositionFile"> The file to write the generation genome composition to.</param>
        public static void ExportGenerationComposition <TInstance>(
            IEnumerable <TInstance> listOfCurrentInstances,
            IEnumerable <GenomeDoubleRepresentation> listOfCurrentGenomes,
            FileInfo generationInstanceCompositionFile,
            FileInfo generationGenomeCompositionFile)
            where TInstance : InstanceBase
        {
            if (listOfCurrentInstances == null)
            {
                throw new ArgumentNullException(nameof(listOfCurrentInstances));
            }

            if (listOfCurrentGenomes == null)
            {
                throw new ArgumentNullException(nameof(listOfCurrentGenomes));
            }

            var instanceLine = GrayBoxUtils.GetAndCheckLine(
                listOfCurrentInstances.Select(instance => instance.ToId()).ToArray(),
                GrayBoxUtils.DataRecorderDelimiter);
            var genomeLine = GrayBoxUtils.GetAndCheckLine(
                listOfCurrentGenomes.Select(genome => genome.ToGenomeIdentifierStringRepresentation()).ToArray(),
                GrayBoxUtils.DataRecorderDelimiter);

            File.AppendAllText(
                generationInstanceCompositionFile.FullName,
                $"{instanceLine}{Environment.NewLine}");

            File.AppendAllText(
                generationGenomeCompositionFile.FullName,
                $"{genomeLine}{Environment.NewLine}");
        }
Beispiel #2
0
        /// <summary>
        /// Moves the old data log files to another directory.
        /// </summary>
        /// <param name="dataRecordDirectoryPath">The path to the data record directory.</param>
        /// <param name="targetDirectoryPath">The path to the target directory.</param>
        /// <param name="tuningStartsFromExistingStatus">Bool, indicating whether the tuning starts from an existing status.</param>
        /// <param name="currentGeneration">The current generation.</param>
        public static void MoveOldDataLogFiles(
            string dataRecordDirectoryPath,
            string targetDirectoryPath,
            bool tuningStartsFromExistingStatus,
            int currentGeneration)
        {
            if (string.IsNullOrEmpty(targetDirectoryPath))
            {
                throw new ArgumentException("The path to the data record directory cannot be empty!");
            }

            if (string.IsNullOrEmpty(targetDirectoryPath))
            {
                throw new ArgumentException("The path to the target directory cannot be empty!");
            }

            if (!Directory.Exists(dataRecordDirectoryPath))
            {
                return;
            }

            var dataLogFiles = GrayBoxUtils.GetAllDataLogFilesInDirectory(dataRecordDirectoryPath);

            if (dataLogFiles.Count == 0)
            {
                return;
            }

            foreach (var dataLogFile in dataLogFiles)
            {
                // If tuning starts from existing status, move only data log files, which are from future generations or post tuning runs!
                if (tuningStartsFromExistingStatus)
                {
                    if (!GrayBoxUtils.TryToGetGenerationIdFromDataLogFileName(dataLogFile.Name, out var generation))
                    {
                        continue;
                    }

                    if (generation >= 0 && generation <= currentGeneration)
                    {
                        continue;
                    }
                }

                GrayBoxUtils.TryToMoveFile(dataLogFile, new FileInfo(Path.Combine(targetDirectoryPath, dataLogFile.Name)));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sets the final results of the list of current data records and writes this list to the corresponding data log file.
        /// </summary>
        /// <param name="finalResult">The final result.</param>
        public void WriteListOfDataRecordsToFile(TResult finalResult)
        {
            lock (this._lock)
            {
                this.AddFinalDataRecordIfMissing(finalResult);

                // Set final results.
                this._listOfDataRecords.ForEach(x => x.TunerDataRecord.FinalResult = finalResult);

                // Write data records to corresponding data log file.
                var dataLogFileName = GrayBoxUtils.GetDataLogFileName(
                    this._tunerDataRecord.GenerationId,
                    ProcessUtils.GetCurrentProcessId(),
                    this._actorId,
                    finalResult.TargetAlgorithmStatus);
                var dataRecorder =
                    new DataRecorder <TResult>(new FileInfo(Path.Combine(this._configuration.DataRecordDirectoryPath, dataLogFileName)));
                dataRecorder.WriteRows(this._listOfDataRecords);
            }
        }