private void ReadAllDifferentConstraint(XmlNode constraintNode) { var constraintIdAttribute = constraintNode.Attributes["id"]; var constraintId = constraintIdAttribute.Value; var constraintName = string.Empty; var expression = string.Empty; for (var i = 0; i < constraintNode.ChildNodes.Count; i++) { var childNode = constraintNode.ChildNodes[i]; switch (childNode.Name) { case "name": constraintName = childNode.InnerText; break; case "expression": expression = childNode.InnerText; break; } } var constraintModel = new AllDifferentConstraintModel(_model, new ModelName(constraintName), new AllDifferentConstraintExpressionModel(expression)); constraintModel.Id = Convert.ToInt32(constraintId); _model.AddConstraint(constraintModel); }
/// <summary> /// Map the all different constraint model into the or-tools solver. /// </summary> /// <param name="allDifferentConstraint">All different constraint model.</param> internal void ProcessConstraint(AllDifferentConstraintModel allDifferentConstraint) { var theVector = this.cache.GetVectorByName(allDifferentConstraint.Expression.Text); var orAllDifferentConstraint = this.solver.MakeAllDifferent(theVector); this.solver.Add(orAllDifferentConstraint); }
public AllDifferentConstraintModelItemViewModel(AllDifferentConstraintModel theAllDifferentModel, IWindowManager theWindowManager) : base(theAllDifferentModel) { Validator = new AllDifferentConstraintModelItemViewModelValidator(); AllDifferentConstraint = theAllDifferentModel; DisplayName = AllDifferentConstraint.Name; ExpressionText = AllDifferentConstraint.Expression.Text; _windowManager = theWindowManager; }
public WorkspaceBuilder WithConstraintAllDifferent(string theExpression) { if (string.IsNullOrWhiteSpace(theExpression)) { throw new ArgumentException(nameof(theExpression)); } var newConstraint = new AllDifferentConstraintModel(_model, new AllDifferentConstraintExpressionModel(theExpression)); _model.AddConstraint(newConstraint); return(this); }
private void WriteConstraint(AllDifferentConstraintModel allDifferentConstraint, XmlElement constraintsRoot) { var allDifferentElement = Document.CreateElement("all-different-constraint"); var idAttribute = Document.CreateAttribute("id"); idAttribute.Value = Convert.ToString(allDifferentConstraint.Id); allDifferentElement.Attributes.Append(idAttribute); var nameElement = Document.CreateElement("name"); var encodedNameNode = Document.CreateCDataSection(allDifferentConstraint.Name); nameElement.AppendChild(encodedNameNode); allDifferentElement.AppendChild(nameElement); var expressionElement = Document.CreateElement("expression"); var encodedExpressionNode = Document.CreateCDataSection(allDifferentConstraint.Expression.Text); expressionElement.AppendChild(encodedExpressionNode); allDifferentElement.AppendChild(expressionElement); constraintsRoot.AppendChild(allDifferentElement); }
private void CreateArcFrom(AllDifferentConstraintModel constraint) { var solverAggregateVariable = _modelSolverMap.GetSolverAggregateVariableByName(constraint.Expression.Text); /* * Iterate through all variables inside the aggregate and ensure * that each variable is not equal to all variables to their * right. The not equal operator is commutative so there is no need * to express the constraint the other way around. */ for (var i = 0; i < solverAggregateVariable.Variables.Count; i++) { for (var z = i + 1; z < solverAggregateVariable.Variables.Count; z++) { var expressionText = $"${solverAggregateVariable.Name}[{i}] <> ${solverAggregateVariable.Name}[{z}]"; var interpreter = new ConstraintExpressionParser(); var parseResult = interpreter.Parse(expressionText); Debug.Assert(parseResult.IsSuccess); _constraintNetwork.AddArc(_arcBuilder.Build(parseResult.Root)); } } }