示例#1
0
        private void UpdateLoop()
        {
            //int currentTick = Environment.TickCount;
            //int stopAtTick = currentTick + CurrentTimeLimit;

            bool useTimeLimit      = CurrentTimeLimit > 0;
            bool useIterationLimit = CurrentIterationLimit > 0;

            while (Continue)
            {
                if (NeedsUpdate)
                {
                    Sim.RemoveActions();
                    Sim.AddActions(true, BestChromosome.Values.Where(y => y > 0).Select(x => CraftingAction.CraftingActions[x]));
                    NeedsUpdate = false;

                    if (CopyBestRotationToPopulations)
                    {
                        for (int i = 0; i < Populations.Length; i++)
                        {
                            Populations[i].PendingBest = BestChromosome.Clone();
                        }
                    }
                    CraftingSim sim = Sim.Clone(true);
                    Utils.AddRotationFromSim(sim);
                    FoundBetterRotation(sim);
                }

                if ((useIterationLimit && (Iterations >= CurrentIterationLimit)))
                {
                    Continue = false;
                }
            }

            Stopped();
        }
示例#2
0
        public async Task Start(int taskCount = 10, int chromosomeCount = 190, bool leaveStartingActions = false, int timeLimit = 0, int iterationLimit = 0)
        {
            AvailableActions = CraftingAction.CraftingActions.Values.Where(x => x.Level <= Sim.Level).Select(y => y.Id).ToArray();
            if (Populations == null)
            {
                Populations = new Population[taskCount];
                for (int i = 0; i < taskCount; i++)
                {
                    Populations[i] = new Population(i, Sim.Clone(), chromosomeCount, CraftingSim.MaxActions, AvailableActions);
                }
            }


            Continue    = true;
            NeedsUpdate = false;
            Iterations  = 0;
            Utils.CraftingStates.Clear();
            TaskCount = taskCount;

            Tasks = new Task[TaskCount];

            if (Populations == null)
            {
                Populations = new Population[TaskCount];
                for (int i = 0; i < TaskCount; i++)
                {
                    Populations[i] = new Population(i, Sim.Clone(), chromosomeCount, CraftingSim.MaxActions, AvailableActions);
                }
            }


            if (Populations.Length != TaskCount)
            {
                Population[] newPopulations = new Population[TaskCount];
                Array.Copy(Populations, newPopulations, Math.Min(TaskCount, Populations.Length));
                Populations = newPopulations;
            }

            for (int i = 0; i < TaskCount; i++)
            {
                if (Populations[i] == null)
                {
                    Populations[i] = new Population(i, Sim.Clone(), chromosomeCount, CraftingSim.MaxActions, AvailableActions);
                }
                else if (Populations[i].Chromosomes.Length != chromosomeCount)
                {
                    Populations[i].ChangeSize(chromosomeCount);
                }
            }

            for (int i = 0; i < TaskCount; i++)
            {
                Populations[i].ChangeAvailableValues(AvailableActions);
            }

            LeaveStartingActions = leaveStartingActions;
            BestChromosome       = new Chromosome(Sim.Clone(), AvailableActions, CraftingSim.MaxActions, Sim.GetCraftingActions().Select(x => x.Id).ToArray());

            for (int i = 0; i < Populations.Length; i++)
            {
                Populations[i].PendingBest = BestChromosome.Clone();
            }

            CurrentTimeLimit      = timeLimit;
            CurrentIterationLimit = iterationLimit;

            await Task.Run(() =>
            {
                Task.Run(UpdateLoop);
                for (int i = 0; i < TaskCount; i++)
                {
                    Tasks[i] = new Task(InnerStart, i);
                    Tasks[i].Start();
                }

                Task.WaitAll(Tasks);
            });
        }