/// <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); }
/*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); } }
/// <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); }