예제 #1
0
        private void Initialize(IAlgorithm algorithm)
        {
            parameters = new ObservableDictionary <string, IItem>();
            results    = new ObservableDictionary <string, IItem>();

            if (algorithm.StoreAlgorithmInEachRun)
            {
                var clone = (IAlgorithm)algorithm.Clone();
                clone.CollectParameterValues(parameters);
                clone.CollectResultValues(results);
                clone.Runs.Clear();
                this.algorithm = clone;
            }
            else
            {
                var par = new Dictionary <string, IItem>();
                var res = new Dictionary <string, IItem>();
                algorithm.CollectParameterValues(par);
                algorithm.CollectResultValues(res);
                var cloner = new Cloner();
                foreach (var k in par)
                {
                    parameters.Add(k.Key, cloner.Clone(k.Value));
                }
                foreach (var k in res)
                {
                    results.Add(k.Key, cloner.Clone(k.Value));
                }
            }
        }
예제 #2
0
        private IAlgorithm ExportAlgorithm(IAlgorithm source)
        {
            var preprocessedAlgorithm = (IAlgorithm)source.Clone();

            preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
            preprocessedAlgorithm.Runs.Clear();
            var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;

            problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
            return(preprocessedAlgorithm);
        }
예제 #3
0
 private void algorithmTabPage_DragDrop(object sender, DragEventArgs e)
 {
     if (e.Effect != DragDropEffects.None)
     {
         IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
         if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy)
         {
             algorithm = (IAlgorithm)algorithm.Clone();
         }
         Content.Algorithm = algorithm;
     }
 }
예제 #4
0
    private void Initialize(IAlgorithm algorithm) {
      parameters = new ObservableDictionary<string, IItem>();
      results = new ObservableDictionary<string, IItem>();

      if (algorithm.StoreAlgorithmInEachRun) {
        var clone = (IAlgorithm)algorithm.Clone();
        clone.CollectParameterValues(parameters);
        clone.CollectResultValues(results);
        clone.Runs.Clear();
        this.algorithm = clone;
      } else {
        var par = new Dictionary<string, IItem>();
        var res = new Dictionary<string, IItem>();
        algorithm.CollectParameterValues(par);
        algorithm.CollectResultValues(res);
        var cloner = new Cloner();
        foreach (var k in par) parameters.Add(k.Key, cloner.Clone(k.Value));
        foreach (var k in res) results.Add(k.Key, cloner.Clone(k.Value));
      }
    }
예제 #5
0
        public void Start()
        {
            if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
            {
                throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
            }

            if (Algorithm != null)
            {
                //create cloned algorithms
                if (clonedAlgorithms.Count == 0)
                {
                    int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value;

                    for (int i = 0; i < Folds.Value; i++)
                    {
                        IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone();
                        clonedAlgorithm.Name = algorithm.Name + " Fold " + i;
                        IDataAnalysisProblem         problem         = clonedAlgorithm.Problem as IDataAnalysisProblem;
                        ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem;

                        int testStart = (i * testSamplesCount) + SamplesStart.Value;
                        int testEnd   = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value;

                        problem.ProblemData.TrainingPartition.Start = SamplesStart.Value;
                        problem.ProblemData.TrainingPartition.End   = SamplesEnd.Value;
                        problem.ProblemData.TestPartition.Start     = testStart;
                        problem.ProblemData.TestPartition.End       = testEnd;
                        DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData;
                        if (problemData != null)
                        {
                            problemData.TrainingPartitionParameter.Hidden = false;
                            problemData.TestPartitionParameter.Hidden     = false;
                        }

                        if (symbolicProblem != null)
                        {
                            symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;
                            symbolicProblem.FitnessCalculationPartition.End   = SamplesEnd.Value;
                        }
                        clonedAlgorithm.Prepare();
                        clonedAlgorithms.Add(clonedAlgorithm);
                    }
                }

                //start prepared or paused cloned algorithms
                int startedAlgorithms = 0;
                foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms)
                {
                    if (startedAlgorithms < NumberOfWorkers.Value)
                    {
                        if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||
                            clonedAlgorithm.ExecutionState == ExecutionState.Paused)
                        {
                            // start and wait until the alg is started
                            using (var signal = new ManualResetEvent(false)) {
                                EventHandler signalSetter = (sender, args) => { signal.Set(); };
                                clonedAlgorithm.Started += signalSetter;
                                clonedAlgorithm.Start();
                                signal.WaitOne();
                                clonedAlgorithm.Started -= signalSetter;

                                startedAlgorithms++;
                            }
                        }
                    }
                }
                OnStarted();
            }
        }
예제 #6
0
 private IAlgorithm ExportAlgorithm(IAlgorithm source) {
   var preprocessedAlgorithm = (IAlgorithm)source.Clone();
   preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
   preprocessedAlgorithm.Runs.Clear();
   var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
   problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
   return preprocessedAlgorithm;
 }
예제 #7
0
        //----- buttons
        #region private async void RunButton_Click(object sender, RoutedEventArgs e)
        private async void RunButton_Click(object sender, RoutedEventArgs e)
        {
            RunButton.IsEnabled    = false;
            ReportButton.IsEnabled = false;
            bool saveOptimizerButton = OptimizerButton.IsEnabled;

            OptimizerButton.IsEnabled = false;
            bool saveResultsButton = ResultsButton.IsEnabled;

            ResultsButton.IsEnabled = false;
            AlgoMenu.IsEnabled      = false;
            _runningBacktest        = true;

            bool debug = ControlPressed;

            DateTime algorithmTimeStamp = _currentAlgorithmInfo.SourcePath != null
                ? (new FileInfo(_currentAlgorithmInfo.SourcePath).LastWriteTime)
                : default(DateTime);

            if (algorithmTimeStamp != _currentAlgorithmTimestamp)
            {
                // algorithm instance out of date. re-instantiate!
                SelectAlgo(AlgoLookupName(_currentAlgorithmInfo));
            }

            ClearLog();

            if (_currentAlgorithm != null)
            {
                await Task.Run(() =>
                {
                    void uiThread()
                    {
                        DateTime timeStamp1 = DateTime.Now;

#if true
                        // replace current instance with a freshly cloned instance
                        // this helps run poorly initialized algorithms
                        var clonedAlgorithm = _currentAlgorithm.Clone();
                        _currentAlgorithm   = clonedAlgorithm;

                        if (_optimizer != null)
                        {
                            _optimizer.MasterInstance = _currentAlgorithm;
                        }
#endif

                        WriteEventHandler(
                            string.Format("running algorithm {0}", _currentAlgorithm.Name)
                            + Environment.NewLine);
                        try
                        {
#if true
                            if (debug)
                            {
                                System.Diagnostics.Debugger.Launch();
                            }
#endif
                            // 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 = _currentAlgorithm.Run(null, null)
                            //    .ToList();
                            _currentAlgorithm.Run();
                        }
                        catch (Exception exception)
                        {
                            WriteEventHandler(
                                string.Format("EXCEPTION: {0}{1}", exception.Message, exception.StackTrace)
                                + Environment.NewLine);
                        }

                        DateTime timeStamp2 = DateTime.Now;
                        WriteEventHandler(
                            string.Format("finished algorithm {0} after {1:F1} seconds", _currentAlgorithm.Name, (timeStamp2 - timeStamp1).TotalSeconds)
                            + Environment.NewLine);
                        //WriteEventHandler(""); // will force flush
                    }
#if false
                    // run on same thread as ui
                    uiThread();
#else
                    // run each simulation on new thread
                    Thread thread = new Thread(uiThread);
                    thread.Start();
                    thread.Join(); // wait for window to close
#endif
                });
            }

            RunButton.IsEnabled       = true;
            ReportButton.IsEnabled    = true;
            OptimizerButton.IsEnabled = saveOptimizerButton;
            ResultsButton.IsEnabled   = saveResultsButton;
            AlgoMenu.IsEnabled        = true;
            _runningBacktest          = false;

            ReportButton_Click(null, null);
        }