/// <summary> /// Creates constraint type in the CS3, which allow only one out of several bool values to be selected at a time. /// Note: The procedure does not generate all tuples when the variable count is more than the allowed branches count for a domain constraint combination. When the variable count is greater than this value, the constraint is still usable for JEPD relations constraints /// </summary> /// <param name="variableCount">The number of variables of the CT</param> private void CreateExactlyOneCT(int variableCount) { GKOConstraintType ct = new GKOConstraintType(); ct.Id = variableCount.GetExactlyOneCTName(); ct.Name = variableCount.GetExactlyOneCTName(); ct.Signature = new List <GKODomainAbstract>(); ct.Tuples = new List <List <string> >(); // Creating the signature // There is one extra variable for the satisfaction of the constraint for (int i = 0; i < variableCount + 1; i++) { ct.Signature.Add(this.BoolDomain); } for (int tupleIx = 0; tupleIx < Math.Pow(2, variableCount); tupleIx++) { List <string> tuple = new List <string>(); bool relSatisfied = false; for (int i = 0; i < variableCount; i++) { // If the i-th bit is set then set True, otherwise False string var = (tupleIx & (1 << i)) == 0 ? TmsManager.FalseValue : TmsManager.TrueValue; tuple.Add(var); } // The relation is satisfied if only one of the elements in the tuple is True relSatisfied = tuple.Where(x => x == TmsManager.TrueValue).Count() == 1; tuple.Add(relSatisfied ? TmsManager.TrueValue : TmsManager.FalseValue); ct.Tuples.Add(tuple); } // Remove some of the variables to increase the performance, if they are not used for Domain constraint combinations if (variableCount > StructuralReasonerOptions.DomainConstraintMaximumBranches) { List <string> notSatisfiedTuple = new List <string>(); // Remove all tuples which do not match the condition ct.Tuples = ct.Tuples.Where(x => x.Last() == TmsManager.TrueValue).ToList(); // Generating and adding the only not-satisfied tuple = (N+1)*False for (int i = 0; i < variableCount + 1; i++) { notSatisfiedTuple.Add(TmsManager.FalseValue); } ct.Tuples.Add(notSatisfiedTuple); } this.ConstraintTypes.Add(ct.Name, ct.CreateIConstraintType(cdaStarTms, this.Domains)); }
/// <summary> /// Creates constraint type in the CS3, which requires that at least one element is set to True. /// </summary> /// <param name="variableCount">The number of variables of the CT</param> private void CreateOrCT(int variableCount) { GKOConstraintType ct = new GKOConstraintType(); ct.Id = variableCount.GetOrCTName(); ct.Name = variableCount.GetOrCTName(); ct.Signature = new List <GKODomainAbstract>(); ct.Tuples = new List <List <string> >(); if (variableCount > StructuralReasonerOptions.DomainConstraintMaximumBranches) { throw new InvalidOperationException(string.Format("Cannot create OR TMS Constraint Type with more elements ({0}) than the allowed number of branches in a Domain constraint ({1})!", variableCount, StructuralReasonerOptions.DomainConstraintMaximumBranches)); } // Creating the signature // There is one extra variable for the satisfaction of the constraint for (int i = 0; i < variableCount + 1; i++) { ct.Signature.Add(this.BoolDomain); } for (int tupleIx = 0; tupleIx < Math.Pow(2, variableCount); tupleIx++) { List <string> tuple = new List <string>(); bool relSatisfied = false; for (int i = 0; i < variableCount; i++) { // If the i-th bit is set then set True, otherwise False string var = (tupleIx & (1 << i)) == 0 ? TmsManager.FalseValue : TmsManager.TrueValue; tuple.Add(var); } // The relation is satisfied if at least one of the elements in the tuple is True relSatisfied = tuple.Any(x => x == TmsManager.TrueValue); tuple.Add(relSatisfied ? TmsManager.TrueValue : TmsManager.FalseValue); ct.Tuples.Add(tuple); } this.ConstraintTypes.Add(ct.Name, ct.CreateIConstraintType(cdaStarTms, this.Domains)); }
///// <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)); }
///// <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))); }