Example #1
0
        private static void Main(string[] args)
        {
            // canonical

            var context = new Context(new ConcreteStrategyA());

            context.AlgorithmInterface();

            context = new Context(new ConcreteStrategyB());
            context.AlgorithmInterface();

            // live
            var sortList = new SortList();

            sortList.Add(10);
            sortList.Add(2);
            sortList.Add(4);
            sortList.Add(5);
            sortList.Add(1);

            sortList.SetStrategy(new BubbleSortStrategy());
            sortList.SetStrategy(new QuickSortStrategy());

            sortList.Sort();
        }
Example #2
0
        public void Merge(SortList <AIPlayer> individuals, List <AIPlayer> offspring, Simulation simulation)
        {
            //If having only a single parent, replace it if better
            foreach (AIPlayer o in offspring)
            {
                if (o.Parent2 == null)
                {
                    for (int i = 0; i < individuals.Count; i++)
                    {
                        if (individuals.Get(i) == o.Parent1)
                        {
                            if (o.GetFitness() > individuals.Get(i).GetFitness())
                            {
                                individuals.Remove(individuals.Get(i));
                                individuals.Add(o);
                            }
                        }
                    }
                }
                else
                {
                    //The offspring were made from 2 parents
                    //If offspring is better than both parents, replace both parents and add a random AIPlayer
                    bool betterThanParent1 = false;
                    bool betterThanParent2 = false;
                    for (int i = 0; i < individuals.Count; i++)
                    {
                        if (individuals.Get(i) == o.Parent1 && individuals.Get(i).GetFitness() < o.Parent1.GetFitness())
                        {
                            betterThanParent1 = true;
                        }
                        if (individuals.Get(i) == o.Parent2 && individuals.Get(i).GetFitness() < o.Parent2.GetFitness())
                        {
                            betterThanParent2 = true;
                        }
                    }

                    //if better than both parents, remove both parents and add a random immigrant
                    if (betterThanParent1 && betterThanParent2)
                    {
                        individuals.Remove(o.Parent1);
                        individuals.Remove(o.Parent2);
                        individuals.Add(o);
                        AIPlayer randomImmagrant = new AIPlayer(simulation.NeuralNetworkMaker);
                        randomImmagrant.CalcFitness(simulation.Game);
                        individuals.Add(randomImmagrant);
                    }
                }
            }
            individuals.Crop(individuals.Count / 2);
            while (individuals.Count < simulation.PopulationSize)
            {
                AIPlayer randomImmagrant = new AIPlayer(simulation.NeuralNetworkMaker);
                randomImmagrant.CalcFitness(simulation.Game);
                individuals.Add(randomImmagrant);
            }
        }
Example #3
0
        public Boolean AddSpecified(CheckedListBox List)
        {
            int cnt = 0;

            for (int i = 0; i <= (List.Items.Count - 1); i++)
            {
                if (List.GetItemChecked(i))
                {
                    cnt = (i + 1);
                }
            }
            if (cnt >= 2)
            {
                for (i = 0; i <= (List.Items.Count - 1); i++)
                {
                    if (List.GetItemChecked(i))
                    {
                        SortList.Add(List.Items[i].ToString());
                    }
                }
                return(true);
            }
            else
            {
                MessageBox.Show("2つ以上の選択が必要", "Error");
                return(false);
            }
        }
Example #4
0
 private void AddRank(MilitaryRank rank)
 {
     if (rank != null)
     {
         MilitaryRanks.Add(rank, rank.ID);
     }
 }
Example #5
0
        /// <summary>
        /// 填充排序
        /// </summary>
        /// <param name="lstSort"></param>
        protected virtual void FillOrderBy(string name, SortList lstSort)
        {
            string str = ViewState[name + SortViewStateID] as string;

            if (string.IsNullOrEmpty(str))
            {
                return;
            }
            string[] sortItems = str.Split(',');
            foreach (string item in sortItems)
            {
                Sort     objSort = new Sort();
                string[] iPart   = item.Trim().Split(' ');
                if (iPart.Length < 1)
                {
                    continue;
                }
                objSort.PropertyName = iPart[0];
                if (iPart.Length < 2)
                {
                    objSort.SortType = SortType.ASC;
                }
                else
                {
                    objSort.SortType = (iPart[1].ToLower() == "asc") ? SortType.ASC : SortType.DESC;
                }
                lstSort.Add(objSort);
            }
        }
Example #6
0
        public Person[] GetPossiblePersons()
        {
            SortList <Person> person = new SortList <Person>();

            for (int i = 0; i < PossiblePersons.Count; i++)
            {
                person.Add(PossiblePersons[i], Position[i]);
            }
            return(person.Get());
        }
Example #7
0
        public void AddProfession(Profession profession)
        {
            //- If already contained
            if (Professions.Find(profession.Importance) != null)
                return;

            Professions.Add(profession, profession.Importance);
            if (MainProfession == null || MainProfession.Importance < profession.Importance)
                MainProfession = profession;
        }
Example #8
0
        public void InitializeRandomPopulation()
        {
            double cloneRate  = Simulation.InitialSimilarity;
            double mutateRate = Simulation.InitialMutation;

            if (cloneRate > 1.0 || cloneRate < 0.0)
            {
                throw new Exception("clone rate is not set correctly.");
            }
            if (individuals == null)
            {
                individuals = new SortList <AIPlayer>();
            }
            individuals.Clear();

            AIPlayer cloneIndividual = new AIPlayer(Simulation.NeuralNetworkMaker);

            cloneIndividual.CalcFitness(Simulation.Game);
            individuals.Add(cloneIndividual);
            int cloneSize = (int)(cloneRate * Simulation.PopulationSize);

            for (int i = 1; i < cloneSize; i++)
            {
                Chromosome clonedChromosome = cloneIndividual.Chromosome.Clone();
                if (mutateRate > 0.0)
                {
                    clonedChromosome = clonedChromosome.GetMutated(mutateRate);
                }
                cloneIndividual = new AIPlayer(clonedChromosome, Simulation.NeuralNetworkMaker);
                cloneIndividual.CalcFitness(Simulation.Game);
                individuals.Add(cloneIndividual);
            }

            int randSize = Simulation.PopulationSize - cloneSize;

            for (int i = 0; i < randSize; i++)
            {
                AIPlayer randomIndividual = new AIPlayer(Simulation.NeuralNetworkMaker);
                randomIndividual.CalcFitness(Simulation.Game);
                individuals.Add(randomIndividual);
            }
        }
Example #9
0
        public SortList <T> IListToSortList <T>(IList lst)
        {
            IEnumerator  e    = lst.GetEnumerator();
            SortList <T> list = new SortList <T>();

            while (e.MoveNext())
            {
                list.Add((T)e.Current);
            }
            return(list);
        }
 public void Merge(SortList <AIPlayer> individuals, List <AIPlayer> offspring, Simulation simulation)
 {
     //If having only single parent, replace it with a probability
     foreach (AIPlayer o in offspring)
     {
         if (o.Parent2 == null)
         {
             if (o.GetFitness() > o.Parent1.GetFitness() && individuals.Contains(o.Parent1))
             {
                 individuals.Remove(o.Parent1);
             }
             individuals.Add(o);
         }
         else
         {
             //The offspring were made from 2 parents
             AIPlayer firstPriority  = null;
             AIPlayer secondPriority = null;
             if (Utility.RandomNum.RandomInt(0, 2) == 0)
             {
                 firstPriority  = o.Parent1;
                 secondPriority = o.Parent2;
             }
             else
             {
                 firstPriority  = o.Parent2;
                 secondPriority = o.Parent1;
             }
             if (o.GetFitness() > firstPriority.GetFitness() && individuals.Contains(firstPriority))
             {
                 individuals.Remove(firstPriority);
                 individuals.Add(o);
             }
             else if (o.GetFitness() > secondPriority.GetFitness() && individuals.Contains(secondPriority))
             {
                 individuals.Remove(secondPriority);
                 individuals.Add(o);
             }
         }
     }
 }
Example #11
0
        private void LoadIdeologies()
        {
            string nspace = "FastPolitics1919.History.Ideologies";
            var    q      = from t in Assembly.GetExecutingAssembly().GetTypes()
                            where t.IsClass && t.Namespace == nspace
                            select t;
            List <Type> ideology_types = q.ToList();

            foreach (Type type in ideology_types)
            {
                Ideology ideology = Engine.CreateClass <Ideology>(type);
                Ideologies.Add(ideology, ideology.ID);
            }
        }
Example #12
0
        private void LoadMapModes()
        {
            string nspace2 = "FastPolitics1919.History.MapModes";
            var    q2      = from t in Assembly.GetExecutingAssembly().GetTypes()
                             where t.IsClass && t.Namespace == nspace2
                             select t;

            List <Type> types = q2.ToList();

            foreach (Type type in types)
            {
                MapMode mode = Engine.CreateClass <MapMode>(type);
                EveryMapMode.Add(mode, mode.ID);
            }
        }
 public void Merge(SortList <AIPlayer> individuals, List <AIPlayer> offspring, Simulation simulation)
 {
     if (simulation.Population.MeasureDiversity() < diversityThreshold)
     {
         individuals.Crop(individuals.Count / 2);
         int immigrants = simulation.PopulationSize - individuals.Count;
         for (int i = 0; i < immigrants; i++)
         {
             AIPlayer aip = new AIPlayer(simulation.NeuralNetworkMaker);
             aip.CalcFitness(simulation.Game);
             individuals.Add(aip);
         }
     }
     replacementRule.Merge(individuals, offspring, simulation);
 }
Example #14
0
        public void TestSmallSingleList()
        {
            SortList <int> list = new SortList <int>();

            for (int i = 0; i < 20; i++)
            {
                list.Clear();
                for (int p = 0; p < i; p++)
                {
                    list.Add(Utility.RandomNum.RandomInt(0, 1000));
                }
                int last = Int32.MaxValue;
                for (int q = 0; q < i; q++)
                {
                    Assert.IsTrue(list.Get(q) <= last);
                    last = list.Get(q);
                }
            }
        }
Example #15
0
        public void TestBigSingleList()
        {
            SortList <int> list = new SortList <int>();

            for (int i = 0; i < 1000; i++)
            {
                list.Add(Utility.RandomNum.RandomInt(0, 1000));
            }
            Assert.AreEqual(1000, list.Count);
            int last = Int32.MaxValue;

            for (int i = 0; i < 1000; i++)
            {
                Assert.IsTrue(list.Get(i) <= last);
                last = list.Get(i);
            }
            list.Clear();
            Assert.AreEqual(list.Count, 0);
        }
        public async Task Init()
        {
            if (ZanrList.Count == 0)
            {
                SortList.Add("Naziv");
                SortList.Add("Najveća ocjena");

                var zanrList = await _zanrService.Get <List <Zanr> >(null);

                foreach (var zanr in zanrList)
                {
                    ZanrList.Add(zanr);
                }
            }

            PredstavaSearchRequest search = new PredstavaSearchRequest();

            if (SelectedZanr != null)
            {
                search.ZanrId = SelectedZanr.ZanrID;
            }

            if (int.TryParse(TrajanjeOd, out int _TrajanjeOd))
            {
                search.TrajanjeOd = _TrajanjeOd;
            }
            if (int.TryParse(TrajanjeDo, out int _TrajanjeDo))
            {
                search.TrajanjeDo = _TrajanjeDo;
            }

            search.Sort = SelectedSort;

            var list = await _predstaveService.Get <IEnumerable <Predstava> >(search);

            PredstaveList.Clear();
            foreach (var predstava in list)
            {
                PredstaveList.Add(predstava);
            }
        }
Example #17
0
        //- Actions
        private void LoadTabActions()
        {
            img_action_header.Source = Images.IconQuestionmark;

            tree_actions.Items.Clear();

            SortList <FPAction> legit = new SortList <FPAction>();

            foreach (Type action_type in Engine.Game.EveryAction)
            {
                object[] args      = new object[] { Engine.CurrentPerson, Person };
                FPAction fp_action = Engine.CreateClass <FPAction>(action_type, args);
                if (fp_action.Condition())
                {
                    legit.Add(fp_action, fp_action.IndexPosition);
                }
            }
            foreach (FPAction action in legit.Get())
            {
                AddAction(action);
            }
        }
Example #18
0
        /// <summary>
        /// 触发排序
        /// </summary>
        /// <param name="name">排序的表格名</param>
        /// <param name="sortName">排序的属性名</param>
        /// <returns></returns>
        protected virtual SortType ChickOrderBy(string name, string sortName)
        {
            SortList lstSort = new SortList();

            FillOrderBy(name, lstSort);


            Sort objSort = lstSort[sortName];

            if (objSort == null)
            {
                objSort = new Sort();
                objSort.PropertyName = sortName;
                objSort.SortType     = SortType.ASC;
                lstSort.Add(objSort);
            }
            else
            {
                if (objSort.SortType == SortType.ASC)
                {
                    objSort.SortType = SortType.DESC;
                }
                else if (objSort.SortType == SortType.DESC)
                {
                    for (int i = lstSort.Count - 1; i >= 0; i--)
                    {
                        if (lstSort[i].PropertyName == objSort.PropertyName)
                        {
                            lstSort.RemoveAt(i);
                        }
                    }
                }
            }
            SetOrderByString(name, lstSort);
            return(objSort.SortType);
        }
 public void Merge(SortList <AIPlayer> population, List <AIPlayer> offspring, Simulation simulation)
 {
     offspring.ForEach(p => population.Add(p));
 }
Example #20
0
 private void GetSortList()
 {
     SortList.Add("Actual Date");
     SortList.Add("Status");
     //await SubmissionDA.GetAllSubmissions();
 }
Example #21
0
        private async void LoadReferenceItems(
            string processName,
            string referenceField,
            string filterExpression,
            EditorCreatedEventArgs e,
            string displayField)
        {
            var context = DataContext as IFilterSetupViewModel;
            var isCurrentStateGuidFilter = false;
            if (context != null)
            {
                isCurrentStateGuidFilter = !context.IsUserFilter && (referenceField == Constants.CurrentStateColumnName)
                                           && (processName == context.ProcessSystemName);
            }

            var sortDescriptors = new SortList { new SortDescriptor(displayField) };

            //enforce SearchQueryGenerator to adjust Guid field into SQL query. required to find CurrentState by a guid. Admin filters use guid to identify States unlike UserFilters.
            if (isCurrentStateGuidFilter)
            {
                sortDescriptors.Add(new SortDescriptor("Guid", SortDirection.Ascending));
            }

            try
            {
                var infoList =
                    await
                    DynamicTypeManager.GetCrossReferenceListAsync<IReferenceItem>(
                        processName,
                        referenceField,
                        sortDescriptors,
                        null,
                        int.MaxValue,
                        0,
                        filterExpression);

                var comboBox = e.Editor.FindChildByType<RadComboBox>();
                if (comboBox == null)
                {
                    return;
                }

                DataFilter.Tag = true;
                try
                {
                    comboBox.ItemsSource = infoList;
                    comboBox.SelectedValuePath = isCurrentStateGuidFilter ? "Guid" : "Id";

                    Observable.FromEventPattern<SelectionChangedEventHandler, SelectionChangedEventArgs>(
                        handler => comboBox.SelectionChanged += handler,
                        handler => comboBox.SelectionChanged -= handler).SubscribeWeakly(this, OnComboBoxSelectionChanged);

                    var itemType = DynamicTypeManager.GetCrossReferenceItemType(processName, referenceField);
                    //comboBox.ItemTemplate = CreateTemplateForReferenceField(itemType, displayField);

                    var displayFieldList = GetDisplayFieldList(processName, referenceField);

                    comboBox.ItemTemplate = CreateTemplateForReferenceField(displayFieldList, itemType, displayField);
                    TextSearch.SetTextPath(comboBox, displayField);

                    var filterViewModel = (SimpleFilterViewModel)e.Editor.DataContext;
                    if (filterViewModel != null && filterViewModel.Descriptor != null)
                    {
                        var stateInfo = filterViewModel.Descriptor.Value as StateInfoClass;
                        if (stateInfo != null)
                        {
                            var placeholder = stateInfo;
                            comboBox.SelectedValue = placeholder.Guid.ToString();

                            //workaround - some state Guids are lower case and some upper
                            if (comboBox.SelectedValue == null)
                            {
                                comboBox.SelectedValue = placeholder.Guid.ToString().ToUpperInvariant();
                            }
                        }
                        else if (filterViewModel.Descriptor.Value is IInfoClass)
                        {
                            var placeholder = filterViewModel.Descriptor.Value as IInfoClass;
                            comboBox.SelectedValue = placeholder.Id;
                        }
                    }
                }
                finally
                {
                    DataFilter.Tag = null;
                }
            }
            catch (DataPortalException ex)
            {
                PopupFactory.NotifyFailure(ex);
            }
        }