Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Coffee Break Code
Message
From
22/02/2014 07:55:32
 
 
To
22/02/2014 07:51:18
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01594925
Message ID:
01594928
Views:
61
Well it's not meant to be production code :-}

Not sure plinq is helping - maybe an excuse to play with this : http://msdn.microsoft.com/en-us/library/dd537632(v=vs.110).aspx

>You left out some constraints. :-) Very clever though.
>
>
>>Just for fun. I know DNA doesn't work this way but the way the sequence 'mutates' over generations is interesting? Pretty slow when the population hits a million :-}
using System;
>>using System.Collections.Generic;
>>using System.Diagnostics;
>>using System.Linq;
>>
>>namespace ConsoleApplication8
>>{
>>    class Program
>>    {
>>        static readonly List<Person> Population = new List<Person>(); 
>>        static Random r = new Random();
>>        static void Main(string[] args)
>>        {
>>
>>            var adam = new Person( new string('0',256),Sex.Male);
>>            var eve = new Person( new string('1',256),Sex.Female);
>>            Population.Add(adam);
>>            Population.Add(eve);
>>            try
>>            {
>>                Breed();
>>            }
>>            catch (Exception ex)
>>            {
>>                Console.WriteLine(string.Format("World is full. Population is {0}",Population.Count));
>>                Console.ReadLine();
>>            }
>>        }
>> 
>>        static void Breed()
>>        {
>>            var females = Population.Where(x => x.Sex == Sex.Female).ToArray();
>>            Console.WriteLine(String.Format("Females:{0} Males:{1}",females.Count(), Population.Count()-females.Count()));
>>
>>            List<Person> children = new List<Person>();
>>            foreach (Person male in Population.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism).Where(x => x.Sex == Sex.Male))
>>            {
>>                children.Add(male + females.Skip(r.Next(0, females.Count())).Take(1).First());
>>            }
>>            Population.AddRange(children);
>>            Console.WriteLine(string.Format("Memory used: {0}Mb",Math.Round((double)Process.GetCurrentProcess().PrivateMemorySize64/1048576)));
>>            Breed();
>>        }
>>    }
>>
>>    public struct Person
>>    {
>>        private readonly string _dna;
>>        public Sex Sex;
>>        private static Random r = new Random();
>>        public Person(string dna,Sex sex)
>>        {
>>            _dna = dna;
>>            Sex = sex;
>>        }
>>
>>        public static Person operator +(Person m, Person f)
>>        {
>>            var result = new char[256];
>>            int mStart = 0;
>>            int fStart = 128;
>>            for (var x = 0; x < 256; x += 2)
>>            {
>>                result[x] = m._dna[mStart++];
>>                result[x + 1] = f._dna[fStart++];
>>            }
>>            return new Person(new String(result), (Sex)Enum.GetValues(typeof(Sex)).GetValue(r.Next(2)));
>>        }
>>    }
>>
>>    public enum Sex
>>    {
>>        Male,
>>        Female
>>    }
>>}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform