private void SourceChanged(object sender, EventArgs e)
        {
            // Null Check
            if (AirfoilOptimizationResource.Instance.BasisAirfoils == null)
            {
                return;
            }
            if (AirfoilOptimizationResource.Instance.CurrentCoefficients == null)
            {
                return;
            }

            // Update baseAirfoil
            this.basisAirfoils = General.BasisAirfoils.Convert(AirfoilOptimizationResource.Instance.BasisAirfoils);

            // Update coefficients
            this.coefficients = AirfoilOptimizationResource.Instance.CurrentCoefficients.GetCoefficientArray() as Double[, ];

            // Re-combinate Airfoil
            this.currentPopulations = AirfoilOptimizationResource.Instance.CurrentPopulations;
            // Re-set current offsprings
            this.offspringAirfoilCandidates = AirfoilOptimizationResource.Instance.OffspringCandidates;

            // Re-generate the coordinates for airfoil previewing.
            ReDrawPreviewWindow();
        }
        public void StartCrossover(Airfoil.CombinedAirfoilsGroup parents)
        {
            parentAirfoils = parents;
            var parentsAirfoilsArray = parentAirfoils.CombinedAirfoils;

            // Initialize Basis airfoils
            basisAirfoils = new General.BasisAirfoils(parentAirfoils.BasisAirfoils.AirfoilGroup);

            // Execute Crossover
            crossoverExecutor.ExecuteCrossover(parentAirfoils.CoefficientOfCombination);

            // Read Offsprings' optimization parameters
            var optParams = crossoverExecutor.OptimizationParamters;

            optParams = SwapJuggedArray(optParams);
            var optCoef = new Airfoil.CoefficientOfCombination(General.ArrayManager.ConvertJuggedArrayToArray(optParams));

            // Assign Selected Parents Index
            parentsIndex = crossoverExecutor.ParentsIndex;

            // Initialize each fields
            offspringAirfoilsCombiner = new Airfoil.CombinedAirfoilsGroup(basisAirfoils);
            Airfoil.AirfoilsMixer airfoilsMixer = new Airfoil.AirfoilsMixer(basisAirfoils, optCoef);

            // Create Offspring Airfoils
            // Combine airfoils
            airfoilsMixer.CombineAirfoils();

            // Store combined airfoils into CombinedAirfoilsGroup class
            offspringAirfoilsCombiner.AddRange(airfoilsMixer.CombinedAirfoils);
        }
        private Airfoil.CombinedAirfoilsGroup CreateOffspringAirfoils(Airfoil.CoefficientOfCombination optParams)
        {
            Airfoil.CombinedAirfoilsGroup offsprings    = new Airfoil.CombinedAirfoilsGroup(basisAirfoils);
            Airfoil.AirfoilsMixer         airfoilsMixer = new Airfoil.AirfoilsMixer(basisAirfoils, optParams);

            airfoilsMixer.CombineAirfoils();
            offspringAirfoils.AddRange(airfoilsMixer.CombinedAirfoils);

            return(offsprings);
        }
        private void OffspringAirfoilsReady(object sender, EventArgs e)
        {
            // Pull offspringAirfoils from OptConfig
            this.offspringAirfoilCandidates = AirfoilOptimizationResource.Instance.OffspringCandidates;

            // Re-generate the coordinates for airfoil previewing.
            ReDrawPreviewWindow();

            // Debug Only ============================================
            //UpdateAirfoilPreviews(offspringAirfoilCandidates);
            // =======================================================
        }
        public void StartSelection(Airfoil.CombinedAirfoilsGroup offsprings)
        {
            // Initialize Basis airfoils
            if (basisAirfoils == null)
            {
                basisAirfoils = new General.BasisAirfoils(parentAirfoils.BasisAirfoils.AirfoilGroup);
            }

            // Executes selection to extract airfoil from offsprings
            var selectedParents = new Airfoil.CombinedAirfoilsGroup(parentAirfoils.BasisAirfoils);

            foreach (var item in parentsIndex)
            {
                selectedParents.Add(
                    parentAirfoils.CombinedAirfoils[item],
                    parentAirfoils.CoefficientOfCombination.GetCoefficients(item));
            }

            selectionExecutor.ExecuteSelection(offsprings + selectedParents);

            // Extract selected offsprings
            SelectedOffspringsNo = selectionExecutor.SelectedAirfoilsNo;
            var selectedAirfoils = selectionExecutor.SelectedAirfoils;

            offspringAirfoils = new Airfoil.CombinedAirfoilsGroup(basisAirfoils);
            for (int i = 0; i < selectedAirfoils.CombinedAirfoils.Length; i++)
            {
                var airfoil      = selectedAirfoils.CombinedAirfoils[i];
                var coefficients = selectedAirfoils.CoefficientOfCombination.GetCoefficients(i);

                offspringAirfoils.Add(airfoil, coefficients);
            }

            // Create next Generation
            int k           = 0;
            var previousGen = parentAirfoils.CombinedAirfoils;

            Airfoil.CombinedAirfoilsGroup nextGenerations = new Airfoil.CombinedAirfoilsGroup(basisAirfoils);
            for (int i = 0; i < previousGen.Length; i++)
            {
                if (IsEqual(i, parentsIndex))
                {
                    var airfoil      = selectedAirfoils.CombinedAirfoils[k];
                    var coefficients = selectedAirfoils.CoefficientOfCombination.GetCoefficients(k);

                    nextGenerations.Add(airfoil, coefficients);
                    ++k;
                }
                else
                {
                    var airfoil      = parentAirfoils.CombinedAirfoils[i];
                    var coefficients = parentAirfoils.CoefficientOfCombination.GetCoefficients(i);

                    nextGenerations.Add(airfoil, coefficients);
                }
            }
            nextAirfoilGenerations = nextGenerations;

            // newest virsion

            /*
             * var nextGenerations = new Airfoil.CombinedAirfoilGroup();
             * for (int i = 0; i < previousGen.Length; i++)
             * {
             *  if (IsEqual(i, parentsIndex))
             *  {
             *      nextGenerations.Add(selectedAirfoils[k].CombinedAirfoil);
             ++k;
             *  }
             *  else
             *  {
             *      nextGenerations.Add(previousGen[i].CombinedAirfoil);
             *  }
             * }
             */
        }
 /// <summary>
 /// Initialize without crossover procedure
 /// </summary>
 /// <param name="parentsIndex"></param>
 public AirfoilGAManager(int[] parentsIndex, Airfoil.CombinedAirfoilsGroup parentAirfoils) : this()
 {
     this.parentsIndex   = parentsIndex;
     this.parentAirfoils = parentAirfoils;
 }