Ejemplo n.º 1
0
 public void WpfCommand_DeleteAllConfigurations()
 {
     foreach (ProblemConfig pc in ProblemConfigs)
     {
         pc.Reset();
     }
     ProblemConfigs.Clear();
 }
Ejemplo n.º 2
0
        public void WpfCommand_GenerateConfigurations_AndValidateForm()
        {
            try
            {
                #region Validating Form
                // Validating the form - Do we have one for Objective Function?
                if (!AppSS.I.ProbQuantMgn.WpfProblemQuantities_ObjectiveFunction.OfType <ProblemQuantity>().Any())
                {
                    MessageBox.Show("You must add at least one quantity to be used as Objective Function.", "Objective Function", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // Validating the form - More than one Equality Constraints?
                if (AppSS.I.ProbQuantMgn.WpfProblemQuantities_Constraint.OfType <ProblemQuantity>().Count(a => a.IsConstraint && a.ConstraintObjective == Quantity_ConstraintObjectiveEnum.EqualTo) > 1)
                {
                    MessageBox.Show($"The maximum number of equal constraints is 1.{Environment.NewLine}Add higher than or lower than instead.", "Equality Constraints", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // Validating the form - Optimizer Type - Constraint Validity
                if (!AppSS.I.ProbQuantMgn.WpfProblemQuantities_Constraint.OfType <ProblemQuantity>().Any())
                {
                    if (AppSS.I.NlOptOpt.UseLagrangian)
                    {
                        AppSS.I.NlOptOpt.UseLagrangian = false; // Forces false.
                    }
                }
                else // We have constraints
                {
                    if (!AppSS.I.NlOptOpt.UseLagrangian)
                    {
                        // Checks if we have equality of inequality constraints
                        bool hasEqualConstraint   = false;
                        bool hasUnequalConstraint = false;

                        // Sets up the constraints
                        foreach (ProblemQuantity constraintQuantity in AppSS.I.ProbQuantMgn.WpfProblemQuantities_Constraint.OfType <ProblemQuantity>())
                        {
                            // Regardless of the constraint type, it will always point to the same function
                            switch (constraintQuantity.ConstraintObjective)
                            {
                            case Quantity_ConstraintObjectiveEnum.EqualTo:
                                hasEqualConstraint = true;
                                break;

                            case Quantity_ConstraintObjectiveEnum.HigherThanOrEqual:
                            case Quantity_ConstraintObjectiveEnum.LowerThanOrEqual:
                                hasUnequalConstraint = true;
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }

                        switch (AppSS.I.NlOptOpt.NlOptSolverType)
                        {
                        case NLoptAlgorithm.LN_COBYLA:    // Both
                        case NLoptAlgorithm.GN_ISRES:     // Both
                        case NLoptAlgorithm.LD_SLSQP:     // Both
                            // They are OK
                            break;

                        case NLoptAlgorithm.LD_CCSAQ:   // NE Only
                        case NLoptAlgorithm.GN_AGS:     // NE Only
                        case NLoptAlgorithm.LD_MMA:     // NE Only
                            MessageBox.Show($"{ListDescSH.I.NlOptAlgorithmEnumDescriptions[AppSS.I.NlOptOpt.NlOptSolverType]} does not support equality constraints by itself (only inequality). You can use Augmented Lagrangian option to embed the constraint into the objective function.", "Constraints", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;

                        default:
                            MessageBox.Show($"{ListDescSH.I.NlOptAlgorithmEnumDescriptions[AppSS.I.NlOptOpt.NlOptSolverType]} does not support any type of constraints by itself. You can use Augmented Lagrangian option to embed the constraint into the objective function.", "Constraints", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;
                        }
                    }
                }
                #endregion

                #region Generating the Problem Config List
                int problemConfigIndex = 0;

                // Combines the lists of lines and integers
                List <IProblemConfig_CombinableVariable> combVars = new List <IProblemConfig_CombinableVariable>();
                combVars.AddRange(AppSS.I.Gh_Alg.GeometryDefs_LineList_View.OfType <LineList_GhGeom_ParamDef>());
                combVars.AddRange(AppSS.I.Gh_Alg.ConfigDefs_Integer_View.OfType <Integer_GhConfig_ParamDef>());

                // Checks if all have at least one option
                foreach (IProblemConfig_CombinableVariable combVar in combVars)
                {
                    if (!combVar.FlatCombinationList.Any())
                    {
                        throw new InvalidOperationException($"All configuration variable must have at least one option.{Environment.NewLine}{combVar.Name} is lacking options.");
                    }
                }

                // Gets a List of possible values for each variable
                // Example: LineList 1 => FeSectionA, FeSectionB
                //          Integers => 1,2,3
                IEnumerable <IEnumerable <ProblemConfig_ElementCombinationValue> > possiblePerVariable = from a in combVars select a.FlatCombinationList;

                IEnumerable <IEnumerable <ProblemConfig_ElementCombinationValue> > allPossibleCombos = EmasaWPFLibraryStaticMethods.GetAllPossibleCombos(possiblePerVariable);

                // Gets the UNIQUE combinations - Heavily based on the Comparer found in ProblemConfig_ElementCombinationValue
                HashSet <List <ProblemConfig_ElementCombinationValue> > uniqueCombs = new HashSet <List <ProblemConfig_ElementCombinationValue> >(new ProblemConfig_ElementCombinationValue.ProblemConfig_ElementCombinationValue_ListComparer());
                foreach (IEnumerable <ProblemConfig_ElementCombinationValue> possibleCombo in allPossibleCombos)
                {
                    List <ProblemConfig_ElementCombinationValue> possibleComboAsList = possibleCombo.ToList();
                    possibleComboAsList.Sort(new ProblemConfig_ElementCombinationValue.ProblemConfig_ElementCombinationValue_Comparer());
                    uniqueCombs.Add(possibleComboAsList);
                }

                // Adds the problem configs to the list
                ProblemConfigs.AddRange(uniqueCombs.Select(comb => new ProblemConfig(comb, problemConfigIndex++)));
                #endregion
            }
            catch (Exception e)
            {
                ExceptionViewer.Show(e);
            }
        }