Пример #1
0
        /// <summary>
        /// Creates a domain constraint restricting an attribute to be greater than the MetricDomain max value - 1
        /// </summary>
        /// <returns></returns>
        public static DomainConstraint MaxValueConstraint(GKOAttribute attribute, GKOIntDomain metricDomain)
        {
            BinaryRelation   lessOrEquals  = StructuralRelationsManager.GetRelation(RelationFamilyNames.MetricRelationsName, MetricRelationNames.LessOrEqualsN);
            DomainConstraint maxConstraint = new DomainConstraint("Metric max value constraint", lessOrEquals, false);

            maxConstraint.DomainRelationParts.Add(new AttributeDomainRelationPart(new ComponentFilter(), attribute));
            maxConstraint.DomainRelationParts.Add(new MetricDomainRelationPart(metricDomain.MaxValue, metricDomain));

            return(maxConstraint);
        }
Пример #2
0
        /// <summary>
        /// Generates the metric domain for the TMS
        /// </summary>
        /// <returns></returns>
        private GKOIntDomain GenerateMetricDomain()
        {
            GKOIntDomain domain = new GKOIntDomain()
            {
                Id        = DomainNameMetric,
                Name      = DomainNameMetric,
                StepWidth = 1,
                MinValue  = StructuralRelationsManager.MetricDomain.MinValue,
                MaxValue  = structuralReasonerOptions.MaxMetricValue
            };

            return(domain);
        }
Пример #3
0
        ///// <summary>
        ///// Create all constraint types corresponding to the subsets of the relations in a relation family
        ///// </summary>
        ///// <param name="calculus">The relation family</param>
        //private void CreateCTForPowerset(RelationFamily calculus)
        //{
        //    //GKOConstraintType constraintType;
        //    //GKODomainAbstract relationsDomain;
        //    //GKODomainAbstract powerSetIxDomain;
        //    //bool enableSoftConstraints = this.structuralReasonerOptions.SoftConstraintsEnabled;

        //    //if (calculus.Name == RelationFamilyNames.MetricRelationsName)
        //    //{
        //    //    throw new NotSupportedException("The metric relations do not support this operation.");
        //    //}

        //    //relationsDomain = StructuralRelationsManager.GetDomain(calculus.GetTmsRelationsDomainName());
        //    //powerSetIxDomain = this.CalculiPwSetIxDomain;

        //    //constraintType = new GKOConstraintType();
        //    //constraintType.Id = calculus.GetPwSetCTName();
        //    //constraintType.Name = calculus.GetPwSetCTName();
        //    //constraintType.Signature = new List<GKODomainAbstract>() { powerSetIxDomain, relationsDomain };
        //    //constraintType.Tuples = new List<List<string>>();
        //    //if (enableSoftConstraints)
        //    //{
        //    //    constraintType.Signature.Add(this.BoolDomain);
        //    //}

        //    //// The name of the constraint is labeled by the integer representation of the included relations
        //    //// e.g. set 17 = 10001, i.e. relations 0 and 3 are included in the constraint with label 17
        //    //for (int setIx = 1; setIx < Math.Pow(2, calculus.Relations.Count); setIx++)
        //    //{
        //    //    List<BinaryRelation> includedRelations = new List<BinaryRelation>();

        //    //    for (int i = 0; i < calculus.Relations.Count; i++)
        //    //    {
        //    //        if ((setIx & (1 << i)) != 0)
        //    //        {
        //    //            //n-th bit is set, so we include the n-th relation
        //    //            includedRelations.Add(calculus.Relations[i]);
        //    //        }
        //    //    }

        //    //    // When soft constraints are enabled the constraint types should be adjusted accordingly
        //    //    if (enableSoftConstraints)
        //    //    {
        //    //        List<BinaryRelation> excludedReltions = calculus.Relations.Except(includedRelations).ToList();

        //    //        foreach (var rel in includedRelations)
        //    //        {
        //    //            constraintType.Tuples.Add(new List<string>() { setIx.ToString(), rel.Name, TmsManager.TrueValue });
        //    //        }

        //    //        // Adding the non-satisfied tuples: *, 0
        //    //        // ct.Tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.FalseValue });
        //    //        foreach (var rel in excludedReltions)
        //    //        {
        //    //            constraintType.Tuples.Add(new List<string>() { setIx.ToString(), rel.Name, TmsManager.FalseValue });
        //    //        }
        //    //    }
        //    //    else
        //    //    {
        //    //        foreach (var rel in includedRelations)
        //    //        {
        //    //            constraintType.Tuples.Add(new List<string>() { setIx.ToString(), rel.Name });
        //    //        }
        //    //    }
        //    //}

        //    //this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));
        //}

        /// <summary>
        /// Creates the metric constraint types
        /// </summary>
        /// <returns></returns>
        private void CreateMetricRelConstrTypes()
        {
            GKOIntDomain          metricDomain    = this.MetricDomain;
            GKODomainAbstract     satisfiedDomain = this.BoolDomain;
            GKOConstraintType     constraintType;
            List <List <string> > tuples;
            // Legacy variable. Now it should be always true
            bool enableSoftConstraints = true;

            // Creating the "Greater than" (a>b) constraint type
            constraintType = new GKOConstraintType()
            {
                Id   = CTNameGreaterThan,
                Name = CTNameGreaterThan
            };
            constraintType.Signature = new List <GKODomainAbstract>()
            {
                metricDomain, metricDomain
            };
            if (enableSoftConstraints)
            {
                constraintType.Signature.Add(satisfiedDomain);
            }

            tuples = new List <List <string> >();
            for (int a = metricDomain.MinValue; a < metricDomain.MaxValue + 1; a += metricDomain.StepWidth)
            {
                if (enableSoftConstraints)
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        // in some cases the non-satisfied tuples are added: *, *, 0
                        // tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.FalseValue });
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString(), a > b ? TrueValue : FalseValue
                        });
                    }
                }
                else
                {
                    for (int b = metricDomain.MinValue; b < a; b += metricDomain.StepWidth)
                    {
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString()
                        });
                    }
                }
            }

            constraintType.Tuples = tuples;
            // Adds the constraint type to the CS3
            this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));

            // Creating the "Greater or equals" (a>=b) constraint type
            constraintType = new GKOConstraintType()
            {
                Id   = CTNameGreaterOrEquals,
                Name = CTNameGreaterOrEquals
            };
            constraintType.Signature = new List <GKODomainAbstract>()
            {
                metricDomain, metricDomain
            };
            if (enableSoftConstraints)
            {
                constraintType.Signature.Add(satisfiedDomain);
            }

            tuples = new List <List <string> >();
            for (int a = metricDomain.MinValue; a < metricDomain.MaxValue + 1; a += metricDomain.StepWidth)
            {
                if (enableSoftConstraints)
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        // in some cases the non-satisfied tuples are added: *, *, 0
                        // tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.FalseValue });
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString(), a >= b ? TrueValue : FalseValue
                        });
                    }
                }
                else
                {
                    for (int b = metricDomain.MinValue; b <= a; b += metricDomain.StepWidth)
                    {
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString()
                        });
                    }
                }
            }

            constraintType.Tuples = tuples;
            // Adds the constraint type to the CS3
            this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));

            // Creating the "Equals" (a=b) constraint type
            constraintType = new GKOConstraintType()
            {
                Id   = CTNameEquals,
                Name = CTNameEquals
            };
            constraintType.Signature = new List <GKODomainAbstract>()
            {
                metricDomain, metricDomain
            };
            if (enableSoftConstraints)
            {
                constraintType.Signature.Add(satisfiedDomain);
            }

            tuples = new List <List <string> >();
            for (int a = metricDomain.MinValue; a < metricDomain.MaxValue + 1; a += metricDomain.StepWidth)
            {
                if (enableSoftConstraints)
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        // in some cases the non-satisfied tuples are added: *, *, 0
                        // tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.FalseValue });
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString(), a == b ? TmsManager.TrueValue : TmsManager.FalseValue
                        });
                    }
                }
                else
                {
                    tuples.Add(new List <string>()
                    {
                        a.ToString(), a.ToString()
                    });
                }
            }

            constraintType.Tuples = tuples;
            // Adds the constraint type to the CS3
            this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));

            // Creating the "Not equals" (a!=b) constraint type
            constraintType = new GKOConstraintType()
            {
                Id   = CTNameNotEquals,
                Name = CTNameNotEquals
            };
            constraintType.Signature = new List <GKODomainAbstract>()
            {
                metricDomain, metricDomain
            };
            if (enableSoftConstraints)
            {
                constraintType.Signature.Add(satisfiedDomain);
            }

            tuples = new List <List <string> >();
            for (int a = metricDomain.MinValue; a < metricDomain.MaxValue + 1; a += metricDomain.StepWidth)
            {
                if (enableSoftConstraints)
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        // in some cases the non-satisfied tuples are added: *, *, 0
                        // tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.FalseValue });
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString(), a != b ? TrueValue : FalseValue
                        });
                    }
                }
                else
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        if (a != b)
                        {
                            tuples.Add(new List <string>()
                            {
                                a.ToString(), b.ToString()
                            });
                        }
                    }
                }
            }

            constraintType.Tuples = tuples;
            // Adds the constraint type to the CS3
            this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));

            // Creating the "Plus" (a + b = c) constraint type
            constraintType = new GKOConstraintType()
            {
                Id   = CTNamePlus,
                Name = CTNamePlus
            };
            constraintType.Signature = new List <GKODomainAbstract>()
            {
                metricDomain, metricDomain, metricDomain
            };
            if (enableSoftConstraints)
            {
                constraintType.Signature.Add(satisfiedDomain);
            }

            tuples = new List <List <string> >();
            for (int a = metricDomain.MinValue; a < metricDomain.MaxValue + 1; a += metricDomain.StepWidth)
            {
                if (enableSoftConstraints)
                {
                    for (int b = metricDomain.MinValue; b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        for (int c = metricDomain.MinValue; c < metricDomain.MaxValue + 1; c += metricDomain.StepWidth)
                        {
                            // in some cases the non-satisfied tuples are added: *, *, *, 0
                            // tuples.Add(new List<string>() { TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.WildcardValue, TmsManager.FalseValue });
                            tuples.Add(new List <string>()
                            {
                                a.ToString(), b.ToString(), c.ToString(), (a + b) == c ? TrueValue : FalseValue
                            });
                        }
                    }
                }
                else
                {
                    for (int b = metricDomain.MinValue; a + b < metricDomain.MaxValue + 1; b += metricDomain.StepWidth)
                    {
                        tuples.Add(new List <string>()
                        {
                            a.ToString(), b.ToString(), (a + b).ToString()
                        });
                    }
                }
            }

            constraintType.Tuples = tuples;
            // Adds the constraint type to the CS3
            this.ConstraintTypes.Add(constraintType.Name, constraintType.CreateIConstraintType(cdaStarTms, this.Domains));
        }
Пример #4
0
        ///// <summary>
        ///// Generates the domain used as index for the powerset of the enabled calculi
        ///// </summary>
        ///// <returns></returns>
        //private GKOIntDomain GenerateCalculiPwSetIxDomain()
        //{
        //    GKOIntDomain domain = new GKOIntDomain()
        //    {
        //        Id = DomainNameCalculiPwSetIx,
        //        Name = DomainNameCalculiPwSetIx,
        //        StepWidth = 1,
        //        MinValue = 0,
        //        MaxValue = (int)Math.Pow(2, StructuralRelationsManager.RelationFamilies.Max(x => x.Relations.Count))
        //    };

        //    return domain;
        //}

        #region Constraint Types

        /// <summary>
        /// Creates constraint types in the CS3, each new CT contains only one value from a domain.
        /// NOTE: These constraint types are used to allow only one value to be selected for a variable.
        /// </summary>
        /// <param name="domain">The domain to create constraint types for</param>
        /// <param name="addSoft">Specifies whether to add a softness variable at the end</param>
        private void CreateIndividualCTForDomain(GKODomainAbstract domain, bool addSoft)
        {
            List <GKOConstraintType> constraintTypes = new List <GKOConstraintType>();
            List <string>            domainValues    = new List <string>();

            if (domain is GKOIntDomain)
            {
                GKOIntDomain domainTemp = domain as GKOIntDomain;

                for (int i = domainTemp.MinValue; i < domainTemp.MaxValue + 1; i += domainTemp.StepWidth)
                {
                    domainValues.Add(i.ToString());
                }
            }
            else if (domain is GKODomain)
            {
                GKODomain domainTemp = domain as GKODomain;

                foreach (var value in domainTemp.Values)
                {
                    domainValues.Add(value);
                }
            }

            foreach (var value in domainValues)
            {
                GKOConstraintType ct = new GKOConstraintType()
                {
                    Id        = domain.GetIndividualValueCTName(value),
                    Name      = domain.GetIndividualValueCTName(value),
                    Signature = new List <GKODomainAbstract>()
                    {
                        domain
                    },
                    Tuples = new List <List <string> >()
                    {
                        new List <string>()
                        {
                            value
                        }
                    }
                };

                if (addSoft)
                {
                    ct.Signature.Add(this.BoolDomain);
                    ct.Tuples[0].Add(TrueValue);
                    foreach (var excludedValue in domainValues)
                    {
                        ct.Tuples.Add(new List <string>()
                        {
                            excludedValue, FalseValue
                        });
                    }
                }

                constraintTypes.Add(ct);
            }

            constraintTypes.ForEach(x => this.ConstraintTypes.Add(x.Name, x.CreateIConstraintType(cdaStarTms, this.Domains)));
        }
 /// <summary>
 /// Creates a new metric relation part
 /// </summary>
 /// <param name="value">The metric value.
 /// </param>
 /// <param name="metricDomainVal">The Domain for the value</param>
 public MetricDomainRelationPart(int value, GKOIntDomain metricDomainVal)
 {
     this.relPart = new MetricRelationPart(value, metricDomainVal);
 }