private void loadButton_Click(object sender, EventArgs e) { Parameters parameters = new Parameters(); string path = pathTextBox.Text; if (String.IsNullOrEmpty(path) || String.IsNullOrWhiteSpace(path) || !path.EndsWith(".txt") || !path.Contains("\\")) { ControlStyle.MessageBoxStyle("Η τοποθεσία(URI) του αρχείου δεν είναι έγκυρη"); return; } using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); parameters = JsonConvert.DeserializeObject <Parameters>(json); } ObjectiveRichTextBox.Text = parameters.Objective; if (parameters.Optimum == OptimizationGoal.Max) { MaxRadioBtn.Checked = true; } if (parameters.Optimum == OptimizationGoal.Min) { MinRadioBtn.Checked = true; } if (parameters.Optimum == OptimizationGoal.MinAbs) { MinAbsRadioBtn.Checked = true; } showAllCheckBox.Checked = parameters.ShowAll; checkDecisionVariables(); for (int j = 0; j < totalNotesControls; j++) { Controls.Find("x" + (j + 1) + "MinTextBox", true)[0].Text = parameters.MinValues[j].ToString(); Controls.Find("x" + (j + 1) + "MaxTextBox", true)[0].Text = parameters.MaxValues[j].ToString(); } NITextBox.Text = parameters.NI.ToString(); HMSTextBox.Text = parameters.HMS.ToString(); HMCRLabel.Text = parameters.HMCR.ToString(); PARTextBox.Text = parameters.PAR.ToString(); BWTextBox.Text = parameters.BW.ToString(); PARMinTextBox.Text = parameters.PARmin.ToString(); PARMaxTextBox.Text = parameters.PARmax.ToString(); BWMinTextBox.Text = parameters.PARmin.ToString(); BWMaxTextBox.Text = parameters.PARmax.ToString(); if (parameters.Variant == HarmonySearchVariant.Classic) { ClassicRadioButton.Checked = true; } if (parameters.Variant == HarmonySearchVariant.Improved) { ImprovedRadioButton.Checked = true; } if (parameters.Variant == HarmonySearchVariant.GlobalBest) { GlobalRadioButton.Checked = true; } if (parameters.Variant == HarmonySearchVariant.SelfAdaptive) { AdaptiveRadioButton.Checked = true; } ControlStyle.MessageBoxSuccessStyle("Οι παράμετροι του αλγορίθμου φορτώθηκαν επιτυχώς από το αρχείο."); }
private void saveButton_Click(object sender, EventArgs e) { if (!isInputOk()) { //ControlStyle.MessageBoxStyle("Parameters are not valid."); ControlStyle.MessageBoxStyle("Οι παράμετροι δεν είναι σωστοί."); return; } string path = pathTextBox.Text; if (String.IsNullOrEmpty(path) || String.IsNullOrWhiteSpace(path) || !path.EndsWith(".txt") || !path.Contains("\\")) { ControlStyle.MessageBoxStyle("Η τοποθεσία(URI) του αρχείου δεν είναι έγκυρη"); return; } Parameters parameters = new Parameters(); parameters.Objective = ObjectiveRichTextBox.Text; parameters.ShowAll = showAllCheckBox.Checked; if (MaxRadioBtn.Checked == true) { parameters.Optimum = OptimizationGoal.Max; } if (MinRadioBtn.Checked == true) { parameters.Optimum = OptimizationGoal.Min; } if (MinAbsRadioBtn.Checked == true) { parameters.Optimum = OptimizationGoal.MinAbs; } parameters.MaxValues = new double[totalNotesControls]; parameters.MinValues = new double[totalNotesControls]; for (int i = 0; i < totalNotesControls; i++) { TextBox maxTextbox = (TextBox)this.Controls.Find("x" + (i + 1) + "MaxTextBox", true)[0]; parameters.MaxValues[i] = double.Parse(maxTextbox.Text, CultureInfo.InvariantCulture); TextBox minTextbox = (TextBox)this.Controls.Find("x" + (i + 1) + "MinTextBox", true)[0]; parameters.MinValues[i] = double.Parse(minTextbox.Text, CultureInfo.InvariantCulture); } parameters.NI = Int32.Parse(NITextBox.Text); parameters.HMS = Int32.Parse(HMSTextBox.Text); parameters.HMCR = Convert.ToDouble(HMCRTextBox.Text); parameters.PAR = Convert.ToDouble(PARTextBox.Text); parameters.BW = Convert.ToDouble(BWTextBox.Text); parameters.PARmin = Convert.ToDouble(PARMinTextBox.Text); parameters.PARmax = Convert.ToDouble(PARMaxTextBox.Text); parameters.BWmin = Convert.ToDouble(BWMinTextBox.Text); parameters.BWmax = Convert.ToDouble(BWMaxTextBox.Text); if (ClassicRadioButton.Checked == true) { parameters.Variant = HarmonySearchVariant.Classic; } if (ImprovedRadioButton.Checked == true) { parameters.Variant = HarmonySearchVariant.Improved; } if (GlobalRadioButton.Checked == true) { parameters.Variant = HarmonySearchVariant.GlobalBest; } if (AdaptiveRadioButton.Checked == true) { parameters.Variant = HarmonySearchVariant.SelfAdaptive; } string json = JsonConvert.SerializeObject(parameters); File.WriteAllText(@path, json); ControlStyle.MessageBoxSuccessStyle("Οι παράμετροι του αλγορίθμου αποθηκεύτηκαν επιτυχώς σε αρχείο."); }
private Boolean isInputOk() { //if(ObjectiveRichTextBox.Text.Equals("") || ObjectiveRichTextBox.Text == null) //{ // ControlStyle.MessageBoxStyle("The objective function is null or empty."); // return false; //} objective = new Expression(ObjectiveRichTextBox.Text); //if (objective.HasErrors()) //{ // ControlStyle.MessageBoxStyle("The objective function is not valid. \n\n" + objective.Error); // return false; //} //if (objective == null) //{ // ControlStyle.MessageBoxStyle("The objective function is not valid. Please try again."); // return false; //} if (totalNotesControls < 2) { //ControlStyle.MessageBoxStyle("The algorithm requires at least 2 decision variables."); ControlStyle.MessageBoxStyle("Ο αλγόριθμος απαιτεί τουλάχιστον 2 μεταβλητές απόφασης."); return(false); } try { for (int k = 1; k <= totalNotesControls; k++) { objective.Parameters["x" + k] = 5; } object res = objective.Evaluate(); } catch (Exception e) { //ControlStyle.MessageBoxStyle("The objective function is not valid. Please try again."); ControlStyle.MessageBoxStyle("Η αντικειμενική συνάρτηση δεν συντάχθηκε σωστά."); return(false); } for (int i = 0; i < totalNotesControls; i++) { TextBox minTextBox = (TextBox)this.Controls.Find("x" + (i + 1) + "MinTextBox", true)[0]; TextBox maxTextBox = (TextBox)this.Controls.Find("x" + (i + 1) + "MaxTextBox", true)[0]; if (!ConfigurationRules.areExtremeValuesValid(minTextBox.Text, maxTextBox.Text)) { //ControlStyle.MessageBoxStyle("Decision variable " + "X" + (i + 1) + " bounds are not valid."); ControlStyle.MessageBoxStyle("Τα όρια της μεταβλητής απόφασης " + "X" + (i + 1) + " δεν είναι σωστά."); return(false); } } if (!ConfigurationRules.isNIValid(NITextBox.Text)) { //ControlStyle.MessageBoxStyle("NI(Number of Improvisations) field is not valid. Please try again."); ControlStyle.MessageBoxStyle("Το πεδίο NI(Number of Improvisations) δεν είναι σωστό."); return(false); } if (!ConfigurationRules.isHMSValid(HMSTextBox.Text)) { //ControlStyle.MessageBoxStyle("HMS(Harmony Memory Size) field is not valid. Please try again."); ControlStyle.MessageBoxStyle("Το πεδίο HMS(Harmony Memory Size) δεν είναι σωστό."); return(false); } if (!ConfigurationRules.isHMCRValid(HMCRTextBox.Text)) { //ControlStyle.MessageBoxStyle("HMCR(Harmony Memory Consideration Rate) field is not valid. Please try again."); ControlStyle.MessageBoxStyle("Το πεδίο HMCR(Harmony Memory Consideration Rate) δεν είναι σωστό."); return(false); } if (!currentVariant.Equals(HarmonySearchVariant.Improved)) { if (!ConfigurationRules.isPARValid(PARTextBox.Text)) { //ControlStyle.MessageBoxStyle("PAR(Pitch Adjustment Rate) field is not valid."); ControlStyle.MessageBoxStyle("Το πεδίο PAR(Pitch Adjustment Rate) δεν είναι σωστό."); return(false); } if (currentVariant.Equals(HarmonySearchVariant.Classic)) { if (!ConfigurationRules.isBWValid(BWTextBox.Text)) { //ControlStyle.MessageBoxStyle("BW(Bandwidth) field is not valid."); ControlStyle.MessageBoxStyle("Το πεδίο BW(Bandwidth) δεν είναι σωστό."); return(false); } } } else { if (!ConfigurationRules.arePARExtremesValid(PARMinTextBox.Text, PARMaxTextBox.Text)) { //ControlStyle.MessageBoxStyle("PAR(Pitch Adjustment Rate) bounds are not valid."); ControlStyle.MessageBoxStyle("Τα όρια του πεδίου PAR(Pitch Adjustment Rate) δεν είναι σωστά."); return(false); } if (!ConfigurationRules.areΒWExtremesValid(BWMinTextBox.Text, BWMaxTextBox.Text)) { //ControlStyle.MessageBoxStyle("BW(Bandwidth) bounds are not not valid."); ControlStyle.MessageBoxStyle("Τα όρια του πεδίου BW(Bandwidth) δεν είναι σωστά."); return(false); } } return(true); }