public static bool IsExpansionHighEnough(int depth, Blade currentStator)
        {
            double bestCurrentEfficiency = 0;
            double bestLaterEfficiency   = 0;

            for (int blade = 0; blade < blades.Count; blade++)
            {
                Blade actualBlade = blades[blade];
                if (actualBlade.isStator)
                {
                    continue;
                }
                double currentCoefficientBizarre = Average(currentCoefficients[depth - 1], currentCoefficients[depth - 1] * actualBlade.sqrtCoefficient);
                double currentEfficiency         = Ratio(currentCoefficientBizarre, targets[depth]) * actualBlade.efficiency;
                if (bestCurrentEfficiency < currentEfficiency)
                {
                    bestCurrentEfficiency = currentEfficiency;
                }
                double laterCoefficientBizarre = Average(currentCoefficients[depth - 1] * currentStator.coefficient, currentCoefficients[depth - 1] * currentStator.coefficient * actualBlade.sqrtCoefficient);
                double laterEfficiency         = Ratio(laterCoefficientBizarre, targets[depth + 1]) * actualBlade.efficiency;
                if (bestLaterEfficiency < laterEfficiency)
                {
                    bestLaterEfficiency = laterEfficiency;
                }
            }
            return(bestLaterEfficiency > bestCurrentEfficiency);
        }
 private void newBTN_Click(object sender, EventArgs e)
 {
     beingEdited = new Blade("New Blade", 1, 1, false, true);
     editForm    = new EditBladeForm(beingEdited, true);
     editForm.Show();
     this.Enabled          = false;
     editForm.FormClosing += EditForm_FormClosing;
 }
 private void editBTN_Click(object sender, EventArgs e)
 {
     beingEdited = usedList.SelectedObject != null ? (Blade)usedList.SelectedObject : (Blade)availableList.SelectedObject;
     editForm    = new EditBladeForm(beingEdited, false);
     editForm.Show();
     this.Enabled          = false;
     editForm.FormClosing += EditForm_FormClosing;
 }
 public EditBladeForm(Blade blade, bool newBlade)
 {
     this.newBlade = newBlade;
     InitializeComponent();
     nameBox.Text           = blade.name;
     efficiencyBox.Value    = (decimal)blade.efficiency;
     coefficientBox.Value   = (decimal)blade.coefficient;
     statorCheckbox.Checked = blade.isStator;
 }
        private void removeBTN_Click(object sender, EventArgs e)
        {
            Blade toRemove = (Blade)usedList.SelectedObject;

            blades.Remove(toRemove);
            availableBlades.Add(toRemove);
            usedList.BuildList();
            availableList.BuildList();
            deleteBTN.Enabled = removeBTN.Enabled = editBTN.Enabled = false;
        }
        public TurbineCalculator()
        {
            instance = this;
            InitializeComponent();
            fuelList.SelectedIndex = 0;
            overlay = new Overlay();
            overlay.Abort_Clicked += new EventHandler(Abort);
            toolTip.SetToolTip(runBTN, "Retrieves the optimal turbine configuration for the chosen length");
            toolTip.SetToolTip(runAllBTN, "Retrieves the optimal turbine configuration amongst all lengths up to the chosen number. Can be pretty slow!!!");

            LoadSettings();

            usedList.SetObjects(blades);
            availableList.SetObjects(availableBlades);

            currentBlades              = new Blade[maxLength];
            bestBlades                 = new Blade[maxLength];
            targets                    = new double[maxLength];
            currentCoefficients        = new double[maxLength];
            currentCoefficientsBizarre = new double[maxLength];
            currentEfficiencySum       = new double[maxLength];
            rotors = new int[maxLength];
        }
        public static void RecursiveCheck(int depth, int length)
        {
            if (cancel.IsCancellationRequested)
            {
                return;
            }
            for (int blade = 0; blade < blades.Count; blade++)
            {
                Blade actualBlade = blades[blade];
                currentBlades[depth] = actualBlade;

                if (depth == 0)
                {
                    currentCoefficients[0]        = actualBlade.coefficient;
                    currentCoefficientsBizarre[0] = actualBlade.sqrtCoefficient;
                    if (!actualBlade.isStator)
                    {
                        currentEfficiencySum[0] = Ratio(currentCoefficientsBizarre[0], targets[0]) * actualBlade.efficiency;
                        rotors[0] = 1;
                    }
                    else
                    {
                        rotors[0] = 0;
                        currentEfficiencySum[0] = 0;
                    }
                }
                else
                {
                    currentCoefficients[depth]        = currentCoefficients[depth - 1] * actualBlade.coefficient;
                    currentCoefficientsBizarre[depth] = currentCoefficients[depth - 1] * actualBlade.sqrtCoefficient;
                    if (!actualBlade.isStator)
                    {
                        if (targets[depth] < currentCoefficientsBizarre[depth - 1])
                        {
                            continue;
                        }
                        currentEfficiencySum[depth] = currentEfficiencySum[depth - 1] +
                                                      (Ratio(currentCoefficientsBizarre[depth], targets[depth]) * actualBlade.efficiency);
                        rotors[depth] = rotors[depth - 1] + 1;
                    }
                    else
                    {
                        if (targets[depth] > currentCoefficientsBizarre[depth - 1] &&
                            depth + 1 < length && !IsExpansionHighEnough(depth, actualBlade))
                        {
                            continue;
                        }
                        rotors[depth] = rotors[depth - 1];
                        currentEfficiencySum[depth] = currentEfficiencySum[depth - 1];
                    }
                }

                if (!IsEfficiencyGoodEnough(depth, length))
                {
                    continue;
                }

                if (depth + 1 < length)
                {
                    RecursiveCheck(depth + 1, length);
                }
                else   //last segment
                {
                    double averageEfficiency    = currentEfficiencySum[depth] / rotors[depth];
                    double expansionCoefficient = Ratio(currentCoefficients[depth], fuelExpansion);
                    double finalEfficiency      = averageEfficiency * expansionCoefficient;
                    if (finalEfficiency > bestEfficiency)
                    {
                        bestEfficiency  = finalEfficiency;
                        bestExpansion   = currentCoefficients[depth] / fuelExpansion;
                        bestLengthOfAll = length;
                        for (int x = 0; x < length; x++)
                        {
                            bestBlades[x] = currentBlades[x];
                        }
                    }
                }
            }
        }