Пример #1
0
        /// <summary>
        /// Wird aufgerufen, wenn die Suche beginnt.
        /// </summary>
        public virtual void OnStartSearch()
        {
            // Ein ConstraintProblem hat nur einen Startknoten
            ConstraintConfiguration startConfiguration = (ConstraintConfiguration)FirstNodes[0].Data;

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Node) == true ||
                _consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Arc) == true ||
                _consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Bounds) == true)
            {
                SetCurrentConfiguration(startConfiguration);
            }

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Bounds) == true)
            {
                if (CheckBoundsConsistency(startConfiguration) == false)
                {
                    throw new ConsistencyException("The constraint has no solution, because there doesn't exist a bounds consistency.");
                }
            }
            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Node) == true)
            {
                if (CheckNodeConsistency(startConfiguration, true) == false)
                {
                    throw new ConsistencyException("The constraint has no solution, because there doesn't exist a node consistency.");
                }
            }
            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Arc) == true)
            {
                if (CheckArcConsistency(startConfiguration) == false)
                {
                    throw new ConsistencyException("The constraint has no solution, because there doesn't exist an arc consistency.");
                }
            }
        }
Пример #2
0
        /*protected bool CheckLimitConsistency(ConstraintConfiguration configuration)
         * {
         *  foreach (Constraint constraint in _constraintList.GetConstraints())
         *  {
         *      constraint.setCurrentConfiguration(configuration);
         *      if (!configuration.CheckLimitConsistency(constraint))
         *          return false;
         *  }
         *  return true;
         * }*/

        /// <summary>
        /// Prüft, ob die übergebene Konfiguration kantenkonsistent ist.
        /// </summary>
        /// <remarks>
        /// Ein binäres Constraint c(v1, v2) ist kantenkonsistent, wenn das Constraint für alle Belegungen b1 aus dem Wertebereich der Variablen v1 und für alle Belegungen b2 aus dem Wertebereich der Variablen v2 erfüllt ist.
        /// Eine Konfiguration ist kantenkonsistent, wenn alle enthaltenen Constraints kantenkonsistent sind.
        /// Anhand dieser Definition finden Beschränkungen im Wertebereich der Variablen von der übergebenen Konfiguration statt.
        /// </remarks>
        /// <param name="configuration">Die übergebene Konfiguration, die es zu testen gilt.</param>
        /// <returns>Gibt true zurück, wenn die Konfiguration kantenkonsistent ist, sonst false.</returns>
        protected bool CheckArcConsistency(ConstraintConfiguration configuration)
        {
            return(configuration.AC3(this));

            /*bool changing = true;
             * bool tempChanging = false;
             *
             * // Performance Test
             * int counter = 0;
             *
             * while (changing == true)
             * {
             *  changing = false;
             *  tempChanging = false;
             *  foreach (Constraint constraint in _constraintList.GetConstraints())
             *  {
             *      if (constraint.CountUnAssignedVars == 2)
             *      {
             *          counter++;
             *          if (!configuration.CheckEdgeConsistency(constraint, ref tempChanging))
             *              return false;
             *      }
             *      if (tempChanging == true)
             *      {
             *          changing = true;
             *      }
             *  }
             * }
             *
             * _log.Debug("Edge: counter is " + counter.ToString());
             *
             * return true;*/
        }
Пример #3
0
        public bool CheckAllConsistency()
        {
            // Ein ConstraintProblem hat nur einen Startknoten
            ConstraintConfiguration startConfiguration = (ConstraintConfiguration)FirstNodes[0].Data;

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Node) == true ||
                _consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Arc) == true ||
                _consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Bounds) == true)
            {
                SetCurrentConfiguration(startConfiguration);
            }

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Bounds) == true)
            {
                if (CheckBoundsConsistency(startConfiguration) == false)
                {
                    return(false);
                }
            }
            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Node) == true)
            {
                if (CheckNodeConsistency(startConfiguration, true) == false)
                {
                    return(false);
                }
            }
            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.Start, ConsistencyType.Arc) == true)
            {
                if (CheckArcConsistency(startConfiguration) == false)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #4
0
 /// <summary>
 /// Alle Constraints werden mit den Werten der übergebenen Konfiguration initialisiert.
 /// </summary>
 /// <remarks>
 /// Die Randbedingungen gehören nicht zu einer Konfiguration. Das erspart deren Kopieren bei Erstellung einer
 /// neuen Konfiguration. Stattdessen werden die Randbedingungen vor deren Prüfungen mit der zu prüfenden Konfiguration
 /// initialisier.
 /// </remarks>
 /// <param name="configuration">Mit dieser Konfiguration werden die Randbedingungen initialisiert.</param>
 protected void SetCurrentConfiguration(ConstraintConfiguration configuration)
 {
     foreach (Constraint constraint in _constraintList.GetConstraints())
     {
         constraint.SetCurrentConfiguration(configuration);
     }
 }
Пример #5
0
 /// <summary>
 /// Das Constraint wird mit den Variablen und  Werten der übergebenen Konfiguration initialisiert.
 /// </summary>
 /// <remarks>
 /// Die Randbedingung gehört nicht zu einer Konfiguration. Das erspart deren Kopieren bei Erstellung einer
 /// neuen Konfiguration. Stattdessen wird die Randbedingung vor deren Prüfung mit der zu prüfenden Konfiguration
 /// initialisier.
 /// </remarks>
 /// <param name="configuration">Mit dieser Konfiguration wird die Randbedingung initialisiert.</param>
 public void SetCurrentConfiguration(ConstraintConfiguration configuration)
 {
     foreach (VariablesOperator varOperator in _varOperators)
     {
         varOperator.Var = configuration.GetVariable(varOperator.Name);
     }
 }
Пример #6
0
        /// <summary>
        /// Erzeugt einen neune Kindknoten, dem die Konfiguration zugewiesen wird.
        /// </summary>
        /// <remarks>Der Typ des Knotens wird über NodeType festgelegt.</remarks>
        /// <param name="parentNode">Der Elternknoten.</param>
        /// <param name="configuration">Die Konfiguration, die von dem neuen Knoten ummantelt wird.</param>
        /// <returns>Der erzeugte Kindknoten.</returns>
        public INode CreateNode(INode parentNode, ConstraintConfiguration configuration)
        {
            switch (_nodeType)
            {
            case NodeType.Node:
                if (parentNode != null)
                {
                    return(new Node(parentNode, configuration));
                }
                else
                {
                    return(new Node(configuration));
                }

            case NodeType.TreeNode:
                if (parentNode != null)
                {
                    return(new TreeNode(parentNode as TreeNode, configuration));
                }
                else
                {
                    return(new TreeNode(configuration));
                }

            default:
                if (parentNode != null)
                {
                    return(new Node(parentNode, configuration));
                }
                else
                {
                    return(new Node(configuration));
                }
            }
        }
        //protected Random _random = new Random(0);

        /// <summary>
        /// Liefert die nächste zu belegende Variable zurück.
        /// </summary>
        /// <remarks>Die Auswahl findet dabei
        /// über die MRV-Heuristic statt(Minimum Remaining Values).
        /// Haben mehrere Variablen die gleiche Anzahl verbleibender Werte, dann
        /// wird die GradHeuristic genutzt, d.h. es wird die Variable genommen, die in
        /// den meisten Randbedingungen vorkommt.
        /// </remarks>
        /// <param name="configuration">Die Konfiguration, die noch nicht belegte Variablen enthält.</param>
        /// <param name="constraintList">Die Liste mit den Randbedingungen.</param>
        /// <returns>
        /// Gibt die als nächstes zu belegende Variable zurück.
        /// </returns>
        public Variable GetHeuristicVariable(ConstraintConfiguration configuration, ConstraintList constraintList)
        {
            Variable        result          = null;
            int             minDomainValues = 0;
            List <Variable> mrvVariables    = new List <Variable>();

            //// Zufall
            //SortedList<double, Variable> sortedList = new SortedList<double, Variable>(configuration.Variables.GetLength(0));
            //foreach (Variable var in configuration.Variables)
            //{
            //    sortedList.Add(_random.NextDouble(), var);
            //}

            //// search the variables with the minimum remaining values
            //foreach(Variable var in sortedList.Values)
            foreach (Variable var in configuration.Variables)
            {
                if (!var.HasAsignedValue)
                {
                    if (result == null)
                    {
                        result          = var;
                        minDomainValues = var.Domain.Count;
                        mrvVariables.Add(var);
                    }
                    else if (minDomainValues > var.Domain.Count)
                    {
                        mrvVariables.Clear();
                        result          = var;
                        minDomainValues = var.Domain.Count;
                        mrvVariables.Add(var);
                    }
                    else if (minDomainValues == var.Domain.Count)
                    {
                        mrvVariables.Add(var);
                    }
                }
            }

            // are there more then one variables we should use the variable with the most constraints
            if (mrvVariables.Count != 1)
            {
                int countConstraints    = 0;
                int varCountConstraints = 0;

                foreach (Variable var in mrvVariables)
                {
                    varCountConstraints = constraintList.GetConstraints(var.Name).Count;
                    if (varCountConstraints > countConstraints)
                    {
                        result           = var;
                        countConstraints = varCountConstraints;
                    }
                }
            }
            return(result);
        }
Пример #8
0
 /// <summary>
 /// Prüft, ob die übergebene Konfiguration knotenkonsistent ist.
 /// <remarks>
 /// Ein unäres (einstelliges) Constraint c(v) ist knotenkonsistent, wenn das Constraint für alle Belegungen b aus dem Wertebereich der Variablen v erfüllt ist.
 /// Eine Konfiguration ist knotenkonsistent, wenn alle enthaltenen Constraints knotenkonsistent sind.
 /// Anhand dieser Definition finden Beschränkungen im Wertebereich der Variablen von der übergebenen Konfiguration statt.
 /// </remarks>
 /// </summary>
 /// <param name="configuration">Die übergebene Konfiguration, die es zu testen gilt.</param>
 /// <param name="onlyOneUnassignedVar">
 /// Gibt an, ob nur unäre Constraints überprüft werden sollen,
 /// oder ob auch Constraints überprüft werden, die zwar mehr Variablen enthalten, wo
 /// aber nur eine davon noch nicht belegt ist.
 /// </param>
 /// <returns>Gibt true zurück, wenn die Konfiguration knotenkonsistent ist, sonst false.</returns>
 protected bool CheckNodeConsistency(ConstraintConfiguration configuration, bool onlyOneUnassignedVar)
 {
     foreach (Constraint constraint in _constraintList.GetConstraints())
     {
         if (!configuration.CheckNodeConsistency(constraint, onlyOneUnassignedVar))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #9
0
 /// <summary>
 /// Todo: Ändern dieser Vorgehensweise
 /// Kleiner Trick, um das Ergebnis der Zielfunktion auch anzeigbar zu machen.
 /// </summary>
 private void AddOptVariable()
 {
     if (_solutionList.Count == 1)
     {
         ConstraintConfiguration constel = (ConstraintConfiguration)_solutionList[0].Data;
         //Variable var = new Variable("objFunction", new Domain(0, 0), constel);
         Variable var = new Variable("objFunction", new Domain());
         constel.AddVariable(var);
         var.Value = _objectiveConstraint.ObjectiveValue;
     }
 }
        /// <summary>
        /// Liefert den Wert der heuristischen Funktion für den übergebenen Knoten.
        /// </summary>
        /// <param name="node">Der Knoten, dessen Heuristik ermittelt werden soll.</param>
        /// <param name="searchProblem">Das Problem, dass es zu lösen gilt.</param>
        /// <param name="searchMethod">Die Suchmethode, die gerade abläuft.</param>
        /// <returns>Gibt den Wert der Heuristik zurück.</returns>
        public double GetHeuristicValue(OKSearchRoom.INode node, IHeuristicSearchProblem searchProblem, ISearchMethod searchMethod)
        {
            double result = 0;

            if (searchProblem is ConstraintOptimizationProblem)
            {
                ConstraintConfiguration       configuration = (ConstraintConfiguration)node.Data;
                ConstraintOptimizationProblem optProblem    = searchProblem as ConstraintOptimizationProblem;

                if (optProblem.SolutionList.Count == 0)
                {
                    return(configuration.GetCountDomainValues());
                }
                else
                {
                    OptimizationConstraint optConstraint = optProblem.GetObjectiveFunction();
                    optConstraint.SetCurrentConfiguration(configuration);
                    IOperation operation = optConstraint.ObjectiveFunction;

                    if (operation.DoOperation(out result) != true)
                    {
                        return(-double.MaxValue);
                    }
                    else
                    {
                        //return configuration.GetCountDomainValues();
                        if (operation.GetType() == typeof(Minimum))
                        {
                            return(result + configuration.GetCountDomainValues());
                        }
                        //return configuration.GetCountDomainValues();
                        //return (configuration.GetCountDomainValues()+result);
                        //return (result + configuration.GetCountDomainValues());
                        //return (result + _objectiveConstraint.ObjectiveValue * configuration.GetCountDomainValues());
                        //return configuration.GetCountDomainValues();
                        //return -(_objectiveConstraint.ObjectiveValue - result) + configuration.GetCountDomainValues() * result;
                        //return -(_objectiveConstraint.ObjectiveValue - result) + configuration.GetCountDomainValues() * _objectiveConstraint.ObjectiveValue;
                        //return -node.Depth * (_objectiveConstraint.ObjectiveValue - result);
                        //return result - _objectiveConstraint.ObjectiveValue;
                        //return -node.Depth * (result - _objectiveConstraint.ObjectiveValue);
                        else
                        {
                            //return node.Depth * (_objectiveConstraint.ObjectiveValue - result);
                            return(configuration.GetCountDomainValues() - result);
                        }
                    }
                }
            }
            return(result);
        }
Пример #11
0
 /// <summary>
 /// Der Kopierkonstruktor.
 /// </summary>
 /// <param name="configuration">Die Konfiguration von der kopiert wird.</param>
 public ConstraintConfiguration(ConstraintConfiguration configuration)
 {
     foreach (Variable var in configuration._variablesList.Values)
     {
         // Eine zugewiesene Variable braucht nicht kopiert werden
         if (var.HasAsignedValue)
         {
             _variablesList.Add(var.Name, var);
         }
         else
         {
             Variable newVar = new Variable(var);
             _variablesList.Add(newVar.Name, newVar);
         }
     }
 }
Пример #12
0
        /// <summary>
        /// todo: Muss noch mehr ausgereizt werden. Hier können anhand von arithmetischen Gleichungen oder
        /// Ungleichungen und deren Eigenschaften Beschränkungen am Wertebereich durchgeführt werden
        /// </summary>
        /// <remarks>
        /// Es finden Beschränkungen im Wertebereich der Variablen von der übergebenen Konfiguration statt.
        /// </remarks>
        /// <example>
        /// Aus x1 + x2 &lt;= 7 wird
        /// x1Max = 7 - x2Min und
        /// x2Max = 7 - X1Min
        /// </example>
        /// <param name="configuration">Die übergebene Konfiguration, die es zu testen gilt.</param>
        /// <returns>Gibt true zurück, wenn die Konfiguration grenzenkonsistent ist, sonst false.</returns>
        protected bool CheckBoundsConsistency(ConstraintConfiguration configuration)
        {
            bool changing     = true;
            bool tempChanging = false;

            while (changing == true)
            {
                changing     = false;
                tempChanging = false;
                foreach (Constraint constraint in _constraintList.GetConstraints())
                {
                    if (ConstraintForms.CheckBoundsConsistency(constraint, ref tempChanging) == false)
                    {
                        return(false);
                    }
                    if (tempChanging == true)
                    {
                        changing = true;
                    }
                }
            }
            return(true);
        }
Пример #13
0
        /// <summary>
        /// Diese Methode wird aufgerufen, wenn eine Konfiguration gefunden wurde, wo jeder Variable der
        /// Konfiguration ein Wert zugewiesen wurde und dabei keine Randbedingung verletzt wurde.
        /// <param name="destination">Der Zielknoten, der die Konfiguration ummantelt</param>
        /// <param name="searchMethod">Die Suchmethode wird mit übergeben</param>
        /// <returns>Wird true zurückgegeben, endet die Suche, ansonsten wird weiter gesucht.</returns>
        /// </summary>
        public virtual bool OnFoundDestination(INode destination, ISearchMethod searchMethod)
        {
            // Es muss nicht geprüft werden, ob die Configuration schon als Lösung exisitert,
            // da der Suchraum zu neuen Variablen immer von einer Variablenbelegung ausgeht.
            ConstraintConfiguration configuration = destination.Data as ConstraintConfiguration;

            foreach (Node node in _solutionList)
            {
                ConstraintConfiguration alreadyFoundConfiguration = node.Data as ConstraintConfiguration;
                if (alreadyFoundConfiguration.ToString() == configuration.ToString())
                {
                    return(false);
                }
            }

            // Lösung hinzufügen
            _solutionList.Add(destination as Node);
            // Prüfen, ob schon genug Lösungen gefunden worden
            if (_solutionList.Count == _countSolutions)
            {
                return(true);
            }
            return(false);
        }
Пример #14
0
 /// <summary>
 /// Der Kopierkonstrukor.
 /// </summary>
 /// <param name="configuration">Die Konfiguration von der kopiert wird.</param>
 public ConstraintOptimizationProblem(ConstraintConfiguration configuration)
     : base(configuration)
 {
     _heuristicValue = new ConstraintOptimizationHeuristic();
 }
Пример #15
0
        /*public OptimizationConstraint OptConstraint
         * {
         *  get
         *  {
         *      return _objectiveConstraint;
         *  }
         * }*/

        /// <summary>
        /// Für den übergebenen Knoten werden neue Unterknoten generiert.
        /// </summary>
        /// <returns>Gibt die Menge der generierten Knoten als Array zurück.</returns>
        /// <param name="node">Anhand des übergebenen Knotens werden neue Unterknoten
        /// generiert.</param>
        /// <param name="maxCount">Beschränkt die Anzahl der Unterknoten.</param>
        public override INode[] GenerateChildren(INode node, int maxCount)
        {
            try
            {
                ConstraintConfiguration parentConfiguration = (ConstraintConfiguration)node.Data;
                Variable     nextVar        = _heuristicVariable.GetHeuristicVariable(parentConfiguration, _constraintList);
                List <INode> generatedNodes = new List <INode>();

                /*if (_destination != null)
                 * {
                 *  if (!CheckEdgeConsistency(parentConfiguration))
                 *      return new Node[0]; ;
                 * }*/

                /*if (!CheckNodeConsistency(parentConfiguration, true))
                 * {
                 *  return new Node[0];
                 * }*/

                //// Zufall
                //SortedList<double, double> sortedList = new SortedList<double, double>(nextVar.Domain.Count);
                //foreach (double value in nextVar.Domain)
                //{
                //    sortedList.Add(_random.NextDouble(), value);
                //}

                //foreach (double value in sortedList.Values)
                foreach (double value in nextVar.Domain)
                {
                    ConstraintConfiguration configuration = new ConstraintConfiguration(parentConfiguration);

                    Variable currentVar = configuration.GetVariable(nextVar.Name);
                    currentVar.Value = value;

                    List <Constraint> constraints = _constraintList.GetConstraints(nextVar.Name);
                    bool operationFailed          = false;
                    foreach (Constraint cons in constraints)
                    {
                        cons.SetCurrentConfiguration(configuration);
                        //if (cons.IsComplied == false)
                        //{
                        //    operationFailed = true;
                        //    break;
                        //}
                        if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachNodeGenerating, ConsistencyType.Node) &&
                            configuration.CheckNodeConsistency(cons, false) == false)
                        {
                            operationFailed = true;
                            break;
                        }
                        else if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachNodeGenerating, ConsistencyType.Arc) == true &&
                                 CheckArcConsistency(configuration) == false)
                        {
                            operationFailed = true;
                            break;
                        }

                        /*else if (!configuration.CheckLimitConsistency(cons))
                         * {
                         *  operationFailed = true;
                         *  break;
                         * }*/
                        /*else if (_destination != null && !configuration.CheckEdgeConsistency(cons, ref operationFailed))
                         * {
                         *  operationFailed = true;
                         *  break;
                         * }
                         * operationFailed = false;*/
                    }
                    if (!operationFailed)
                    {
                        // Prüfe zusätzlich die Zielfunktion der Optimierung, wenn schon eine Lösung existiert
                        if (_solutionList.Count != 0)
                        {
                            _objectiveConstraint.SetCurrentConfiguration(configuration);
                            if (_objectiveConstraint.IsComplied == false)
                            {
                                operationFailed = true;
                            }
                        }
                    }
                    if (!operationFailed)
                    {
                        // Die Domaene kann gelöscht werden
                        currentVar.Domain = null;
                        generatedNodes.Add(CreateNode(node, configuration));

                        /*if (generatedNodes.Count > 10)
                         *  break;*/
                        //configuration.Show();
                        //_log.Debug("OptConfiguration: " + configuration.ToString());
                    }
                }


                INode[] result = new INode[generatedNodes.Count];
                generatedNodes.CopyTo(result);
                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #16
0
        /// <summary>
        /// Dies Funktion wird aufgerufen, wenn eine Lösung gefunden wurde.
        /// </summary>
        /// <param name="destination">Gibt den Knoten an, der die Lösungskonfiguration ummantelt.</param>
        /// <param name="searchMethod">
        /// Gibt die Suchmethode an, mit der die Lösung gefunden wurde.
        /// <remarks>
        /// Dies ist notwendig, um den Zugriff auf die Knotenmenge der Suchmethode zu erhalten und diese dann ändern zu können.
        /// </remarks>
        /// </param>
        /// <returns></returns>
        public override bool OnFoundDestination(INode destination, ISearchMethod searchMethod)
        {
            // Bei einem Optimierungsproblem wird sich nur die beste Lösung gemerkt
            if (_solutionList.Count == 0)
            {
                _solutionList.Add(destination as Node);
            }
            else
            {
                _solutionList[0] = destination as Node;
            }
            ConstraintConfiguration configuration = (ConstraintConfiguration)destination.Data;

            _objectiveConstraint.SetCurrentConfiguration(configuration);
            _objectiveConstraint.SetObjectiveValue();

            List <INode> newNodeSet = new List <INode>();

            /*foreach (INode node in searchMethod.NodeSet)
             * {
             *  configuration = (ConstraintConfiguration)node.Data;
             *  _objectiveConstraint.setCurrentConfiguration(configuration);
             *  if (_objectiveConstraint.IsComplied)
             *  {
             *      if (CheckNodeConsistency(configur5ation, false) == true)
             *          newNodeSet.Add(node);
             *      else
             *          configuration = null;
             *  }
             * }*/

            configuration = (ConstraintConfiguration)_begin.Data;

            if (_eventHandler != null)
            {
                _eventHandler.FoundEvent(searchMethod, this, destination, _objectiveConstraint.ObjectiveValue);
            }

            SetCurrentConfiguration(configuration);

            if (CheckBoundsConsistency(configuration) == false)
            {
                AddOptVariable();
                return(true);
            }

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachSolution, ConsistencyType.Node) == true &&
                CheckNodeConsistency(configuration, false) == false)
            {
                AddOptVariable();
                return(true);
            }

            if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachSolution, ConsistencyType.Arc) == true &&
                CheckArcConsistency(configuration) == false)
            {
                AddOptVariable();
                return(true);
            }

            //if (CheckLimitConsistency(configuration) == false)
            //    return true;

            Logger.LogDebug("##### Node the begin node has the following configuration");
            Logger.LogDebug(((ConstraintConfiguration)_begin.Data).ToString());

            newNodeSet.Add(_begin);

            Logger.LogDebug("##### Arc the begin node has the following configuration");
            Logger.LogDebug(((ConstraintConfiguration)_begin.Data).ToString());

            searchMethod.NodeSet = newNodeSet;

            return(false);
        }
Пример #17
0
 /// <summary>
 /// Der Konstruktor.
 /// <remarks>
 /// Dem Konstruktor wird eine AusgangsKonfiguration übergeben, die alle Variablen und deren Wertebereiche enthält.
 /// Diese Variablen sollen dann mit festen Werten belegt werden.
 /// </remarks>
 /// </summary>
 /// <param name="configuration">AusgangsKonfiguration</param>
 public ConstraintProblem(ConstraintConfiguration configuration)
 {
     _begin = CreateNode(null, configuration);
     ConstraintForms.Init();
 }
Пример #18
0
        public double GetHeuristicValue(OKSearchRoom.INode node, IHeuristicSearchProblem searchProblem, ISearchMethod searchMethod)
        {
            ConstraintConfiguration configuration = (ConstraintConfiguration)node.Data;

            return(configuration.GetCountDomainValues());
        }
Пример #19
0
        ///// <summary>
        ///// Liefert den Wert der heuristischen Funktion für den übergebenen Knoten.
        ///// </summary>
        ///// <returns>Gibt den Wert der Heuristik zurück.</returns>
        ///// <param name="node">Der Knoten, dessen Heuristik ermittelt werden soll.</param>
        //public virtual double GetHeuristic(INode node)
        //{
        //    ConstraintConfiguration configuration = (ConstraintConfiguration)node.Data;
        //    return configuration.GetCountDomainValues();
        //}

        /// <summary>
        /// Vergleicht die aktuelle Konfiguration mit der ZielKonfiguration. Gibt true
        /// zurück, wenn beide übereinstimmen sonst false.
        /// </summary>
        /// <param name="node"></param>
        public virtual bool CompareNodes(INode node)
        {
            ConstraintConfiguration configuration = (ConstraintConfiguration)node.Data;

            return(configuration.CompleteAssigned);
        }
Пример #20
0
        /// <summary>
        /// Für den übergebenen Knoten werden neue Unterknoten generiert.
        /// </summary>
        /// <param name="node">Anhand des übergebenen Knotens werden neue Unterknoten
        /// generiert.</param>
        /// <param name="maxCount">Beschränkt die Anzahl der Unterknoten.</param>
        /// <returns>
        /// Gibt die Menge der generierten Knoten als Array zurück.
        /// </returns>
        public virtual INode[] GenerateChildren(INode node, int maxCount)
        {
            ConstraintConfiguration parentConfiguration = (ConstraintConfiguration)node.Data;
            Variable     nextVar        = _heuristicVariable.GetHeuristicVariable(parentConfiguration, _constraintList);
            List <INode> generatedNodes = new List <INode>();

            if (nextVar == null)
            {
                throw new Exception("nextVar is null");
            }

            //// Zufall
            //List<KeyValuePair<double, double>> sortedList = new List<KeyValuePair<double, double>>(nextVar.Domain.Count);
            //foreach (double value in nextVar.Domain)
            //{
            //    sortedList.Add(new KeyValuePair<double,double>(_random.NextDouble(), value));
            //}
            //sortedList.Sort();

            //foreach (KeyValuePair<double,double> pair in sortedList)
            foreach (double value in nextVar.Domain)
            {
                ConstraintConfiguration configuration = new ConstraintConfiguration(parentConfiguration);
                Variable currentVar = configuration.GetVariable(nextVar.Name);
                currentVar.Value = value;

                List <Constraint> constraints = _constraintList.GetConstraints(nextVar.Name);
                bool operationFailed          = false;
                foreach (Constraint cons in constraints)
                {
                    bool tempChanging = false;
                    cons.SetCurrentConfiguration(configuration);
                    // todo: Ist diese Abfrage wirklich notwendig?
                    //if (cons.IsComplied == false)
                    //{
                    //    operationFailed = true;
                    //    break;
                    //}
                    if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachNodeGenerating, ConsistencyType.Bounds) && ConstraintForms.CheckBoundsConsistency(cons, ref tempChanging) == false)
                    {
                        operationFailed = true;
                        break;
                    }
                    else if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachNodeGenerating, ConsistencyType.Node) && configuration.CheckNodeConsistency(cons, false) == false)
                    {
                        operationFailed = true;
                        break;
                    }
                    else if (_consistencyOptions.GetCheckConsistencyActive(ConsistencyCheckRegion.EachNodeGenerating, ConsistencyType.Arc) && CheckArcConsistency(configuration) == false)
                    {
                        operationFailed = true;
                        break;
                    }
                }
                if (!operationFailed)
                {
                    // Die Domaene kann gelöscht werden
                    currentVar.Domain = null;
                    generatedNodes.Add(CreateNode(node, configuration));
                    //configuration.Show();
                }
            }


            INode[] result;
            result = new INode[generatedNodes.Count];

            generatedNodes.CopyTo(result);
            return(result);
        }