Exemple #1
0
        void AddVariable(DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariable newiv, StackLayout ivcontainer, List <string> objs)
        {
            Flowsheet fsheet = (Flowsheet)flowsheet;

            var su = flowsheet.FlowsheetOptions.SelectedUnitSystem;
            var nf = flowsheet.FlowsheetOptions.NumberFormat;

            List <string> proplist  = new List <string>();
            List <string> proplist2 = new List <string>();

            proplist.Add("");
            proplist2.Add("");

            var container = new DynamicLayout {
                Padding = new Padding(5), Width = 300
            };

            //container.BackgroundColor = Eto.Drawing.Colors.White;
            container.Tag = new Random().Next() + mycase.variables.Count + 1;

            var slcontainer = new StackLayoutItem(container);

            s.CreateAndAddLabelRow(container, "Optimization Variable #" + (ivcontainer.Items.Count + 1).ToString());

            var btnremove = s.CreateAndAddButtonRow(container, "Remove Variable", null, (arg1a, arg2a) =>
            {
                mycase.variables.Remove(newiv.id);
                ivcontainer.Items.Remove(slcontainer);
            });

            s.CreateAndAddLabelRow(container, "Definitions");

            s.CreateAndAddStringEditorRow(container, "Name", newiv.name, (arg3b, arg2b) =>
            {
                newiv.name = arg3b.Text;
            });

            s.CreateAndAddDropDownRow(container, "Type", new[] { "DEP", "IND", "AUX", "CON" }.ToList(), (int)newiv.type, (arg3, arg2) => { newiv.type = (DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariableType)arg3.SelectedIndex; });

            DropDown spinprop = null;

            int idx = 0;

            if (flowsheet.SimulationObjects.ContainsKey(newiv.objectID))
            {
                idx = objs.IndexOf(flowsheet.SimulationObjects[newiv.objectID].GraphicObject.Tag);
            }

            s.CreateAndAddLabelRow(container, "Flowsheet Object Property Link");

            s.CreateAndAddDropDownRow(container, "Object", objs, idx, (arg3c, arg2c) =>
            {
                if (arg3c.SelectedIndex > 0)
                {
                    var obj        = flowsheet.GetFlowsheetSimulationObject(objs[arg3c.SelectedIndex]);
                    newiv.objectID = obj.Name;
                    if (newiv.type == DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariableType.Independent)
                    {
                        proplist = obj.GetProperties(PropertyType.WR).ToList();
                    }
                    else
                    {
                        proplist = obj.GetProperties(PropertyType.ALL).ToList();
                    }
                    proplist.Insert(0, "");
                    proplist2 = proplist.Select(x => flowsheet.GetTranslatedString(x)).ToList();

                    spinprop.Items.Clear();
                    spinprop.Items.AddRange(proplist2.Select((x) => new ListItem {
                        Key = x, Text = x
                    }));
                }
            });

            int idx2 = 0;

            if (flowsheet.SimulationObjects.ContainsKey(newiv.objectID))
            {
                var obj = flowsheet.SimulationObjects[newiv.objectID];
                if (newiv.type == DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariableType.Independent)
                {
                    proplist = obj.GetProperties(PropertyType.WR).ToList();
                }
                else
                {
                    proplist = obj.GetProperties(PropertyType.ALL).ToList();
                }
                proplist.Insert(0, "");
                proplist2 = proplist.Select(x => flowsheet.GetTranslatedString(x)).ToList();
                idx2      = proplist.IndexOf(newiv.propID);
            }

            TextBox tviv = null;

            spinprop = s.CreateAndAddDropDownRow(container, "Property", proplist2, idx2, (arg3c, arg2c) =>
            {
                if (spinprop.SelectedIndex > 0)
                {
                    newiv.propID       = proplist[arg3c.SelectedIndex];
                    newiv.unit         = fsheet.SimulationObjects[newiv.objectID].GetPropertyUnit(newiv.propID, su);
                    newiv.initialvalue = (double)fsheet.SimulationObjects[newiv.objectID].GetPropertyValue(newiv.propID, su);
                    tviv.Text          = newiv.initialvalue.ToString(nf);
                }
            });

            s.CreateAndAddLabelRow(container, "Variable Bounds");
            s.CreateAndAddDescriptionRow(container, "Bounds will be taken into account only if you use a bounded solver.");
            s.CreateAndAddTextBoxRow(container, nf, "Lower Bound", newiv.lowerlimit.GetValueOrDefault(), (arg3, arg2) => { newiv.lowerlimit = arg3.Text.ToDoubleFromCurrent(); });
            s.CreateAndAddTextBoxRow(container, nf, "Upper Bound", newiv.upperlimit.GetValueOrDefault(), (arg3, arg2) => { newiv.upperlimit = arg3.Text.ToDoubleFromCurrent(); });

            s.CreateAndAddLabelRow(container, "Initial Value");
            s.CreateAndAddDescriptionRow(container, "Set the initial value of the variable (IND only, other variable types will have their values read from the flowsheet).");
            tviv = s.CreateAndAddTextBoxRow(container, nf, "Initial Value", newiv.initialvalue, (arg3, arg2) => { newiv.initialvalue = arg3.Text.ToDoubleFromCurrent(); });

            ivcontainer.Items.Add(slcontainer);
        }
Exemple #2
0
        void Init()
        {
            DynamicLayout p1, p2;

            StackLayout t1;

            p1 = UI.Shared.Common.GetDefaultContainer();
            p2 = UI.Shared.Common.GetDefaultContainer();

            p1.Width = 380;
            p2.Width = 380;

            p1.Height = 540;

            t1 = new StackLayout();
            t1.Items.Add(new StackLayoutItem(p1));
            t1.Items.Add(new StackLayoutItem(p2));
            t1.Orientation = Orientation.Horizontal;

            Padding = new Padding(10);

            if (flowsheet.OptimizationCollection.Count == 0)
            {
                flowsheet.OptimizationCollection.Add(new DWSIM.SharedClasses.Flowsheet.Optimization.OptimizationCase());
            }

            mycase = flowsheet.OptimizationCollection.First();

            var su = flowsheet.FlowsheetOptions.SelectedUnitSystem;
            var nf = flowsheet.FlowsheetOptions.NumberFormat;

            var objlist = flowsheet.SimulationObjects.Values.Select((x2) => x2.GraphicObject.Tag).ToList();

            objlist.Insert(0, "");

            s.CreateAndAddLabelRow2(this, "Use the optimizer to bring the flowsheet to a new 'state' regarding the maximum or minimum or value of a user-defined parameter.");

            this.Add(t1);

            s.CreateAndAddLabelRow(p1, "Case ID");
            s.CreateAndAddFullTextBoxRow(p1, mycase.name, (arg3, arg2) => { mycase.name = arg3.Text; });

            s.CreateAndAddLabelRow(p1, "Case Description");
            s.CreateAndAddFullTextBoxRow(p1, mycase.description, (arg3, arg2) => { mycase.description = arg3.Text; });

            s.CreateAndAddDropDownRow(p1, "Optimization Type", new[] { "Minimization", "Maximization" }.ToList(), (int)mycase.type, (arg3, arg2) => { mycase.type = (DWSIM.SharedClasses.Flowsheet.Optimization.OPTType)arg3.SelectedIndex; });
            s.CreateAndAddDropDownRow(p1, "Objective Function", new[] { "Variable", "Expression" }.ToList(), (int)mycase.objfunctype, (arg3, arg2) => { mycase.objfunctype = (DWSIM.SharedClasses.Flowsheet.Optimization.OPTObjectiveFunctionType)arg3.SelectedIndex; });

            s.CreateAndAddTextBoxRow(p1, nf, "Maximum Iterations", (double)mycase.maxits, (arg3, arg2) => { mycase.maxits = (int)arg3.Text.ToDoubleFromCurrent(); });
            s.CreateAndAddTextBoxRow(p1, nf, "Absolute Tolerance", mycase.tolerance, (arg3, arg2) => { mycase.tolerance = arg3.Text.ToDoubleFromCurrent(); });

            var varcontainer = new StackLayout {
                Orientation = Orientation.Horizontal, Padding = new Eto.Drawing.Padding(0), Spacing = 10
            };

            var btnAddVar = s.CreateAndAddBoldLabelAndButtonRow(p2, "Variables", "Add Variable", null, (arg1, arg2) =>
            {
                var newiv  = new DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariable();
                newiv.id   = new Random().Next().ToString();
                newiv.name = "var" + mycase.variables.Count.ToString();
                mycase.variables.Add(newiv.id, newiv);

                AddVariable(newiv, varcontainer, objlist);
            });

            var desclbl = s.CreateAndAddDescriptionRow(p2, "Variables can be 'INDependent' (actual variables), 'AUXiliary' " +
                                                       "(for usage as a parameter in the objective function expression, if applicable), " +
                                                       "'DEPendent' (the parameter to be minimized/maximized, if applicable) or CONstraint, " +
                                                       "if you don want it to go beyond the defined limits at the solution.");

            var sc = new Scrollable {
                Border = BorderType.None, Content = varcontainer
            };

            s.CreateAndAddControlRow(p2, sc);

            //t1.SizeChanged += (sender, e) =>
            //{
            //    if (p1.ParentWindow != null)
            //    {
            //        p1.Width = (int)(p1.ParentWindow.Width / 2 - 10);
            //        p2.Width = (int)(p2.ParentWindow.Width / 2 - 10);
            //        p1.Height = p1.ParentWindow.Height - 140;
            //        p2.Height = p1.Height;
            //        sc.Height = p2.Height - btnAddVar.Height - 35 - desclbl.Height;
            //        foreach (var item in varcontainer.Items)
            //        {
            //            item.Control.Width = sc.Width - 25;
            //        }
            //    }
            //};

            foreach (var item in mycase.variables.Values)
            {
                AddVariable(item, varcontainer, objlist);
            }

            s.CreateAndAddLabelRow(p1, "Expression");
            s.CreateAndAddDescriptionRow(p1, "Enter an expression to be minimized or maximized using IND and AUX variables as parameters, if the Objective Function is defined as 'Expression'.");
            s.CreateAndAddFullTextBoxRow(p1, mycase.expression, (arg3, arg2) => { mycase.expression = arg3.Text; });

            s.CreateAndAddLabelRow(p1, "Optimization Control");
            int solvm = ((int)mycase.solvm - 5);

            if (solvm < 0)
            {
                solvm = 0;
            }
            s.CreateAndAddDropDownRow(p1, "Method", new[] { "Simplex", "LBFGS", "Truncated Newton", "Simplex (Bounded)", "LBFGS (Bounded)", "Truncated Newton (Bounded)" }.ToList(), solvm, (arg3, arg2) => { mycase.solvm = (DWSIM.SharedClasses.Flowsheet.Optimization.OptimizationCase.SolvingMethod)(arg3.SelectedIndex + 5); });
            s.CreateAndAddTextBoxRow(p1, "R", "Numerical derivative step", mycase.epsilon, (arg3, arg2) => { mycase.epsilon = arg3.Text.ToDoubleFromCurrent(); });
            s.CreateAndAddTextBoxRow(p1, "R", "Barrier multiplier", mycase.barriermultiplier, (arg3, arg2) => { mycase.barriermultiplier = arg3.Text.ToDoubleFromCurrent(); });

            s.CreateAndAddEmptySpace(this);

            s.CreateAndAddButtonRow(this, "Run Optimization", null, (arg3, arg2) =>
            {
                StartTask();
            });

            s.CreateAndAddEmptySpace(this);

            s.CreateAndAddEmptySpace(this);
        }
Exemple #3
0
        void Init()
        {
            Padding = new Padding(10);

            if (flowsheet.OptimizationCollection.Count == 0)
            {
                flowsheet.OptimizationCollection.Add(new DWSIM.SharedClasses.Flowsheet.Optimization.OptimizationCase());
            }

            mycase = flowsheet.OptimizationCollection.First();

            var su = flowsheet.FlowsheetOptions.SelectedUnitSystem;
            var nf = flowsheet.FlowsheetOptions.NumberFormat;

            var objlist = flowsheet.SimulationObjects.Values.Select((x2) => x2.GraphicObject.Tag).ToList();

            objlist.Insert(0, "");

            s.CreateAndAddDescriptionRow(this, "Use the optimizer to bring the flowsheet to a new 'state' regarding the maximum or minimum or value of a user-defined parameter.");

            s.CreateAndAddLabelRow(this, "Case ID");
            s.CreateAndAddFullTextBoxRow(this, mycase.name, (arg3, arg2) => { mycase.name = arg3.Text; });

            s.CreateAndAddLabelRow(this, "Case Description");
            s.CreateAndAddFullTextBoxRow(this, mycase.description, (arg3, arg2) => { mycase.description = arg3.Text; });

            s.CreateAndAddDropDownRow(this, "Optimization Type", new[] { "Minimization", "Maximization" }.ToList(), (int)mycase.type, (arg3, arg2) => { mycase.type = (DWSIM.SharedClasses.Flowsheet.Optimization.OPTType)arg3.SelectedIndex; });
            s.CreateAndAddDropDownRow(this, "Objective Function", new[] { "Variable", "Expression" }.ToList(), (int)mycase.objfunctype, (arg3, arg2) => { mycase.objfunctype = (DWSIM.SharedClasses.Flowsheet.Optimization.OPTObjectiveFunctionType)arg3.SelectedIndex; });

            s.CreateAndAddTextBoxRow(this, nf, "Maximum Iterations", (double)mycase.maxits, (arg3, arg2) => { mycase.maxits = (int)arg3.Text.ToDoubleFromCurrent(); });
            s.CreateAndAddTextBoxRow(this, nf, "Absolute Tolerance", mycase.tolerance, (arg3, arg2) => { mycase.tolerance = arg3.Text.ToDoubleFromCurrent(); });

            var varcontainer = new StackLayout {
                Orientation = Orientation.Horizontal, Padding = new Eto.Drawing.Padding(10), Spacing = 10, BackgroundColor = Colors.White
            };

            s.CreateAndAddBoldLabelAndButtonRow(this, "Variables", "Add Variable", null, (arg1, arg2) =>
            {
                var newiv  = new DWSIM.SharedClasses.Flowsheet.Optimization.OPTVariable();
                newiv.id   = new Random().Next().ToString();
                newiv.name = "var" + mycase.variables.Count.ToString();
                mycase.variables.Add(newiv.id, newiv);

                AddVariable(newiv, varcontainer, objlist);
            });
            s.CreateAndAddDescriptionRow(this, "Variables can be 'INDependent' (actual variables), 'AUXiliary' " +
                                         "(for usage as a parameter in the objective function expression, if applicable), " +
                                         "'DEPendent' (the parameter to be minimized/maximized, if applicable) or CONstraint, " +
                                         "if you don want it to go beyond the defined limits at the solution.");

            s.CreateAndAddControlRow(this, new Scrollable {
                Border = BorderType.None, Content = varcontainer
            });

            foreach (var item in mycase.variables.Values)
            {
                AddVariable(item, varcontainer, objlist);
            }

            s.CreateAndAddLabelRow(this, "Expression");
            s.CreateAndAddDescriptionRow(this, "Enter an expression to be minimized or maximized using IND and AUX variables as parameters, if the Objective Function is defined as 'Expression'.");
            s.CreateAndAddFullTextBoxRow(this, mycase.expression, (arg3, arg2) => { mycase.expression = arg3.Text; });

            s.CreateAndAddLabelRow(this, "Optimization Control");
            int solvm = ((int)mycase.solvm - 5);

            if (solvm < 0)
            {
                solvm = 0;
            }
            s.CreateAndAddDropDownRow(this, "Method", new[] { "Simplex", "LBFGS", "Truncated Newton", "Simplex (Bounded)", "LBFGS (Bounded)", "Truncated Newton (Bounded)" }.ToList(), solvm, (arg3, arg2) => { mycase.solvm = (DWSIM.SharedClasses.Flowsheet.Optimization.OptimizationCase.SolvingMethod)(arg3.SelectedIndex + 5); });
            s.CreateAndAddTextBoxRow(this, "R", "Numerical derivative step", mycase.epsilon, (arg3, arg2) => { mycase.epsilon = arg3.Text.ToDoubleFromCurrent(); });
            s.CreateAndAddTextBoxRow(this, "R", "Barrier multiplier", mycase.barriermultiplier, (arg3, arg2) => { mycase.barriermultiplier = arg3.Text.ToDoubleFromCurrent(); });

            s.CreateAndAddButtonRow(this, "Run Optimization", null, (arg3, arg2) =>
            {
                StartTask();
            });
        }