protected bool FindSolutionByBacktracking(int variableIndex)
        {
            if (variableIndex == Variables.Count)
            {
                OnSolutionFound?.Invoke(Variables);
                return(true);
            }

            foreach (var val in Variables[variableIndex].Domain)
            {
                Variables[variableIndex].Current = val;
                Counter++;
                if (Constraints.All(con => con.Check.Invoke()))
                {
                    var solutionFound = FindSolutionByBacktracking(variableIndex + 1);
                    if (solutionFound)
                    {
                        return(true);
                    }
                }
                else
                {
                    Variables[variableIndex].Current = default;
                }
            }

            return(false);
        }
예제 #2
0
 /// <summary>
 /// Executes the operation on the specified value.
 /// </summary>
 /// <param name="Value">The value.</param>
 /// <returns>The result of the operation</returns>
 public T Execute(T Value)
 {
     if (!Constraints.All(x => x.Eval((T)Value)))
     {
         return(Value);
     }
     return(Operation.Execute((T)Value));
 }
예제 #3
0
 /// <summary>
 /// Executes the Workflow on the specified value.
 /// </summary>
 /// <param name="Value">The value.</param>
 /// <returns>The result of the Workflow</returns>
 public T Execute(T Value)
 {
     if (!Constraints.All(x => x.Eval(Value)))
     {
         return(Value);
     }
     return(Workflow.Start(Value));
 }
예제 #4
0
 /// <summary>
 /// Determines whether the specified <see cref="System.Object"/>, is equal to this instance.
 /// </summary>
 /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
 /// <returns>
 /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance;
 /// otherwise, <c>false</c>.
 /// </returns>
 public override bool Equals(object obj)
 {
     return((obj is Table Item) &&
            Columns.All(x => Item.Columns.Contains(x)) &&
            Name == Item.Name &&
            Constraints.All(x => Item.Constraints.Contains(x)) &&
            Triggers.All(x => Item.Triggers.Contains(x)));
 }
예제 #5
0
파일: Agent.cs 프로젝트: pascalpfeil/ssharp
 public bool ValidateConstraints()
 {
     return(Constraints.All(constraint => constraint()));
 }
예제 #6
0
 private bool MatchConstraints(RouteValueDictionary values)
 {
     return(Constraints == null || Constraints.All(constraint => constraintsMatchers[constraint.Key](SafeGetValueAsString(constraint.Key, values))));
 }
예제 #7
0
 public override bool CanUseIndex()
 {
     return(Constraints.All(constraint => ((IInternalConstraint)constraint).CanUseIndex()));
 }
예제 #8
0
 public override bool Match(object @object)
 {
     return(Constraints.All(constraint => ((IInternalConstraint)constraint).Match(@object)));
 }
예제 #9
0
 /// <summary>
 /// Checks the constraints for a given substitution (in the form of a list of keyvalue pairs of names and values)
 /// </summary>
 /// <param name="substititution">The substitution to be tested against the constraints of the action</param>
 /// <returns>True if the substitution fulfills all constraints, false otherwise</returns>
 private bool CheckConstraints(Dictionary <string, object> substititution) => Constraints.All(x => x(substititution));
예제 #10
0
 protected bool ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection)
 {
     return(Constraints == null || Constraints.All(constraintsItem => ProcessConstraint(httpContext, constraintsItem.Value, constraintsItem.Key, values, routeDirection)));
 }
예제 #11
0
        private TypeDescriptor GetElement0Type()
        {
            if (_sample == null)
            {
                var etype = CILType.GetElementType();
                return(etype == null ? null : new TypeDescriptor(etype));
            }

            if (CILType.IsArray)
            {
                Array array = (Array)_sample;
                if (array.Length == 0)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }

                object sample = array.GetValue(new int[array.Rank]);
                if (sample == null)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }
                else
                {
                    return(new TypeDescriptor(sample));
                }
            }
            else if (CILType.IsByRef)
            {
                return(new TypeDescriptor(_sample));
            }
            else if (Constraints.Length > 0)
            {
                var indexers = CILType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                               .Where(p => p.GetIndexParameters().Length == Constraints.Length &&
                                      p.GetIndexParameters().All(ip => ip.ParameterType == typeof(int)));
                if (indexers.Any())
                {
                    var indexer = indexers.First();
                    if (Constraints.All(c => c.Size > 0))
                    {
                        object[] index = Constraints.Select(r => (object)r.FirstBound).ToArray();
                        try
                        {
                            var indexSample = indexer.GetValue(_sample, index);
                            return(TypeDescriptor.GetTypeOf(indexSample));
                        }
                        catch (Exception)
                        {
                            return(indexer.PropertyType);
                        }
                    }
                    else
                    {
                        return(indexer.PropertyType);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        // int => indeks variabla, int - indeks wartosci z dziedziny
        protected bool FindSolutionForwardChecking(int variableIndex)
        {
            var values = Variables[variableIndex].MaskedDomain;

            foreach (var val in values)
            {
                Variables[variableIndex].Current = val;
                Counter++;
                if (Constraints.All(con => con.Check.Invoke()))
                {
                    if (Variables.All(v => v.Current != null))
                    {
                        OnSolutionFound?.Invoke(Variables);
                        return(true);
                    }

                    // prune
                    var neighborVariables = new List <(IVariable <T>, BinaryConstraint <T>)>();
                    Constraints.ForEach(c =>
                    {
                        var cast = c as BinaryConstraint <T>;
                        if (cast != null && cast.VariableA == Variables[variableIndex])
                        {
                            neighborVariables.Add((cast.VariableB, cast));
                        }

                        if (cast != null && cast.VariableB == Variables[variableIndex])
                        {
                            neighborVariables.Add((cast.VariableA, cast));
                        }
                    });
                    neighborVariables = neighborVariables.Where(v => v.Item1.Current == null).ToList();

                    var prunedValues = new List <(int, int)>();
                    foreach (var pair in neighborVariables)
                    {
                        int idx      = Variables.IndexOf(pair.Item1);
                        var variable = Variables[idx];
                        if (variable.Current == null)
                        {
                            for (int i = 0; i < variable.Domain.Count; i++)
                            {
                                if (variable.DomainMask[i])
                                {
                                    variable.Current = variable.Domain[i];
                                    //counter++;
                                    if (!pair.Item2.Check())
                                    {
                                        variable.DomainMask[i] = false;
                                        prunedValues.Add((idx, i));
                                    }
                                    variable.Current = default;
                                }
                            }

                            pair.Item1.Current = default;
                        }
                    }
                    // end of prune

                    // HEURISTICS
                    int newIndex;
#if H_DOM_SIZE
                    var chosenVar = Variables
                                    .Where(v => v.Current == null)
                                    .OrderBy(v => v.MaskedDomain.Count)
                                    .First();
                    newIndex = Variables.IndexOf(chosenVar);
#elif H_MCV
                    var chosenVar = Variables
                                    .Where(v => v.Current == null)
                                    .OrderBy(v => Constraints.Sum(c =>
                    {
                        var cast = c as BinaryConstraint <T>;
                        if (cast == null)
                        {
                            return(0);
                        }
                        else if (cast.VariableA == v || cast.VariableB == v)
                        {
                            return(1);
                        }

                        return(0);
                    }))
                                    .First();
                    newIndex = Variables.IndexOf(chosenVar);
#elif H_LCV
                    var chosenVar = Variables
                                    .Where(v => v.Current == null)
                                    .OrderByDescending(v => Constraints.Sum(c =>
                    {
                        var cast = c as BinaryConstraint <T>;
                        if (cast == null)
                        {
                            return(0);
                        }
                        else if (cast.VariableA == v || cast.VariableB == v)
                        {
                            return(1);
                        }

                        return(0);
                    }))
                                    .First();
                    newIndex = Variables.IndexOf(chosenVar);
#else
                    newIndex = variableIndex + 1;
#endif

                    var solutionFound = FindSolutionForwardChecking(newIndex);
                    if (solutionFound)
                    {
                        return(true);
                    }
                    else
                    {
                        // revert prune
                        foreach (var pair in prunedValues)
                        {
                            Variables[pair.Item1].DomainMask[pair.Item2] = true;
                        }
                        // end of revert prune
                    }
                }
                else
                {
                    Variables[variableIndex].Current = default;
                }
            }

            Variables[variableIndex].Current = default;

            return(false);
        }