public void TestEmpty()
        {
            Majority m = new Majority();

            int[] input = {};

            Assert.ThrowsException <Exception>(() => m.HasMajority(input));
        }
        public void Test3()
        {
            Majority m = new Majority();

            int[] input = { 1, 2, 2, 1 };

            Assert.IsFalse(m.HasMajority(input));
        }
        public void TestTrue()
        {
            Majority m = new Majority();

            int[] input = { 1, 2, 2 };

            Assert.IsTrue(m.HasMajority(input));
        }
        public void TestFalse()
        {
            // a) create object
            Majority m = new Majority();

            // b) setup
            int[] input          = { 1, 2, 3, 1, 2, 3 };
            bool  expectedResult = false;
            bool  actualResult;

            // c) act
            actualResult = m.HasMajority(input);

            // d) assert
            Assert.AreEqual(actualResult, expectedResult);
        }
        public void TestPerformance()
        {
            // a) create objects
            int      N = 100000000;
            Majority m = new Majority();

            // b) setup
            int[]  input = new int[N];
            Random r     = new Random();

            for (int i = 0; i < input.Length; i++)
            {
                input[i] = r.Next(100);
            }

            // c) act
            m.HasMajority(input);
        }
        public ActionResult AssoEdit(short infoId, short localeId, string submit,
                                     List <AssoMaj> majs,
                                     List <AssoEvent_Locale> events)
        {
            if (ModelState.IsValid)
            {
                #region Add majorities
                List <byte> currentMajId = db.Majorities.Where(x => x.LocaleId == localeId).Select(x => x.RaceId).ToList();
                if (majs != null)
                {
                    foreach (AssoMaj maj in majs)
                    {
                        //if the association already exists
                        if (currentMajId.Contains(maj.RaceId))
                        {
                            Majority toEdit = db.Majorities.Where(x => x.RaceId == maj.RaceId && x.LocaleId == localeId).First();
                            toEdit.IsFirst         = maj.IsFirst;
                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentMajId.Remove(maj.RaceId);
                        }
                        else
                        {
                            Majority toAdd = new Majority
                            {
                                LocaleId = localeId,
                                IsFirst  = maj.IsFirst,
                                RaceId   = maj.RaceId
                            };
                            db.Majorities.Add(toAdd);
                            db.SaveChanges();
                        }
                    }
                }
                if (currentMajId.Count != 0)
                {
                    foreach (byte id in currentMajId)
                    {
                        Majority gone = db.Majorities.Where(x => x.LocaleId == localeId && x.RaceId == id).FirstOrDefault();
                        db.Majorities.Remove(gone);
                        db.SaveChanges();
                    }
                }
                #endregion

                #region Add LocaleEvents
                List <short> currentEventId = db.LocaleEvents.Where(x => x.LocaleId == localeId).Select(x => x.EventId).ToList();
                if (events != null)
                {
                    foreach (AssoEvent_Locale assoEvent in events)
                    {
                        //if the association already exists
                        if (currentEventId.Contains(assoEvent.EventId))
                        {
                            LocaleEvent toEdit = db.LocaleEvents.Where(x => x.LocaleId == localeId && x.EventId == assoEvent.EventId).First();
                            //if I ever add more columns to LocaleEvent, edit them here
                            //db.Entry(toEdit).State = EntityState.Modified;
                            //db.SaveChanges();
                            currentEventId.Remove(assoEvent.EventId);
                        }
                        else
                        {
                            LocaleEvent toAdd = new LocaleEvent
                            {
                                LocaleId = localeId,
                                EventId  = assoEvent.EventId
                            };
                            db.LocaleEvents.Add(toAdd);
                        }
                    }
                }
                if (currentEventId.Count != 0)
                {
                    foreach (short id in currentEventId)
                    {
                        LocaleEvent gone = db.LocaleEvents.Where(x => x.LocaleId == localeId && x.EventId == id).FirstOrDefault();
                        db.LocaleEvents.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion
                return(Json(true));
            }
            //having a lot of trouble with the return so the ajax right now
            ViewBag.Majs   = db.Races.OrderBy(r => r.RaceName).ToList();
            ViewBag.Events = db.Events.OrderBy(e => e.Name).ToList();
            var          locale = db.Locales.Find(localeId);
            AssoLocaleVM model  = new AssoLocaleVM {
                InfoId = infoId, LocaleId = localeId, Submit = submit, AssoMajs = majs, AssoEvents = events, Name = locale.Name
            };
            return(View(model));
        }
 public AssoMaj(Majority majority)
 {
     RaceId  = majority.RaceId;
     IsFirst = majority.IsFirst;
 }
Beispiel #8
0
        protected override void Execute(CodeActivityContext context)
        {
            List <string> listStrSrc = CandidateStringList.Get(context);

            int intListCount = listStrSrc.Count;

            if (intListCount % 2 == 0 || intListCount < 3)
            {
                throw (new ArgumentException("Number of list item must be odd number and no less than 3"));
            }

            Dictionary <string, int>    dicStringCount = new Dictionary <string, int>();
            Dictionary <string, double> dicRMSE        = new Dictionary <string, double>();

            //Check CandidateStringList
            int i = 0;

            while (i < listStrSrc.Count)
            {
                if (String.IsNullOrEmpty(listStrSrc[i]))
                {
                    throw (new ArgumentException("Empty string is not allowed in CandidateStringList."));
                }
                i++;
            }

            //Main Calculation loop
            i = 0;
            double[] dblSumOfSquare = new double[intListCount * intListCount];
            while (i < intListCount)
            {
                //For check matching
                if (dicStringCount.ContainsKey(listStrSrc[i]))
                {
                    dicStringCount[listStrSrc[i]]++;
                }
                else
                {
                    dicStringCount[listStrSrc[i]] = 1;
                }

                //Calculate Edit Distance
                int j = 0;
                Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(listStrSrc[i]);

                while (j < intListCount)
                {
                    if (i != j)
                    {
                        double dblNormalizedEditDistance = ((double)lev.DistanceFrom(listStrSrc[j])) / (double)Math.Max(listStrSrc[i].Length, listStrSrc[j].Length);
                        dblSumOfSquare[j + i * intListCount] = Math.Pow(dblNormalizedEditDistance, 2);
                    }
                    j++;
                }
                i++;
            }

            // Calculate RMSE
            i = 0;
            while (i < intListCount)
            {
                if (!dicRMSE.ContainsKey(listStrSrc[i]))
                {
                    dicRMSE[listStrSrc[i]] = 0;
                    int j = 0;
                    while (j < intListCount)
                    {
                        if (i != j)
                        {
                            dicRMSE[listStrSrc[i]] += dblSumOfSquare[j + i * intListCount];
                        }
                        j++;
                    }
                    dicRMSE[listStrSrc[i]] = Math.Sqrt(dicRMSE[listStrSrc[i]] / (double)(intListCount - 1));
                }
                i++;
            }



            //Check matching count

            string[] sortedKeysCount   = new string[dicStringCount.Count];
            int[]    sortedValuesCount = new int[dicStringCount.Count];
            dicStringCount.Keys.CopyTo(sortedKeysCount, 0);
            dicStringCount.Values.CopyTo(sortedValuesCount, 0);
            Array.Sort(sortedValuesCount, sortedKeysCount);

            Array.Reverse(sortedValuesCount);
            Array.Reverse(sortedKeysCount);

            int    intCountMode             = sortedValuesCount[0];
            string strCandidateFromMatching = sortedKeysCount[0];

            if (intCountMode == intListCount)
            {
                Unanimous.Set(context, strCandidateFromMatching);
            }
            else
            {
                Unanimous.Set(context, string.Empty);
            }

            if (intCountMode > (intListCount >> 1))
            {
                Majority.Set(context, strCandidateFromMatching);
            }
            else
            {
                Majority.Set(context, string.Empty);
            }

            //Sort RMSE
            string[] sortedKeys   = new string[dicRMSE.Count];
            double[] sortedValues = new double[dicRMSE.Count];

            dicRMSE.Keys.CopyTo(sortedKeys, 0);
            dicRMSE.Values.CopyTo(sortedValues, 0);
            Array.Sort(sortedValues, sortedKeys);

            SortedTextArray.Set(context, sortedKeys);
            RMSEArray.Set(context, sortedValues);
        }