Example #1
0
        private RailResource()
        {
            MasterInstance master = RailResource.Master;

            if (master == null)
            {
                throw new ApplicationException("RailResource not initialized");
            }

            // Copy references to read-only stuff from the master
            this.EventTypeCompressor  = master.EventTypeCompressor;
            this.EntityTypeCompressor = master.EntityTypeCompressor;
            this.entityFactories      = master.EntityFactories;
            this.entityTypeToKey      = master.EntityTypeToKey;
            this.eventTypeToKey       = master.EventTypeToKey;

            // Clone or instantiate the rest
            this.commandPool = master.CloneCommandPool();
            this.statePools  = master.CloneStatePools();
            this.eventPools  = master.CloneEventPools();

            this.deltaPool         = new RailPool <RailStateDelta>();
            this.commandUpdatePool = new RailPool <RailCommandUpdate>();
#if SERVER
            this.recordPool = new RailPool <RailStateRecord>();
#endif
        }
        private Algorithm RunIteration()
        {
            // create algorithm instance to run
            Algorithm instanceToRun = MasterInstance.Clone();

            // mark this as an optimizer run
            instanceToRun.IsOptimizing = true;

            // create result entry
            OptimizerResult result = new OptimizerResult();

            foreach (OptimizerParam parameter in MasterInstance.OptimizerParams.Values)
            {
                result.Parameters[parameter.Name] = parameter.Value;
            }
            result.Fitness = null;
            Results.Add(result);

            // run algorithm with these values
            _jobQueue.QueueJob(() =>
            {
                instanceToRun.Run();
                result.NetAssetValue = instanceToRun.NetAssetValue[0];
                result.MaxDrawdown   = instanceToRun.NetAssetValueMaxDrawdown;
                result.Fitness       = instanceToRun.FitnessValue;
                instanceToRun        = null;
                lock (_optimizerLock)
                {
                    _numIterationsCompleted++;

                    TimeSpan t   = DateTime.Now - _startTime;
                    TimeSpan eta = TimeSpan.FromSeconds(
                        (_numIterationsTotal - _numIterationsCompleted)
                        * t.TotalSeconds / _numIterationsCompleted);
                    _maxFitness = Math.Max(_maxFitness, (double)result.Fitness);

                    Output.WriteLine("GridOptimizer: {0} of {1} iterations completed, max fitness = {2:F4}, eta = {3}h{4}m{5}s",
                                     _numIterationsCompleted, _numIterationsTotal, _maxFitness,
                                     Math.Floor(eta.TotalHours), eta.Minutes, eta.Seconds);
                }
            });

            return(instanceToRun);
        }
Example #3
0
        private void RunIteration()
        {
            if (!MasterInstance.CheckParametersValid())
            {
                // parameters are invalid. skip this iteration
                _numIterationsTotal--;
                return;
            }

            // create algorithm instance to run
            Algorithm instanceToRun = MasterInstance.Clone();

            // mark this as an optimizer run
            instanceToRun.IsOptimizing = true;

            // create result entry
            OptimizerResult result = new OptimizerResult();

            foreach (OptimizerParam parameter in MasterInstance.OptimizerParams.Values)
            {
                result.Parameters[parameter.Name] = parameter.Value;
            }
            result.Fitness = null;
            Results.Add(result);

            // run algorithm with these values
            _jobQueue.QueueJob(() =>
            {
                try
                {
                    // make sure to enter the extended Run method
                    // the default implementation will forward
                    // to the simple Run method, if required
                    // also, we need to convert the result to a list,
                    // in order to circumvent lazy execution
                    var noLazyExec = instanceToRun.Run(_algoStart, _algoEnd)
                                     .ToList();

                    result.NetAssetValue = instanceToRun.NetAssetValue[0];
                    result.MaxDrawdown   = instanceToRun.NetAssetValueMaxDrawdown;
                    result.Fitness       = instanceToRun.FitnessValue;
                    instanceToRun        = null;
                }

                catch (Exception e)
                {
                    // we ignore any exeption while running the algo
                    lock (_optimizerLock)
                    {
                        if (_verbose)
                        {
                            Output.WriteLine("{0}: Iteration failed. {1}", this.GetType().Name, e.Message);
                        }
                    }
                }

                finally
                {
                    lock (_optimizerLock)
                    {
                        _numIterationsCompleted++;

                        TimeSpan t   = DateTime.Now - _optimizationStart;
                        TimeSpan eta = TimeSpan.FromSeconds(
                            (_numIterationsTotal - _numIterationsCompleted)
                            * t.TotalSeconds / _numIterationsCompleted);

                        if (result.Fitness != null)
                        {
                            _maxFitness = _maxFitness != null
                                ? Math.Max((double)_maxFitness, (double)result.Fitness)
                                : (double)result.Fitness;
                        }

                        if (_verbose)
                        {
                            Output.WriteLine("GridOptimizer: {0} of {1} iterations completed, max fitness = {2:F4}, eta = {3}h{4}m{5}s",
                                             _numIterationsCompleted, _numIterationsTotal, _maxFitness,
                                             Math.Floor(eta.TotalHours), eta.Minutes, eta.Seconds);
                        }
                    }
                }
            });
        }