public static void ValidateWithSchema(string xmlConfig)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlConfig);

            string[] xsds = { Resource.wf_common, Resource.hv_configuration };
            foreach (string xsd in xsds)
            {
                using (TextReader schemaReader = new StringReader(xsd))
                {
                    XmlSchema schema = XmlSchema.Read(schemaReader, null);
                    xmlDoc.Schemas.Add(schema);
                }
            }

            xmlDoc.Validate(null);

            XmlSerializer serializer = new XmlSerializer(typeof(HyperVolume));

            using (TextReader reader = new StringReader(xmlConfig))
            {
                HyperVolume hyperVolume = (HyperVolume)serializer.Deserialize(reader);

                ValidateFilters(hyperVolume);
                ValidateForms(hyperVolume);
            }
        }
        public SMPSO(Problem problem, string trueParetoFront)
            : base(problem)
        {
            hy = new HyperVolume();
            MetricsUtil mu = new MetricsUtil();

            trueFront       = mu.ReadNonDominatedSolutionSet(trueParetoFront);
            trueHypervolume = hy.Hypervolume(trueFront.WriteObjectivesToMatrix(),
                                             trueFront.WriteObjectivesToMatrix(),
                                             problem.NumberOfObjectives);

            // Default configuration
            r1Max  = 1.0;
            r1Min  = 0.0;
            r2Max  = 1.0;
            r2Min  = 0.0;
            C1Max  = 2.5;
            C1Min  = 1.5;
            C2Max  = 2.5;
            C2Min  = 1.5;
            WMax   = 0.1;
            WMin   = 0.1;
            ChVel1 = -1;
            ChVel2 = -1;
        }
        public SMPSO(Problem problem,
                     List <double> variables,
                     string trueParetoFront)
            : base(problem)
        {
            r1Max  = variables[0];
            r1Min  = variables[1];
            r2Max  = variables[2];
            r2Min  = variables[3];
            C1Max  = variables[4];
            C1Min  = variables[5];
            C2Max  = variables[6];
            C2Min  = variables[7];
            WMax   = variables[8];
            WMin   = variables[9];
            ChVel1 = variables[10];
            ChVel2 = variables[11];

            hy = new HyperVolume();
            MetricsUtil mu = new MetricsUtil();

            trueFront       = mu.ReadNonDominatedSolutionSet(trueParetoFront);
            trueHypervolume = hy.Hypervolume(trueFront.WriteObjectivesToMatrix(),
                                             trueFront.WriteObjectivesToMatrix(),
                                             problem.NumberOfObjectives);
        }
Esempio n. 4
0
        /// <summary>
        /// Calculates how much hypervolume each point dominates exclusively. The
        /// points have to be transformed beforehand, to accommodate the assumptions
        /// of Zitzler's hypervolume code.
        /// </summary>
        /// <param name="numberOfobjectives"></param>
        /// <param name="front">transformed objective values</param>
        /// <returns>HV contributions</returns>
        public double[] HVContributions(int numberOfobjectives, double[][] front)
        {
            HyperVolume hyperVolume        = new HyperVolume();
            int         numberOfObjectives = numberOfobjectives;

            double[]   contributions = new double[front.Length];
            double[][] frontSubset   = new double[front.Length - 1][];          //[front[0].Length];
            for (int i = 0; i < front.Length - 1; i++)
            {
                frontSubset[i] = new double[front[0].Length];
            }

            List <double[]> frontCopy = new List <double[]>();

            frontCopy.AddRange(front);

            double[][] totalFront = frontCopy.ToArray();

            double totalVolume = hyperVolume.CalculateHypervolume(totalFront, totalFront.Length, numberOfObjectives);

            for (int i = 0; i < front.Length; i++)
            {
                double[] evaluatedPoint = frontCopy[i];
                frontCopy.RemoveAt(i);
                frontSubset = frontCopy.ToArray();
                // STEP4. The hypervolume (control is passed to java version of Zitzler code)
                double hv           = hyperVolume.CalculateHypervolume(frontSubset, frontSubset.Length, numberOfObjectives);
                double contribution = totalVolume - hv;
                contributions[i] = contribution;
                // put point back
                frontCopy.Insert(i, evaluatedPoint);
            }
            return(contributions);
        }
        private static bool FormExists(string formName, HyperVolume hyperVolume)
        {
            foreach (Form form in hyperVolume.Form)
            {
                if (formName.Equals(form.Name))
                {
                    return(true);
                }
            }

            return(false);
        }
 private static void ValidateFilters(HyperVolume hyperVolume)
 {
     foreach (Filter filter in hyperVolume.Filter)
     {
         Collection <FilterFilterForm> filterForms = filter.FilterForm;
         if (filterForms != null)
         {
             foreach (FilterFilterForm filterForm in filterForms)
             {
                 String formName = filterForm.FormName;
                 if (!formName.IsNullOrEmpty())
                 {
                     if (!FormExists(formName, hyperVolume))
                     {
                         throw new HyperVolumeConfigurationException($"Filter refers to unknown form \"{formName}\".");
                     }
                 }
             }
         }
     }
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="problem">Problem to solve</param>
 public SMSEMOA(Problem problem)
     : base(problem)
 {
     utils = new MetricsUtil();
     hv    = new HyperVolume();
 }
Esempio n. 8
0
        public IDictionary <string, IDictionary <string, IDictionary <string, List <double> > > > GetIndicators()
        {
            GenerateReferenceParetoFronts();

            var         indicators = new Dictionary <string, IDictionary <string, IDictionary <string, List <double> > > >();
            MetricsUtil utils      = new MetricsUtil();

            for (int i = 0, li = experiment.QualityIndicators.Count; i < li; i++)
            {
                string indicatorString = experiment.QualityIndicators[i].ToUpper();
                double value           = 0;

                var problems = new Dictionary <string, IDictionary <string, List <double> > >();
                indicators.Add(indicatorString, problems);

                foreach (var problem in experiment.ExperimentProblems)
                {
                    var algorithm = new Dictionary <string, List <double> >();
                    problems.Add(problem.Alias, algorithm);

                    var trueFront = utils.ReadFront(problem.ParetoFront);

                    foreach (var algorithmDictionary in problem.AlgorithmDictionary)
                    {
                        var indicator = new List <double>();

                        algorithm.Add(algorithmDictionary.Key, indicator);

                        foreach (var alg in algorithmDictionary.Value)
                        {
                            var solutionFront = alg.Result.GetObjectives();

                            switch (indicatorString)
                            {
                            case "HV":
                                HyperVolume hv = new HyperVolume();
                                value = hv.Hypervolume(solutionFront, trueFront, trueFront[0].Length);
                                break;

                            case "SPREAD":
                                Spread spread = new Spread();
                                value = spread.CalculateSpread(solutionFront, trueFront, trueFront[0].Length);
                                break;

                            case "IGD":
                                InvertedGenerationalDistance igd = new InvertedGenerationalDistance();
                                value = igd.CalculateInvertedGenerationalDistance(solutionFront, trueFront, trueFront[0].Length);
                                break;

                            case "EPSILON":
                                Epsilon epsilon = new Epsilon();
                                value = epsilon.CalcualteEpsilon(solutionFront, trueFront, trueFront[0].Length);
                                break;
                            }

                            indicator.Add(value);
                        }
                    }
                }
            }
            return(indicators);
        }
        private static void ValidateForms(HyperVolume hyperVolume)
        {
            int massiveForms = hyperVolume.Form.Count(f => f.Role == FormRole.MassiveUpdate);

            if (massiveForms > 1)
            {
                throw new HyperVolumeConfigurationException("Hyper volume should not contain more than one massive update form.");
            }

            List <GestPayButton> gestPayButtons = new List <GestPayButton>();

            foreach (Form form1 in hyperVolume.Form)
            {
                int nameCount = 0;

                foreach (Form form2 in hyperVolume.Form)
                {
                    if (form2.Name.Equals(form1.Name) && ++nameCount > 1)
                    {
                        throw new HyperVolumeConfigurationException($"Form with name \"{form1.Name}\" already exists.");
                    }
                }

                FormFakeDocument fakeDocument   = form1.FakeDocument;
                bool             isFakeDocument = fakeDocument != null && fakeDocument.Enabled == true;
                if (form1.Role == FormRole.MassiveUpdate)
                {
                    if (form1.LocalAppButton.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain Local App button.");
                    }

                    if (form1.GestPayButton.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain Payment button.");
                    }

                    if (form1.GSignButton.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain GSign button.");
                    }

                    if (form1.PreviewPanel.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain Preview panel.");
                    }

                    if (form1.ScanButton.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain Scan button.");
                    }

                    if (form1.QuestionnaireButton.Any())
                    {
                        throw new HyperVolumeConfigurationException("Massive update form should not contain Questionnaire button.");
                    }
                }

                foreach (LocalAppButton appButton in form1.LocalAppButton)
                {
                    if (appButton.Enabled == true)
                    {
                        throw new HyperVolumeConfigurationException("Fake document creation form should not contain any Local App Button enabled");
                    }
                }

                foreach (Button button1 in form1.Button)
                {
                    int idCount = 0;
                    foreach (Button button2 in form1.Button)
                    {
                        if (button1.CommandId.Equals(button2.CommandId) && ++idCount > 1)
                        {
                            throw new HyperVolumeConfigurationException($"Button with ID \"{button1.CommandId}\" already exists.");
                        }
                    }

                    if (form1.Role == FormRole.NewRecord)
                    {
                        foreach (PDFPrintAction printAction in button1.PdfPrintAction)
                        {
                            if (printAction.Enabled && !isFakeDocument)
                            {
                                throw new HyperVolumeConfigurationException(
                                          "Non fake document creation form should not contain PDF print action enabled.");
                            }
                        }

                        foreach (StampedPrintAction printAction in button1.StampedPrintAction)
                        {
                            if (printAction.Enabled)
                            {
                                throw new HyperVolumeConfigurationException("Document creation form should not contain document print action enabled.");
                            }
                        }
                    }
                }

                foreach (GestPayButton gpButton in form1.GestPayButton)
                {
                    if (form1.Role == FormRole.NewRecord)
                    {
                        throw new HyperVolumeConfigurationException("Form for new document creation should not contain Payment buttons");
                    }

                    foreach (GestPayButton paymentButton in gestPayButtons)
                    {
                        if (!gpButton.Amount.Equals(paymentButton.Amount, StringComparison.OrdinalIgnoreCase) ||
                            !gpButton.MerchantId.Equals(paymentButton.MerchantId, StringComparison.OrdinalIgnoreCase) ||
                            !gpButton.Currency.Equals(paymentButton.Currency, StringComparison.OrdinalIgnoreCase) ||
                            !gpButton.TransactionStatus.Equals(paymentButton.TransactionStatus, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new HyperVolumeConfigurationException("HyperVolume should not contain different definitions of Payment buttons");
                        }
                    }
                    gestPayButtons.Add(gpButton);
                }

                foreach (GSignButton gsButton in form1.GSignButton)
                {
                    if (form1.Role == FormRole.NewRecord && gsButton.Enabled == true)
                    {
                        throw new HyperVolumeConfigurationException("Form for document creation should not contain GSign button.");
                    }
                }

                foreach (ScanButton scanButton in form1.ScanButton)
                {
                    if (form1.Role == FormRole.UpdateRecord)
                    {
                        throw new HyperVolumeConfigurationException("Update record form should not contain Scan button.");
                    }

                    if (isFakeDocument && scanButton.Enabled == true)
                    {
                        throw new HyperVolumeConfigurationException("Fake document creation form should not contain any Scan buttons enabled.");
                    }
                }

                foreach (PreviewPanel previewPanel in form1.PreviewPanel)
                {
                    if (form1.Role == FormRole.NewRecord && previewPanel.Enabled == true)
                    {
                        throw new HyperVolumeConfigurationException("Record creation form can not contain Preview panel.");
                    }
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Calculates the hv contribution of different populations. Receives an
        /// array of populations and computes the contribution to HV of the
        /// population consisting in the union of all of them
        /// </summary>
        /// <param name="archive"></param>
        /// <param name="populations">Consisting in all the populatoins</param>
        /// <returns>HV contributions of each population</returns>
        public double[] HVContributions(SolutionSet archive, SolutionSet[] populations)
        {
            SolutionSet union;
            int         size    = 0;
            double      offset_ = 0.0;

            //determining the global size of the population
            foreach (SolutionSet population in populations)
            {
                size += population.Size();
            }

            //allocating space for the union
            union = archive;

            //determining the number of objectives
            int numberOfObjectives = union.Get(0).NumberOfObjectives;

            //writing everything in matrices
            double[][][] frontValues = new double[populations.Length + 1][][];

            frontValues[0] = union.WriteObjectivesToMatrix();
            for (int i = 0; i < populations.Length; i++)
            {
                if (populations[i].Size() > 0)
                {
                    frontValues[i + 1] = populations[i].WriteObjectivesToMatrix();
                }
                else
                {
                    frontValues[i + 1] = new double[0][];
                }
            }

            // obtain the maximum and minimum values of the Pareto front
            double[] maximumValues = GetMaximumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);
            double[] minimumValues = GetMinimumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);

            // normalized all the fronts
            double[][][] normalizedFront = new double[populations.Length + 1][][];
            for (int i = 0; i < normalizedFront.Length; i++)
            {
                if (frontValues[i].Length > 0)
                {
                    normalizedFront[i] = GetNormalizedFront(frontValues[i], maximumValues, minimumValues);
                }
                else
                {
                    normalizedFront[i] = new double[0][];
                }
            }

            // compute offsets for reference point in normalized space
            double[] offsets = new double[maximumValues.Length];
            for (int i = 0; i < maximumValues.Length; i++)
            {
                offsets[i] = offset_ / (maximumValues[i] - minimumValues[i]);
            }

            //Inverse all the fronts front. This is needed because the original
            //metric by Zitzler is for maximization problems
            double[][][] invertedFront = new double[populations.Length + 1][][];
            for (int i = 0; i < invertedFront.Length; i++)
            {
                if (normalizedFront[i].Length > 0)
                {
                    invertedFront[i] = InvertedFront(normalizedFront[i]);
                }
                else
                {
                    invertedFront[i] = new double[0][];
                }
            }

            // shift away from origin, so that boundary points also get a contribution > 0
            foreach (double[][] anInvertedFront in invertedFront)
            {
                foreach (double[] point in anInvertedFront)
                {
                    for (int i = 0; i < point.Length; i++)
                    {
                        point[i] += offsets[i];
                    }
                }
            }

            // calculate contributions
            double[]    contribution = new double[populations.Length];
            HyperVolume hyperVolume  = new HyperVolume();

            for (int i = 0; i < populations.Length; i++)
            {
                if (invertedFront[i + 1].Length == 0)
                {
                    contribution[i] = 0;
                }
                else
                {
                    int auxSize = 0;
                    for (int j = 0; j < populations.Length; j++)
                    {
                        if (j != i)
                        {
                            auxSize += invertedFront[j + 1].Length;
                        }
                    }

                    if (size == archive.Size())
                    {                     // the contribution is the maximum hv
                        contribution[i] = hyperVolume.CalculateHypervolume(invertedFront[0], invertedFront[0].Length, numberOfObjectives);
                    }
                    else
                    {
                        //make a front with all the populations but the target one
                        int        index = 0;
                        double[][] aux   = new double[auxSize][];
                        for (int j = 0; j < populations.Length; j++)
                        {
                            if (j != i)
                            {
                                for (int k = 0; k < populations[j].Size(); k++)
                                {
                                    aux[index++] = invertedFront[j + 1][k];
                                }
                            }
                        }
                        contribution[i] = hyperVolume.CalculateHypervolume(invertedFront[0], invertedFront[0].Length, numberOfObjectives)
                                          - hyperVolume.CalculateHypervolume(aux, aux.Length, numberOfObjectives);
                    }
                }
            }

            return(contribution);
        }
Esempio n. 11
0
        /// <summary>
        /// Calculates the hv contribution of different populations. Receives an
        /// array of populations and computes the contribution to HV of the
        /// population consisting in the union of all of them
        /// </summary>
        /// <param name="populations">Consisting in all the populatoins</param>
        /// <returns>HV contributions of each population</returns>
        public double[] HVContributions(SolutionSet[] populations)
        {
            bool empty = true;

            foreach (SolutionSet population2 in populations)
            {
                if (population2.Size() > 0)
                {
                    empty = false;
                }
            }

            if (empty)
            {
                double[] contributions = new double[populations.Length];
                for (int i = 0; i < populations.Length; i++)
                {
                    contributions[i] = 0;
                }
                for (int i = 0; i < populations.Length; i++)
                {
                    Logger.Log.Info("Contributions: " + contributions[i]);
                    Console.WriteLine(contributions[i]);
                }
                return(contributions);
            }

            SolutionSet union;
            int         size    = 0;
            double      offset_ = 0.0;

            //determining the global size of the population
            foreach (SolutionSet population1 in populations)
            {
                size += population1.Size();
            }

            //allocating space for the union
            union = new SolutionSet(size);

            // filling union
            foreach (SolutionSet population in populations)
            {
                for (int j = 0; j < population.Size(); j++)
                {
                    union.Add(population.Get(j));
                }
            }

            //determining the number of objectives
            int numberOfObjectives = union.Get(0).NumberOfObjectives;

            //writing everything in matrices
            double[][][] frontValues = new double[populations.Length + 1][][];

            frontValues[0] = union.WriteObjectivesToMatrix();
            for (int i = 0; i < populations.Length; i++)
            {
                if (populations[i].Size() > 0)
                {
                    frontValues[i + 1] = populations[i].WriteObjectivesToMatrix();
                }
                else
                {
                    frontValues[i + 1] = new double[0][];
                }
            }

            // obtain the maximum and minimum values of the Pareto front
            double[] maximumValues = GetMaximumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);
            double[] minimumValues = GetMinimumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);

            // normalized all the fronts
            double[][][] normalizedFront = new double[populations.Length + 1][][];
            for (int i = 0; i < normalizedFront.Length; i++)
            {
                if (frontValues[i].Length > 0)
                {
                    normalizedFront[i] = GetNormalizedFront(frontValues[i], maximumValues, minimumValues);
                }
                else
                {
                    normalizedFront[i] = new double[0][];
                }
            }

            // compute offsets for reference point in normalized space
            double[] offsets = new double[maximumValues.Length];
            for (int i = 0; i < maximumValues.Length; i++)
            {
                offsets[i] = offset_ / (maximumValues[i] - minimumValues[i]);
            }

            //Inverse all the fronts front. This is needed because the original
            //metric by Zitzler is for maximization problems
            double[][][] invertedFront = new double[populations.Length + 1][][];
            for (int i = 0; i < invertedFront.Length; i++)
            {
                if (normalizedFront[i].Length > 0)
                {
                    invertedFront[i] = InvertedFront(normalizedFront[i]);
                }
                else
                {
                    invertedFront[i] = new double[0][];
                }
            }

            // shift away from origin, so that boundary points also get a contribution > 0
            foreach (double[][] anInvertedFront in invertedFront)
            {
                foreach (double[] point in anInvertedFront)
                {
                    for (int i = 0; i < point.Length; i++)
                    {
                        point[i] += offsets[i];
                    }
                }
            }

            // calculate contributions
            double[]    contribution = new double[populations.Length];
            HyperVolume hyperVolume  = new HyperVolume();

            for (int i = 0; i < populations.Length; i++)
            {
                if (invertedFront[i + 1].Length == 0)
                {
                    contribution[i] = 0;
                }
                else
                {
                    if (invertedFront[i + 1].Length != invertedFront[0].Length)
                    {
                        double[][] aux = new double[invertedFront[0].Length - invertedFront[i + 1].Length][];
                        int        startPoint = 0, endPoint;
                        for (int j = 0; j < i; j++)
                        {
                            startPoint += invertedFront[j + 1].Length;
                        }
                        endPoint = startPoint + invertedFront[i + 1].Length;
                        int index = 0;
                        for (int j = 0; j < invertedFront[0].Length; j++)
                        {
                            if (j < startPoint || j >= (endPoint))
                            {
                                aux[index++] = invertedFront[0][j];
                            }
                        }

                        contribution[i] = hyperVolume.CalculateHypervolume(invertedFront[0], invertedFront[0].Length, numberOfObjectives)
                                          - hyperVolume.CalculateHypervolume(aux, aux.Length, numberOfObjectives);
                    }
                    else
                    {
                        contribution[i] = hyperVolume.CalculateHypervolume(invertedFront[0], invertedFront[0].Length, numberOfObjectives);
                    }
                }
            }

            return(contribution);
        }