Example #1
0
        public override void Run(ModelInterface model)
        {
            Factory.Parallel.SetMode(parallelMode);
            Factory.SysLog.ResetConfiguration();
            engine              = Factory.Engine.TickEngine("Design");
            engine.MaxBarsBack  = 2;
            engine.MaxTicksBack = 2;
            SymbolInfo symbolInfo = Factory.Symbol.LookupSymbol("Design");

            engine.SymbolInfo       = new SymbolInfo[] { symbolInfo };
            engine.IntervalDefault  = ProjectProperties.Starter.IntervalDefault;
            engine.DesiredRunMode   = RunMode.Historical;
            engine.DataFolder       = DataFolder;
            engine.EnableTickFilter = ProjectProperties.Engine.EnableTickFilter;

            if (CancelPending)
            {
                return;
            }

            engine.Model = model;

            engine.QueueTask();
            engine.WaitTask();
            var parallel = Factory.Parallel;

            parallel.Dispose();
        }
Example #2
0
        public override void Run(ModelInterface model)
        {
            Factory.SysLog.ResetConfiguration();
            engine              = Factory.Engine.TickEngine;
            engine.MaxBarsBack  = 2;
            engine.MaxTicksBack = 2;
            SymbolInfo symbolInfo = Factory.Symbol.LookupSymbol("Design");

            engine.SymbolInfo       = new SymbolInfo[] { symbolInfo };
            engine.Providers        = SetupProviders(false, false);
            engine.IntervalDefault  = ProjectProperties.Starter.IntervalDefault;
            engine.RunMode          = RunMode.Historical;
            engine.EnableTickFilter = ProjectProperties.Engine.EnableTickFilter;

            if (CancelPending)
            {
                return;
            }

            engine.Model = model;

            engine.QueueTask();
            engine.WaitTask();
        }
Example #3
0
        public override void Run(ModelLoaderInterface loader)
        {
            Factory.SysLog.ResetConfiguration();
            try {
                if (loader.OptimizeOutput == null)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(FileName));
                    File.Delete(FileName);
                }
            } catch (Exception ex) {
                log.Error("Error while creating directory and deleting '" + FileName + "'.", ex);
                return;
            }
            this.loader           = loader;
            this.loader.QuietMode = true;
            startMillis           = Factory.TickCount;
            engineIterations      = new List <TickEngine>();

            loader.OnInitialize(ProjectProperties);

            totalTasks = 0;

            foreach (var num in RecursiveOptimize(0))
            {
                totalTasks++;
            }

            tasksRemaining = totalTasks;

            int tasksPerEngine = CalculateTasksPerEngine(totalTasks);

            int iterations     = Math.Max(1, totalTasks / maxParallelPasses);
            int leftOverPasses = Math.Max(0, totalTasks - (maxParallelPasses * iterations));

            if (totalTasks % maxParallelPasses > 0)
            {
                log.Notice("Planning " + iterations + " iterations with " + maxParallelPasses + " passes plus 1 pass with " + leftOverPasses + " iterations.");
            }
            else
            {
                log.Notice("Planning " + iterations + " iterations with " + maxParallelPasses + " passes each.");
            }

            ModelInterface topModel = new Portfolio();

            passCount = 0;
            foreach (var num in RecursiveOptimize(0))
            {
                ModelInterface model = ProcessLoader(loader, passCount);
                topModel.Chain.Dependencies.Add(model.Chain);
                passCount++;
                if (passCount % tasksPerEngine == 0)
                {
                    TickEngine engine = ProcessHistorical(topModel, true);
                    engine.QueueTask();
                    engineIterations.Add(engine);
                    topModel = new Portfolio();
                    if (engineIterations.Count >= Environment.ProcessorCount)
                    {
                        ProcessIteration();
                    }
                }
            }

            if (topModel.Chain.Dependencies.Count > 0)
            {
                TickEngine engine = ProcessHistorical(topModel, true);
                engine.QueueTask();
                engineIterations.Add(engine);
            }

            if (engineIterations.Count > 0)
            {
                ProcessIteration();
            }

            long elapsedMillis = Factory.TickCount - startMillis;

            log.Notice("Finished optimizing in " + elapsedMillis + "ms.");
        }
Example #4
0
        public override void Run(ModelLoaderInterface loader)
        {
            Factory.SysLog.ResetConfiguration();
            this.loader = loader;

            try {
                if (loader.OptimizeOutput == null)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(FileName));
                    File.Delete(FileName);
                }
            } catch (Exception ex) {
                log.Error("Error while creating directory and deleting '" + FileName + "'.", ex);
                return;
            }

            log.Notice("Beginning Genetic Optimize of: ");
            log.Notice(loader.Name + " model loader. Type: " + loader.GetType().Name);
            loader.QuietMode = true;

            loader.OnInitialize(ProjectProperties);

            optimizeVariables = new List <ModelProperty>();
            for (int i = 0; i < loader.Variables.Count; i++)
            {
                ModelProperty var = loader.Variables[i];
                if (var.Optimize)
                {
                    optimizeVariables.Add(var);
                }
            }

            // Get Total Number of Bits
            int totalBits = 0;

            for (int i = 0; i < optimizeVariables.Count; i++)
            {
                ModelProperty var  = optimizeVariables[i];
                int           bits = Convert.ToString(var.Count - 1, 2).Length;
                totalBits += bits;
            }

            if (optimizeVariables.Count == 1)
            {
                generationCount = 1;
            }

            // Get the highest count.
            populationCount = totalPasses / generationCount;
            tasksRemaining  = totalPasses;

            log.Notice("Assigning genomes.");

            // Create initial set of random chromosomes.
            generation = new List <Chromosome>();
            // This list assures we never retry a previous one twice.
            alreadyTried = new List <Chromosome>();

            // Create a genome holder.
            int[] genome = new int[optimizeVariables.Count];

            // Indexes for going through randomList
            int[] indexes = new int[optimizeVariables.Count];

//			for( int repeat=0; repeat < Math.Min(optimizeVariables.Count,2); repeat++) {
//
            //Get random values for each.
            List <List <int> > randomLists = new List <List <int> >();

            for (int i = 0; i < optimizeVariables.Count; i++)
            {
                randomLists.Add(GetRandomIndexes(optimizeVariables[i]));
            }

            // Create initial population
            for (int loop = 0; loop < populationCount; loop++)
            {
                // Set the genome from the randomLists using the indexes.
                for (int i = 0; i < optimizeVariables.Count; i++)
                {
                    genome[i] = randomLists[i][indexes[i]];
                }

                Chromosome chromosome = new Chromosome(genome);
                log.Debug(chromosome.ToString());
                generation.Add(chromosome);
                alreadyTried.Add(chromosome);
                for (int i = 0; i < indexes.Length; i++)
                {
                    indexes[i]++;
                    ModelProperty var = optimizeVariables[i];
                    if (indexes[i] >= populationCount)
                    {
                        indexes[i] = 0;
                    }
                }
            }
//			}

                        #if CLRPROFILER
            CLRProfilerControl.LogWriteLine("Entering Genetic Loop");
            CLRProfilerControl.AllocationLoggingActive = true;
            CLRProfilerControl.CallLoggingActive       = false;
                        #endif

            int totalEngineCount = Environment.ProcessorCount * generationCount;

            // Pre-setup engines. This causes the progress
            // bar to show a complete set of information for all
            // generations.
            var engines = new Stack <TickEngine>();
            for (int i = 0; i < totalEngineCount; i++)
            {
                engines.Push(SetupEngine(true));
            }

            for (int genCount = 0; genCount < generationCount && !CancelPending; genCount++)
            {
                // Assign fitness values
                var topModels = new List <ModelInterface>();
                for (int i = generation.Count - 1; i >= 0; i--)
                {
                    Chromosome chromosome = generation[i];
                    if (!chromosome.FitnessAssigned)
                    {
                        ModifyVariables(chromosome);
                        var model = ProcessLoader(loader, i);
                        topModels.Add(model);
                    }
                    else
                    {
                        tasksRemaining--;
                        log.Debug("Saves processing on " + chromosome + "!");
                    }
                }

                int tasksPerEngine = CalculateTasksPerEngine(topModels.Count);

                ModelInterface topModel  = new Portfolio();
                int            passCount = 0;
                foreach (var model in topModels)
                {
                    topModel.Chain.Dependencies.Add(model.Chain);
                    passCount++;
                    if (passCount % tasksPerEngine == 0)
                    {
                        var engine = engines.Pop();
                        engine.Model = topModel;
                        engine.QueueTask();
                        engineIterations.Add(engine);
                        topModel = new Portfolio();
                        if (engineIterations.Count >= Environment.ProcessorCount)
                        {
                            ProcessIteration();
                        }
                    }
                }

                if (topModel.Chain.Dependencies.Count > 0)
                {
                    TickEngine engine = ProcessHistorical(topModel, true);
                    engine.QueueTask();
                    engineIterations.Add(engine);
                }

                if (engineIterations.Count > 0)
                {
                    ProcessIteration();
                }

                generation.Sort();

                log.Notice("After sorting generation...");
                double maxFitness = 0;
                for (int i = 0; i < generation.Count; i++)
                {
                    log.Debug(generation[i].ToString());
                    maxFitness = Math.Max(generation[i].Fitness, maxFitness);
                }
                // If none of the genes in the chromosome
                // had a positive fitness, stop here.
                if (maxFitness <= 0)
                {
                    break;
                }

                List <Chromosome> newGeneration = new List <Chromosome>();
                log.Notice("Crossover starting...");
                while (newGeneration.Count < populationCount - 1)
                {
                    Chromosome chromo1 = Roulette();
                    Chromosome chromo2;
                    do
                    {
                        chromo2 = Roulette();
                    } while(chromo2.Equals(chromo1));

                    log.Debug("Before: " + chromo1 + " - " + chromo2);
                    chromo1.DoubleCrossOver(chromo2);
                    log.Debug("After: " + chromo1 + " - " + chromo2);

                    if (alreadyTried.Contains(chromo1))
                    {
                        chromo1 = alreadyTried[alreadyTried.IndexOf(chromo1)];
                    }
                    else
                    {
                        alreadyTried.Add(chromo1);
                    }
                    if (alreadyTried.Contains(chromo2))
                    {
                        chromo2 = alreadyTried[alreadyTried.IndexOf(chromo2)];
                    }
                    else
                    {
                        alreadyTried.Add(chromo2);
                    }
                    newGeneration.Add(chromo1);
                    newGeneration.Add(chromo2);
                }
                generation = newGeneration;
            }

            GetEngineResults();

            WriteEngineResults(loader, engineIterations);

            engineIterations.Clear();

                        #if CLRPROFILER
            CLRProfilerControl.AllocationLoggingActive = false;
            CLRProfilerControl.CallLoggingActive       = false;
            CLRProfilerControl.LogWriteLine("Exiting Genetic Loop");
                #endif

            log.Notice("Genetic Algorithm Finished.");
        }