public override void Evolve()
        {
            int individualsToEliminate = (int)(_generationGap * _populationSize);

            // A single generation is when GenerationGap % of population is eliminated and replaced
            IList <T> offspringPopulation = new List <T>();

            while (offspringPopulation.Count < individualsToEliminate)
            {
                IList <T> matingPool = new List <T>(CrossoverOperator.NumberOfParents);
                for (int j = 0; j < CrossoverOperator.NumberOfParents; j++)
                {
                    T mate = SelectionOperator.Execute(_population);
                    matingPool.Add(mate);
                }

                IList <T> children = CrossoverOperator.Execute(matingPool);

                for (int j = 0; j < children.Count; j++)
                {
                    MutationOperator.Execute(children[j]);
                    _problem.Evaluate(children[j]);
                    offspringPopulation.Add(children[j]);
                }
            }

            offspringPopulation = offspringPopulation.Take(individualsToEliminate).ToList();

            _population = ReplacementOperator.Execute(_population, offspringPopulation);

            UpdateState();
        }
Beispiel #2
0
        private void setupSelectionButton(SelectionOperator mode, String name)
        {
            Button selectionButton = (Button)widget.findWidget(name);

            selectionOperators.addButton(mode, selectionButton);
            selectionButton.NeedToolTip   = true;
            selectionButton.EventToolTip += selectionButton_EventToolTip;
        }
Beispiel #3
0
 void anatomyController_SelectionOperatorChanged(AnatomyController source, SelectionOperator arg)
 {
     if (allowSelectionModeChanges)
     {
         allowSelectionModeChanges    = false;
         selectionOperators.Selection = arg;
         allowSelectionModeChanges    = true;
     }
 }
Beispiel #4
0
        private string GetOptionSet(string str_in, string str_fname, string str_fvalue)
        {
            string       str2    = "";
            OperatorType unknown = OperatorType.Unknown;
            ArrayList    list    = new ArrayList();

            foreach (PLMOperator @operator in SelectionOperator.GetOperator(this.curFieldAttr.DataType2))
            {
                if (str_in.Equals(@operator.OperName))
                {
                    str2    = str_fvalue;
                    unknown = @operator.OperType;
                }
            }
            switch (unknown)
            {
            case OperatorType.Equal:
                if ((this.curFieldAttr.DataType2 != PLMDataType.String) && (this.curFieldAttr.DataType2 != PLMDataType.Char))
                {
                    return(str_fname + " = " + str2);
                }
                return(str_fname + " = '" + str2 + "'");

            case OperatorType.NotEqual:
                return(str_fname + " <> " + str2);

            case OperatorType.LikeLeft:
                return(str_fname + " like '" + str2 + "*'");

            case OperatorType.Like:
                return(str_fname + " like '*" + str2 + "*'");

            case OperatorType.LikeRight:
                return(str_fname + " like '*" + str2 + "'");

            case OperatorType.Greater:
                return(str_fname + " > " + str2);

            case OperatorType.Less:
                return(str_fname + " < " + str2);

            case OperatorType.GreaterEqual:
                return(str_fname + " >= " + str2);

            case OperatorType.LessEqual:
                return(str_fname + " <= " + str2);
            }
            if ((this.curFieldAttr.DataType2 == PLMDataType.String) || (this.curFieldAttr.DataType2 == PLMDataType.Char))
            {
                return(str_fname + " = '" + str2 + "'");
            }
            return(str_fname + " = " + str2);
        }
Beispiel #5
0
        private void AddOperLst(DEMetaAttribute attr)
        {
            int       num  = 0;
            ArrayList list = new ArrayList();

            foreach (PLMOperator @operator in SelectionOperator.GetOperator(attr.DataType2))
            {
                if (num == 0)
                {
                    this.cmBox_oper.Text = @operator.OperName;
                }
                this.cmBox_oper.Properties.Items.Add(@operator.OperName);
                num++;
            }
        }
        // TODO -> Find different ways to create initial population
        // TODO-> always add in next gen
        private void CreateInitialPopulation()
        {
            var citiesSet = _tspInstance.CitiesSet;

            if (citiesSet == null)
            {
                throw new Exception("Cities cannot be null");
            }

            if (citiesSet.Count < 1)
            {
                throw new Exception("Cities must at least have a length of 3.");
            }

            // copying cities into an array starting from index 1
            var cities   = citiesSet.Keys.ToArray(); // TODO -> Should I have an array in tsp instance instead ???  to avoid to array ?
            var elements = new int[cities.Length - 1];

            for (int i = 1; i < cities.Length; i++)
            {
                elements[i - 1] = cities[i];
            }

            for (int i = 0; i < Population.PopulationSize; i++)
            {
                // fisher-yates-shuffle O(n)
                for (int j = elements.Length - 1; j > 0; j--)
                {
                    var swapIndex = Random.Next(j + 1);
                    var tmp       = elements[j];
                    elements[j]         = elements[swapIndex];
                    elements[swapIndex] = tmp;
                }

                // adding to population
                var chromosome = new Chromosome <int>(elements);
                CalculateFitness(chromosome);

                Population.AddChromosome(i, chromosome);
            }

            // prepare for next evolution
            Population.SetNextGeneration();
            SelectionOperator.SetNextGeneration();
        }
        void anatomyController_SelectionOperatorChanged(AnatomyController source, SelectionOperator arg)
        {
            switch (arg)
            {
            case SelectionOperator.Select:
                IconName = "AnatomyFinder.Select";
                break;

            case SelectionOperator.Add:
                IconName = "AnatomyFinder.Add";
                break;

            case SelectionOperator.Remove:
                IconName = "AnatomyFinder.Remove";
                break;
            }
            fireIconChanged();
        }
        private void CreateNextGeneration()
        {
            var startingIndex = UsingElitism ? NumberOfElite : 0;

            if (UsingElitism)
            {
                Population.SortByFitness();
                for (int i = 0; i < NumberOfElite; i++)
                {
                    Population.AddChromosome(i, Population.Chromosomes[i]);
                }

                startingIndex = NumberOfElite;
            }

            var populationSize = Population.PopulationSize;

            for (int i = startingIndex; i < populationSize; i++)
            {
                // TODO -> Should I make sure that both parents are different ??

                // selection operation
                var fatherChromosome = SelectionOperator.PopulationSelection(Population);
                var motherChromosome = SelectionOperator.PopulationSelection(Population);

                // TODO -> return more than one child always!!!
                // crossover operation
                var childChromosome = CrossoverOperator.Crossover(fatherChromosome, motherChromosome); // TODO -> some crossovers can return more than one child

                // mutate operation
                MutationOperator.Mutate(childChromosome);

                // add child chromsome to the next generation population
                CalculateFitness(childChromosome);
                Population.AddChromosome(i, childChromosome);
            }

            // prepare for next evolution
            Population.SetNextGeneration();
            SelectionOperator.SetNextGeneration();

            Generation++;
        }
Beispiel #9
0
        public override void Evolve()
        {
            IList <T> offspringPopulation = new List <T>();

            // Let elites survive to next generation
            int       numberOfElites = (int)(_elitismRate * _populationSize);
            IList <T> elites         = _population
                                       .OrderByDescending(c => c, _problem.FitnessComparer)
                                       .Take(numberOfElites).ToList();

            foreach (T individual in elites)
            {
                offspringPopulation.Add(individual);
            }

            while (offspringPopulation.Count < _populationSize)
            {
                IList <T> matingPool = new List <T>(CrossoverOperator.NumberOfParents);
                for (int i = 0; i < CrossoverOperator.NumberOfParents; i++)
                {
                    matingPool.Add(SelectionOperator.Execute(_population));
                }

                IList <T> children = CrossoverOperator.Execute(matingPool);

                for (int i = 0; i < children.Count; i++)
                {
                    MutationOperator.Execute(children[i]);
                    _problem.Evaluate(children[i]);
                    offspringPopulation.Add(children[i]);
                }
            }

            // When there is some excess chromosomes, discard them by taking only the needed ones
            _population = offspringPopulation.Take(_populationSize).ToList();

            UpdateState();
        }
        /// <summary>
        /// Sets the options and other configuration used in the GA.
        /// </summary>
        private void ApplyOptions()
        {
            /*** setting the selection operator to the specified type ***/
            var selectionType = GaOptions.SelectionType;

            if (selectionType == SelectionType.None)
            {
                throw new Exception("Selection method cannot be none when setting the method.");
            }

            var populationSize = Population.PopulationSize;

            switch (selectionType)
            {
            case SelectionType.Rws:
                SelectionOperator = new RouletteWheelSelection <T>(populationSize, Random);
                break;

            case SelectionType.Tos:
                // the tournamnet selection percentage is hardcoded for now to 2%, TODO => this must be passed in the GA Options
                SelectionOperator = new TournamentSelection <T>(populationSize, Random, 0.02);
                break;
            }
            /*** setting the selection operator to the specified type ***/

            /*** setting the crossover operator to the specified type ***/
            var crossoverType = GaOptions.CrossoverType;

            if (crossoverType == CrossoverType.None)
            {
                throw new Exception("Crossover method cannot be none when setting the method.");
            }

            switch (crossoverType)
            {
            case CrossoverType.OrderOne:
                CrossoverOperator = new OrderOne <T>(Random);
                break;

            case CrossoverType.Cycle:
                CrossoverOperator = new CycleCrossover <T>(Random);
                break;
            }
            /*** setting the crossover operator to the specified type ***/

            /*** setting the mutation operator to the specified type ***/
            var mutationType = GaOptions.MutationType;

            if (mutationType == MutationType.None)
            {
                throw new Exception("Mutation method cannot be none when setting the method.");
            }

            switch (mutationType)
            {
            case MutationType.SingleSwap:
                MutationOperator = new SingleSwapMutation <T>(Random);
                break;

            case MutationType.InversionMutation:
                MutationOperator = new InversionMutation <T>(Random, GaOptions.MutationRate);
                break;
            }
            /*** setting the mutation operator to the specified type ***/

            /*** setting the stopping criteria for the algorithm ***/
            var stoppingCriteriaOptions = GaOptions.StoppingCriteriaOptions;
            var stoppingCriteriaType    = stoppingCriteriaOptions.StoppingCriteriaType;

            switch (stoppingCriteriaType)
            {
            case StoppingCriteriaType.TimeBased:
                var minutesPassed = stoppingCriteriaOptions.MinutesPassed;
                if (minutesPassed <= 0)
                {
                    throw new Exception($"When using {stoppingCriteriaType}, the minutes passed must be larger than 0.");
                }
                StoppingCriteria = new TimeBaseStoppingCriteria(minutesPassed);
                break;

            case StoppingCriteriaType.SpecifiedIterations:
                var maximumIterations = stoppingCriteriaOptions.MaximumIterations;
                if (maximumIterations <= 0)
                {
                    throw new Exception($"When using {stoppingCriteriaType}, the max iterations must be larger than 0.");
                }
                StoppingCriteria = new IterationStoppingCriteria(maximumIterations);
                break;
            }

            /*** setting the stopping criteria for the algorithm ***/
        }
Beispiel #11
0
        private void button9_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count > 0)
            {
                dataGridView1.Visible = true;
                dataGridView1.Columns.Clear();
                dataGridView1.Rows.Clear();
                dataGridView1.AllowUserToAddRows    = false;
                dataGridView1.AllowUserToDeleteRows = false;
                List <string>            selectedlist = new List <string>();
                List <SelectionOperator> operators    = new List <SelectionOperator>();
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    SelectionOperator op = new SelectionOperator();
                    if (dataGridView2.Rows[i].Cells[0].Value != null)
                    {
                        op.TabCol = dataGridView2.Rows[i].Cells[0].Value.ToString();
                        op.Type   = DataLayer.Configuration.GetTableColumns(comboBox1.SelectedItem.ToString(), op.TabCol.Split('.')[0]).Where(x => x.ColumnName == op.TabCol.Split('.')[1]).FirstOrDefault().Type;
                    }
                    if (dataGridView2.Rows[i].Cells[1].Value != null)
                    {
                        op.Operator = dataGridView2.Rows[i].Cells[1].Value.ToString();
                    }
                    if (dataGridView2.Rows[i].Cells[2].Value != null)
                    {
                        op.Value = dataGridView2.Rows[i].Cells[2].Value.ToString();
                    }


                    if (op.TabCol != null && op.Operator != null && op.Value != null)
                    {
                        operators.Add(op);
                    }
                }


                foreach (var item in listBox2.Items)
                {
                    selectedlist.Add(item.ToString());
                }
                List <string> selectedtables = new List <string>();
                foreach (string str in selectedlist)
                {
                    string[] s = str.Split('.');
                    selectedtables.Add(s[0]);
                }
                selectedtables = selectedtables.Distinct().ToList();
                List <KeyValue> colIndex = Utils.Utilities.GetColumnIndex(selectedlist, comboBox1.SelectedItem.ToString());
                if (colIndex.Count > 0)
                {
                    List <string> result = null;
                    if (selectedtables.Count > 1)
                    {
                        result = DataLayer.DataAccess.GetKeyValuesIndexJoin(comboBox1.SelectedItem.ToString(), colIndex, false, operators);
                    }
                    else
                    {
                        return;
                    }
                    for (int i = 0; i < selectedlist.Count; i++)
                    {
                        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                        column.HeaderText = selectedlist[i];
                        column.Name       = selectedlist[i];
                        dataGridView1.Columns.Add(column);
                    }
                    for (int i = 0; i < result.Count; i++)
                    {
                        dataGridView1.Rows.Add();
                        for (int j = 0; j < selectedlist.Count; j++)
                        {
                            dataGridView1.Rows[i].Cells[j].Value = GetColumnValue(result[i], j);
                        }
                    }
                }
            }
        }
Beispiel #12
0
 public void Selection()
 {
     this.population = SelectionOperator.Selection(this); //new List<Chromosome>();
 }