Beispiel #1
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);
     }
 }
Beispiel #2
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);
            }
        }
Beispiel #3
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);
        }