Beispiel #1
0
 /// <summary>
 /// Set master instance to parameters from optimzation result.
 /// </summary>
 /// <param name="result">optimization result</param>
 public void SetParametersFromResult(OptimizerResult result)
 {
     foreach (var parameter in result.Parameters)
     {
         MasterInstance.OptimizerParams[parameter.Key].Value = parameter.Value;
     }
 }
        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);
        }
Beispiel #3
0
        /// <summary>
        /// Export optimizer results to Excel.
        /// </summary>
        /// <param name="excelPath"></param>
        public void ResultsToExcel(string excelPath)
        {
            Plotter logger = new Plotter();

            logger.SelectChart("Optimizer Results", "iteration");

            for (int i = 0; i < Results.Count; i++)
            {
                OptimizerResult result = Results[i];

                logger.SetX(i);
                logger.Plot("NetAssetValue", (result.NetAssetValue != null) ? string.Format("{0}", result.NetAssetValue) : "");
                logger.Plot("Fitness", (result.Fitness != null) ? string.Format("{0}", result.Fitness) : "");

                foreach (var parameter in result.Parameters)
                {
                    logger.Plot(parameter.Key, parameter.Value);
                }
            }

            logger.OpenWith(excelPath);
        }
Beispiel #4
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);
                        }
                    }
                }
            });
        }