public SaveObjectiveFunctionsViewModel(string objectiveFunctionsJson, string defaultDirectoryPath)
        {
            DefaultDirectoryPath = defaultDirectoryPath;

            var objectiveFunctionsJObject  = JObject.Parse(objectiveFunctionsJson);
            var objectiveFunctionArguments = (JArray)objectiveFunctionsJObject["Arguments"];

            ObjectiveFunctions.Clear();
            foreach (var a in objectiveFunctionArguments)
            {
                var jObject = JObject.Parse(a.ToString());
                ObjectiveFunctions.Add(new Models.ObjectiveFunction(jObject));
            }

            var prescriptionsJArray = (JArray)objectiveFunctionsJObject["Prescriptions"];
            var PrescriptionsAll    = prescriptionsJArray.ToObject <List <Models.Prescription> >();

            Prescriptions.Clear();
            foreach (var p in PrescriptionsAll)
            {
                if (ObjectiveFunctions.Where(o => (o.PlanLabel == p.PlanLabel)).Count() > 0)
                {
                    Prescriptions.Add(p);
                }
            }

            OkCommand       = new DelegateCommand(() => { CanExecute = true; });
            CancelCommand   = new DelegateCommand(() => { CanExecute = false; });
            SaveFileCommand = new DelegateCommand(SaveFile);
        }
            public IDesign CreateDesign(double[] x)
            {
                double[] objectiveValues = new double[ObjectiveFunctions.Count];
                for (int i = 0; i < ObjectiveFunctions.Count; ++i)
                {
                    objectiveValues[i] = ObjectiveFunctions[i](x);
                }

                double[] constraintValues = new double[ConstraintFunctions.Count];
                for (int i = 0; i < ConstraintFunctions.Count; ++i)
                {
                    constraintValues[i] = ConstraintFunctions[i](x);
                }

                return(new Design(objectiveValues, constraintValues));
            }
        private void SetObjectiveFunctionArguments()
        {
            foreach (var r in Rois)
            {
                if (!r.InUse || r.NameInObjectiveFunction == RoiNameNone)
                {
                    continue;
                }

                var query = ObjectiveFunctions.Where(o => (o.InUse && o.RoiName == r.NameInObjectiveFunction));
                if (query.Count() == 0)
                {
                    continue;
                }

                foreach (var o in query)
                {
                    var    planLabelQuery = PlanLabels.Where(p => p.LabelInObjectiveFunction == o.PlanLabel);
                    string newLabel       = string.Empty;
                    if (planLabelQuery.Count() > 0)
                    {
                        var  newPlanLabel = planLabelQuery.First();
                        bool inUse        = newPlanLabel.InUse;
                        if (!inUse)
                        {
                            continue;
                        }
                        newLabel = newPlanLabel.Label;
                        if (planLabelQuery.Count() >= 2)
                        {
                            Message = $"Multiple plans are assigned to {o.PlanLabel}. Use {newLabel}.";
                        }
                        o.SetPlanLabelInArguments(newLabel);
                    }
                    else if (!(o.PlanLabel == PlanLabelCombinedDose))
                    {
                        continue;
                    }

                    o.UpdateWeightInArguments();
                    o.SetRoiNameInArguments(r.Name);

                    if (o.PlanLabel == PlanLabelCombinedDose)
                    {
                        if (!DoesUseCombinedDose)
                        {
                            continue;
                        }
                        newLabel = PlanLabelCombinedDose;
                    }

                    var    prescriptionQuery = Prescriptions.Where(p => p.PlanLabel == o.PlanLabel);
                    double scale             = 0;
                    if (prescriptionQuery.Count() == 0)
                    {
                        scale   = 1.0;
                        Message = $"Prescription does not exist for new: {newLabel} and original: {o.PlanLabel}";
                    }
                    else
                    {
                        var prescription           = prescriptionQuery.First();
                        var originalPrescribedDose = prescription.PrescribedDoseInObjectiveFunction;
                        var prescribedDose         = prescription.PrescribedDose;
                        if (DoesRescaleDose)
                        {
                            if (originalPrescribedDose == 0)
                            {
                                Message = $"No rescale because the original prescribed dose = 0";
                                scale   = 1.0;
                            }
                            else
                            {
                                scale = prescribedDose / originalPrescribedDose;
                            }
                        }
                    }

                    var functionType = o.Arguments["FunctionType"].ToObject <string>();
                    if (functionType == "DoseFallOff")
                    {
                        var highDoseLevel = scale * o.Arguments["HighDoseLevel"].ToObject <double>();
                        var lowDoseLevel  = scale * o.Arguments["LowDoseLevel"].ToObject <double>();
                        o.Arguments["HighDoseLevel"] = highDoseLevel;
                        o.Arguments["LowDoseLevel"]  = lowDoseLevel;
                    }
                    else if (functionType == "UniformDose" ||
                             functionType == "MaxDose" || functionType == "MinDose" ||
                             functionType == "MaxEud" || functionType == "MinEud" ||
                             functionType == "MaxDvh" || functionType == "MinDvh")
                    {
                        var doseLevel = scale * o.Arguments["DoseLevel"].ToObject <double>();
                        o.Arguments["DoseLevel"] = doseLevel;
                    }

                    r.ObjectiveFunctionArguments.Add(o.Arguments.ToString());
                }
            }
        }
        private void ChooseFile()
        {
            var dialog = new CommonOpenFileDialog("Choose File");

            if (Directory.Exists(DefaultDirectoryPath))
            {
                dialog.InitialDirectory = DefaultDirectoryPath;
            }

            dialog.IsFolderPicker = false;

            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                FilePath = dialog.FileName;

                using (var sr = new StreamReader(FilePath))
                    using (JsonTextReader reader = new JsonTextReader(sr))
                    {
                        ObjectiveFunctionsJObject = (JObject)JToken.ReadFrom(reader);
                    }

                var prescriptionsJArray = (JArray)ObjectiveFunctionsJObject["Prescriptions"];
                Prescriptions = prescriptionsJArray.ToObject <ObservableCollection <Models.Prescription> >();

                var descriptionJObject = ObjectiveFunctionsJObject["Description"];
                if (descriptionJObject != null)
                {
                    Description = descriptionJObject.ToObject <string>();
                }
                else
                {
                    Description = string.Empty;
                }

                var argumentsJArray = (JArray)ObjectiveFunctionsJObject["Arguments"];
                ObjectiveFunctions.Clear();
                foreach (var a in argumentsJArray)
                {
                    var jObject = JObject.Parse(a.ToString());
                    ObjectiveFunctions.Add(new Models.ObjectiveFunction(jObject));
                    string roiName = jObject["RoiName"].ToObject <string>();
                    if (!RoiNamesInObjectiveFunctions.Contains(roiName))
                    {
                        RoiNamesInObjectiveFunctions.Add(roiName);
                    }

                    string planLabel = jObject["PlanLabel"].ToObject <string>();
                    if (!(planLabel == PlanLabelCombinedDose) && !PlanLabelsInObjectiveFuntions.Contains(planLabel))
                    {
                        PlanLabelsInObjectiveFuntions.Add(planLabel);
                    }
                }
                RoiNamesInObjectiveFunctions.OrderBy(r => r);

                foreach (var r in Rois)
                {
                    if (RoiNamesInObjectiveFunctions.Contains(r.Name))
                    {
                        r.InUse = true;
                        r.NameInObjectiveFunction = r.Name;
                    }
                }

                foreach (var p in PlanLabels)
                {
                    if (p.InUse && PlanLabelsInObjectiveFuntions.Contains(p.Label))
                    {
                        p.LabelInObjectiveFunction = p.Label;
                    }
                }

                var planSumDose = PlanLabels.Select(p => p.PrescribedDose).Sum();
                foreach (var p in Prescriptions)
                {
                    var query = PlanLabels.Where(pl => pl.Label == p.PlanLabel);
                    if (query.Count() > 0)
                    {
                        p.PrescribedDose = query.First().PrescribedDose;
                        continue;
                    }

                    if (p.PlanLabel == PlanLabelCombinedDose)
                    {
                        p.PrescribedDose = planSumDose;
                    }
                }
            }
            else
            {
                Message = "\"Choose File\" is canceled";
            }
        }