public Phenotype[] ChooseBestByRoulete(int qty)
        {
            Phenotype[] result = new Phenotype[qty];

            float totalFitness = 0;

            foreach (Phenotype phenotype in phenotypes)
            {
                totalFitness += phenotype.fitness;
            }

            KeyValuePair<Phenotype, double>[] candiates = new KeyValuePair<Phenotype, double>[phenotypes.Count];
            int i = 0;
            foreach (Phenotype phenotype in phenotypes)
            {
                double p = (rouleteBiasToTheBest + rnd.NextDouble() * (1 - rouleteBiasToTheBest)) * phenotype.fitness / totalFitness;
                candiates[i] = new KeyValuePair<Phenotype, double>(phenotype, p);
                i++;
            }

            IOrderedEnumerable<KeyValuePair<Phenotype, double>> bestCandidates = candiates.OrderByDescending(
                (KeyValuePair<Phenotype, double> candidate) => candidate.Value
            );

            int added = 0;
            foreach (KeyValuePair<Phenotype, double> candidate in bestCandidates)
            {
                result[added++] = candidate.Key;
                if (added >= qty) break;
            }

            return result;
        }
Example #2
0
        public void TestEnvironmentProperty()
        {
            Assert.NotEqual(0, new Process().StartInfo.Environment.Count);

            ProcessStartInfo psi = new ProcessStartInfo();

            // Creating a detached ProcessStartInfo will pre-populate the environment
            // with current environmental variables.

            IDictionary <string, string> environment = psi.Environment;

            Assert.NotEqual(0, environment.Count);

            int countItems = environment.Count;

            environment.Add("NewKey", "NewValue");
            environment.Add("NewKey2", "NewValue2");

            Assert.Equal(countItems + 2, environment.Count);
            environment.Remove("NewKey");
            Assert.Equal(countItems + 1, environment.Count);

            environment.Add("NewKey2", "NewValue2Overridden");
            Assert.Equal("NewValue2Overridden", environment["NewKey2"]);

            //Clear
            environment.Clear();
            Assert.Equal(0, environment.Count);

            //ContainsKey
            environment.Add("NewKey", "NewValue");
            environment.Add("NewKey2", "NewValue2");
            Assert.True(environment.ContainsKey("NewKey"));

            Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), environment.ContainsKey("newkey"));
            Assert.False(environment.ContainsKey("NewKey99"));

            //Iterating
            string result = null;
            int    index  = 0;

            foreach (string e1 in environment.Values.OrderBy(p => p))
            {
                index++;
                result += e1;
            }
            Assert.Equal(2, index);
            Assert.Equal("NewValueNewValue2", result);

            result = null;
            index  = 0;
            foreach (string e1 in environment.Keys.OrderBy(p => p))
            {
                index++;
                result += e1;
            }
            Assert.Equal("NewKeyNewKey2", result);
            Assert.Equal(2, index);

            result = null;
            index  = 0;
            foreach (KeyValuePair <string, string> e1 in environment.OrderBy(p => p.Key))
            {
                index++;
                result += e1.Key;
            }
            Assert.Equal("NewKeyNewKey2", result);
            Assert.Equal(2, index);

            //Contains
            Assert.True(environment.Contains(new KeyValuePair <string, string>("NewKey", "NewValue")));
            Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), environment.Contains(new KeyValuePair <string, string>("nEwKeY", "NewValue")));
            Assert.False(environment.Contains(new KeyValuePair <string, string>("NewKey99", "NewValue99")));

            //Exception not thrown with invalid key
            Assert.Throws <ArgumentNullException>(() => environment.Contains(new KeyValuePair <string, string>(null, "NewValue99")));

            environment.Add(new KeyValuePair <string, string>("NewKey98", "NewValue98"));

            //Indexed
            string newIndexItem = environment["NewKey98"];

            Assert.Equal("NewValue98", newIndexItem);

            //TryGetValue
            string stringout = null;

            Assert.True(environment.TryGetValue("NewKey", out stringout));
            Assert.Equal("NewValue", stringout);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.True(environment.TryGetValue("NeWkEy", out stringout));
                Assert.Equal("NewValue", stringout);
            }

            stringout = null;
            Assert.False(environment.TryGetValue("NewKey99", out stringout));
            Assert.Equal(null, stringout);

            //Exception not thrown with invalid key
            Assert.Throws <ArgumentNullException>(() =>
            {
                string stringout1 = null;
                environment.TryGetValue(null, out stringout1);
            });

            //Exception not thrown with invalid key
            Assert.Throws <ArgumentNullException>(() => environment.Add(null, "NewValue2"));

            environment.Add("NewKey2", "NewValue2OverriddenAgain");
            Assert.Equal("NewValue2OverriddenAgain", environment["NewKey2"]);

            //Remove Item
            environment.Remove("NewKey98");
            environment.Remove("NewKey98");   //2nd occurrence should not assert

            //Exception not thrown with null key
            Assert.Throws <ArgumentNullException>(() => { environment.Remove(null); });

            //"Exception not thrown with null key"
            Assert.Throws <KeyNotFoundException>(() => environment["1bB"]);

            Assert.True(environment.Contains(new KeyValuePair <string, string>("NewKey2", "NewValue2OverriddenAgain")));
            Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), environment.Contains(new KeyValuePair <string, string>("NEWKeY2", "NewValue2OverriddenAgain")));

            Assert.False(environment.Contains(new KeyValuePair <string, string>("NewKey2", "newvalue2Overriddenagain")));
            Assert.False(environment.Contains(new KeyValuePair <string, string>("newkey2", "newvalue2Overriddenagain")));

            //Use KeyValuePair Enumerator
            string[] results = new string[2];
            var      x       = environment.GetEnumerator();

            x.MoveNext();
            results[0] = x.Current.Key + " " + x.Current.Value;
            x.MoveNext();
            results[1] = x.Current.Key + " " + x.Current.Value;

            Assert.Equal(new string[] { "NewKey NewValue", "NewKey2 NewValue2OverriddenAgain" }, results.OrderBy(s => s));

            //IsReadonly
            Assert.False(environment.IsReadOnly);

            environment.Add(new KeyValuePair <string, string>("NewKey3", "NewValue3"));
            environment.Add(new KeyValuePair <string, string>("NewKey4", "NewValue4"));


            //CopyTo - the order is undefined.
            KeyValuePair <string, string>[] kvpa = new KeyValuePair <string, string> [10];
            environment.CopyTo(kvpa, 0);

            KeyValuePair <string, string>[] kvpaOrdered = kvpa.OrderByDescending(k => k.Value).ToArray();
            Assert.Equal("NewKey4", kvpaOrdered[0].Key);
            Assert.Equal("NewKey2", kvpaOrdered[2].Key);

            environment.CopyTo(kvpa, 6);
            Assert.Equal(default(KeyValuePair <string, string>), kvpa[5]);
            Assert.StartsWith("NewKey", kvpa[6].Key);
            Assert.NotEqual(kvpa[6].Key, kvpa[7].Key);
            Assert.StartsWith("NewKey", kvpa[7].Key);
            Assert.NotEqual(kvpa[7].Key, kvpa[8].Key);
            Assert.StartsWith("NewKey", kvpa[8].Key);

            //Exception not thrown with null key
            Assert.Throws <ArgumentOutOfRangeException>(() => { environment.CopyTo(kvpa, -1); });

            //Exception not thrown with null key
            Assert.Throws <ArgumentException>(() => { environment.CopyTo(kvpa, 9); });

            //Exception not thrown with null key
            Assert.Throws <ArgumentNullException>(() =>
            {
                KeyValuePair <string, string>[] kvpanull = null;
                environment.CopyTo(kvpanull, 0);
            });
        }
        protected void _GetSearchResults(CMSPageRequest pageRequest)
        {
            int  pagesize, page;
            bool all;

            Search.SiteSearch siteSearch;
            SearchInstruction instructions = getSearchInstructions(pageRequest,
                                                                   out page,
                                                                   out pagesize,
                                                                   out all,
                                                                   out siteSearch);

            //override pagesize from Schema
            var pgSize = pageRequest.GetElementValue("ResultsPageSize");

            if (pgSize != null)
            {
                pagesize = pgSize.ToInt() ?? 10;
            }

            Search.Search search = new Search.Search(Request);

            int count = 0;

            if (search != null)
            {
                //use the SearchInstruction based overload method to achieve max flexibility (non-paginated results)
                var pageResults = search.QueryFinal(siteSearch, out count, instructions, 200, true);

                //find matches for both member records and pages
                var    M           = new MembersWrapper();
                string termsString = pageRequest.QueryString["terms"] ?? "";
                //var memDic = M.Search(termsString);
                var memDic       = new List <KeyValuePair <string, Member> >();
                var pgCollection = pageResults
                                   .Select(r => new KeyValuePair <float, SearchResultItem>(r.Score, r));

                //apply sourceFilter and merge the two dictionaries
                string sourceFilter = pageRequest.QueryString["sourceFilter"] ?? "";
                IEnumerable <KeyValuePair <float, object> > mergedCollection = new KeyValuePair <float, object> [0];
                if (sourceFilter == "" || sourceFilter != "members")
                {
                    mergedCollection = pgCollection
                                       .Where(x =>
                    {
                        bool externalItem = (x.Value.Type == "ingeniux.search.htmlsitesource");
                        return(sourceFilter == "" ||
                               (sourceFilter == "local" && !externalItem) ||
                               (sourceFilter == "bni.com" && externalItem));
                    })
                                       .Select(x =>
                                               new KeyValuePair <float, object>(x.Key, x.Value));
                }

                if (sourceFilter == "" || sourceFilter == "members")
                {
                    mergedCollection = mergedCollection
                                       .Concat(memDic
                                               .Select(x => new KeyValuePair <float, object>(float.Parse(x.Key.SubstringBefore("_")), x.Value)));
                }

                //sort results
                var returnResults = mergedCollection.OrderByDescending(x => x.Key).Select(x => x.Value).ToList();

                //apply pagination
                ViewBag.totalResults = returnResults.Count;
                ViewBag.pageSize     = pagesize;
                pageRequest.Tag      = returnResults.Skip((page - 1) * pagesize).Take(pagesize).ToList();
                //pageRequest.Tag = mergedDic;  //no pagination
            }
        }