public override void FromStorage(Storage.Storage storage, Storage.Records.Optimizer optimizer) { base.FromStorage(storage, optimizer); storage.Query("SELECT `id` FROM `constraints` ORDER BY `id`", delegate(IDataReader reader) { ConstraintMatrix cons = new ConstraintMatrix(Job.Optimizer.Configuration.PopulationSize); int consid = reader.GetInt32(0); storage.Query("SELECT `parameter` FROM `constraint_parameters` WHERE `constraint` = @0", delegate(IDataReader rd) { string name = reader.GetString(0); cons.Add(Job.Optimizer.Parameter(name)); d_constraintsFor[name] = cons; return(true); }, consid); storage.Query("SELECT `id`, `equality`, `value` FROM `constraint_equations` WHERE `constraint` = @0 ORDER BY `id`", delegate(IDataReader eqreader) { int eqid = eqreader.GetInt32(0); bool equality = eqreader.GetInt32(1) == 1; double val = eqreader.GetDouble(2); Linear.Vector coefficients = new Linear.Vector(); storage.Query("SELECT `value` FROM `constraint_coefficients` WHERE `equation` = @0 ORDER BY `id`", delegate(IDataReader cfreader) { coefficients.Add(cfreader.GetDouble(0)); return(true); }, eqid); cons.Add(new Linear.Constraint(equality, coefficients, val)); return(true); }, consid); d_constraints.Add(cons); return(true); }); }
private void AddConstraint(XmlNode node) { XmlNodeList lst = node.SelectNodes("parameters/parameter"); List <string> pars = new List <string>(); if (lst.Count == 0) { XmlNode parameters = node.SelectSingleNode("parameters"); if (parameters != null && !String.IsNullOrEmpty(parameters.InnerText.Trim())) { pars.AddRange(Array.ConvertAll(parameters.InnerText.Trim().Split(','), item => item.Trim())); } } else { foreach (XmlNode p in lst) { pars.Add(p.InnerText.Trim()); } } if (pars.Count == 0) { throw new Exception("No parameters were specified"); } ConstraintMatrix constr = new ConstraintMatrix(Job.Optimizer.Configuration.PopulationSize); foreach (string pname in pars) { Parameter p = Job.Optimizer.Parameter(pname); if (p == null) { throw new Exception(String.Format("The parameter `{0}' could not be found", pname)); } constr.Add(p); } foreach (XmlNode eq in node.SelectNodes("equation")) { XmlAttribute val = eq.Attributes["value"]; double v = 0; bool isequality = true; if (val != null) { Biorob.Math.Expression expr; Biorob.Math.Expression.Create(val.Value.Trim(), out expr); v = expr.Evaluate(Biorob.Math.Constants.Context); } XmlAttribute equality = eq.Attributes["equality"]; if (equality != null) { isequality = (equality.Value.Trim() == "yes"); } string[] coefs = Array.ConvertAll(eq.InnerText.Split(','), item => item.Trim()); if (coefs.Length != pars.Count) { throw new Exception(String.Format("The number of coefficients is not equal to the number of parameters (expected {0}, but got {1})", pars.Count, coefs.Length)); } Linear.Vector coefficients = new Linear.Vector(coefs.Length); for (int i = 0; i < coefs.Length; ++i) { Biorob.Math.Expression expr; Biorob.Math.Expression.Create(coefs[i].Trim(), out expr); coefficients.Add(expr.Evaluate(Biorob.Math.Constants.Context)); } constr.Add(new Linear.Constraint(isequality, coefficients, v)); } if (!constr.Solve()) { throw new Exception("Could not solve system of linear constraints!"); } foreach (string param in constr.Parameters) { if (d_constraintsFor.ContainsKey(param)) { throw new Exception(String.Format("The parameter `{0}' is already part of another constraint...", param)); } } d_constraints.Add(constr); foreach (string param in constr.Parameters) { d_constraintsFor[param] = constr; } }