// 1-norm // t >= sum( |x_i| ), x is a vector Variable public static void norm1(Model M, Variable t, Variable x) { Variable u = M.Variable(x.GetShape(), Domain.Unbounded()); abs(M, u, x); M.Constraint(Expr.Sub(t, Expr.Sum(u)), Domain.GreaterThan(0.0)); }
private void FetchTest_Check(Variable v, int[] shape) { Assert.IsFalse(v.DataSet.HasChanges); Assert.IsTrue(v.DataSet.IsAutocommitEnabled); Assert.AreEqual(shape.Length, v.Rank); for (int i = 0; i < v.Rank; i++) { Assert.AreEqual(shape[i], v.GetShape()[i]); } var data = v.GetData(); int[] index = new int[v.Rank]; for (int i = 0; i < data.Length; i++) { Assert.IsTrue(((double)data.GetValue(index)) > 0); for (int j = v.Rank; --j >= 0;) { index[j]++; if (index[j] < shape[j]) { break; } index[j] = 0; } } Assert.IsNotNull(v.MissingValue); Assert.IsNotNull(v.Metadata["Units", SchemaVersion.Recent]); Assert.IsNotNull(v.Metadata["Name", SchemaVersion.Recent]); Assert.IsNotNull(v.Metadata["long_name", SchemaVersion.Recent]); }
// Cardinality bound // At most k of entries in x are nonzero, assuming in advance |x_i|<=1. public static void card(Model M, Variable x, int k) { Variable t = M.Variable(x.GetShape(), Domain.Binary()); abs(M, t, x); M.Constraint(Expr.Sum(t), Domain.LessThan(k)); }
// Semicontinuous variable // x = 0 or a <= x <= b public static void semicontinuous(Model M, Variable x, double a, double b) { Variable u = M.Variable(x.GetShape(), Domain.Binary()); M.Constraint(Expr.Sub(x, Expr.Mul(a, u)), Domain.GreaterThan(0.0)); M.Constraint(Expr.Sub(x, Expr.Mul(b, u)), Domain.LessThan(0.0)); }
/** * Purpose: Models the hypograph of the n-th power of the * determinant of a positive definite matrix. * * The convex set (a hypograph) * * C = { (X, t) \in S^n_+ x R | t <= det(X)^{1/n} }, * * can be modeled as the intersection of a semidefinite cone * * [ X, Z; Z^T Diag(Z) ] >= 0 * * and a number of rotated quadratic cones and affine hyperplanes, * * t <= (Z11*Z22*...*Znn)^{1/n} (see geometric_mean). * * References: * [1] "Lectures on Modern Optimization", Ben-Tal and Nemirovski, 2000. */ public static void det_rootn(Model M, Variable X, Variable t) { int n = X.GetShape().Dim(0); // Setup variables Variable Y = M.Variable(Domain.InPSDCone(2 * n)); // Setup Y = [X, Z; Z^T diag(Z)] Variable Y11 = Y.Slice(new int[] { 0, 0 }, new int[] { n, n }); Variable Y21 = Y.Slice(new int[] { n, 0 }, new int[] { 2 * n, n }); Variable Y22 = Y.Slice(new int[] { n, n }, new int[] { 2 * n, 2 * n }); M.Constraint(Expr.Sub(Expr.MulElm(Matrix.Eye(n), Y21), Y22), Domain.EqualsTo(0.0)); M.Constraint(Expr.Sub(X, Y11), Domain.EqualsTo(0.0)); // t^n <= (Z11*Z22*...*Znn) Variable[] tmpv = new Variable[n]; for (int i = 0; i < n; ++i) { tmpv[i] = Y22.Index(i, i); } Variable z = Var.Reshape(Var.Vstack(tmpv), n); geometric_mean(M, z, t); }
public static long TotalLength(this Variable var) { if (var == null) { throw new ArgumentNullException("var"); } return(var.GetShape().Select(i => (long)i).Aggregate((i, j) => i * j)); }
public static Point[][] SplitSecondDimension(Variable variable) { if (variable == null) throw new ArgumentNullException("variable"); if (variable.Rank != 2) throw new ArgumentException("Only 2d variables are supported"); int n = variable.GetShape()[1]; int m = variable.GetShape()[0]; Array d = variable.GetData(); Point[][] pts = new Point[n][]; for (int i = 0; i < n; i++) { List<Point> tmp = new List<Point>(m); if (variable.TypeOfData == typeof(float)) { float[,] dt = (float[,])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) if (!Double.IsNaN(dt[j, i])) tmp.Add(new Point(j, dt[j, i])); } else { float mv; try { mv = Convert.ToSingle(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to float: " + exc.Message); mv = float.NaN; } for (int j = 0; j < m; j++) if (dt[j, i] != mv) tmp.Add(new Point(j, dt[j, i])); } } else if (variable.TypeOfData == typeof(double)) { double[,] dt = (double[,])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) if (!Double.IsNaN(dt[j, i])) tmp.Add(new Point(j, dt[j, i])); } else { double mv; try { mv = Convert.ToDouble(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to double: " + exc.Message); mv = double.NaN; } for (int j = 0; j < m; j++) if (dt[j, i] != mv) tmp.Add(new Point(j, dt[j, i])); } } else { object mv = variable.GetMissingValue(); for (int j = 0; j < m; j++) { object obj = d.GetValue(j, i); if (!Object.Equals(obj, mv)) tmp.Add(new Point(j, Convert.ToDouble(obj))); } } pts[i] = tmp.ToArray(); } return pts; }
static void PrintData(Variable v, string range, string format) { try { // Find out which data to take int rank = v.Rank; var shape = v.GetShape(); var origin = rank == 0 ? null : new int[rank]; var count = rank == 0 ? null : new int[rank]; var stride = rank == 0 ? null : new int[rank]; for (int i = 0; i < rank; i++) { origin[i] = 0; count[i] = shape[i]; stride[i] = 1; } if (range != null) { // parse range specification 'from:to' or 'from:step:to' var ranges = range.Split(','); for (int i = 0; i < rank && i < ranges.Length; i++) { var fromto = ranges[i].Split(':'); if (!int.TryParse(fromto[0], out origin[i])) { origin[i] = 0; } if (origin[i] < 0) { origin[i] = 0; } if (origin[i] >= shape[i]) { origin[i] = shape[i] - 1; } if (fromto.Length < 2) { count[i] = 1; } else { if (!int.TryParse(fromto[fromto.Length - 1], out count[i])) { count[i] = 0; } if (count[i] == 0 || count[i] >= shape[i]) { count[i] = shape[i] - 1; } if (count[i] < origin[i]) { count[i] = origin[i]; } if (fromto.Length > 2) { if (!int.TryParse(fromto[1], out stride[i])) { stride[i] = 1; } if (stride[i] <= 0) { stride[i] = 1; } } count[i] = 1 + (count[i] - origin[i]) / stride[i]; } } } // Now get the data Array data = v.GetData(origin, stride, count); if (data == null || (rank > 0 && data.Rank != rank)) { Console.Error.WriteLine("No data"); } else { int prefix = string.Join(",", Array.ConvertAll(shape, i => i.ToString())).Length + 2; // parse format int cell = 8; string frm = "{0,8}"; if (format != null) { int p = format.IndexOf(':'); if (p < 0) { if (!int.TryParse(format, out cell)) { cell = 8; } frm = "{0," + cell + "}"; } else { if (!int.TryParse(format.Substring(0, p), out cell)) { cell = 8; } frm = "{0," + cell + format.Substring(p) + "}"; } } // Now print the data if (rank == 0) // scalar variable { Console.WriteLine(); string item = string.Format(frm, data.GetValue(0)); // check that item fits a cell if (item.Length > cell) { item = item.Substring(0, cell - 1) + "#"; } Console.Write(" "); Console.Write(item); } else // has rank > 0 (array) { int[] indices = new int[data.Rank]; int[] varIndices = new int[rank]; int bufferWidth = Math.Max(80, Console.BufferWidth); // .. to allow for non-console output string lineSeparator = Environment.NewLine; while (indices[0] < data.GetLength(0)) { // print last dimension Console.Write(lineSeparator); lineSeparator = string.Empty; int lastIndex = data.Rank - 1; indices[lastIndex] = 0; while (indices[lastIndex] < data.GetLength(lastIndex)) { // print line of cells // print prefix of current indices for (int i = 0; i < rank; i++) { varIndices[i] = origin[i] + indices[i] * stride[i]; } string item = ("[" + string.Join(",", Array.ConvertAll(varIndices, i => i.ToString())) + "]").PadRight(prefix); Console.Write(item); int left = bufferWidth - item.Length; while (left > cell && indices[lastIndex] < data.GetLength(lastIndex)) { item = string.Format(frm, data.GetValue(indices)); // check that item fits a cell if (item.Length > cell) { item = item.Substring(0, cell - 1) + "#"; } Console.Write(" "); Console.Write(item); left -= item.Length + 1; indices[lastIndex]++; varIndices[lastIndex] += stride[lastIndex]; } Console.WriteLine(); if (indices[lastIndex] < data.GetLength(lastIndex)) { lineSeparator = Environment.NewLine; } } lastIndex -= 1; // increment recursively other dimensions while (lastIndex >= 0 && ++indices[lastIndex] >= data.GetLength(lastIndex)) { if (lastIndex > 0) { indices[lastIndex] = 0; } lastIndex -= 1; } } } } Console.WriteLine(); } catch (Exception e) { Console.Error.WriteLine("Error while printing data: " + e.Message); } }
static void PrintData(Variable v, string range, string format) { try { // Find out which data to take int rank = v.Rank; var shape = v.GetShape(); var origin = rank == 0 ? null : new int[rank]; var count = rank == 0 ? null : new int[rank]; var stride = rank == 0 ? null : new int[rank]; for (int i = 0; i < rank; i++) { origin[i] = 0; count[i] = shape[i]; stride[i] = 1; } if (range != null) { // parse range specification 'from:to' or 'from:step:to' var ranges = range.Split(','); for (int i = 0; i < rank && i < ranges.Length; i++) { var fromto = ranges[i].Split(':'); if (!int.TryParse(fromto[0], out origin[i])) origin[i] = 0; if (origin[i] < 0) origin[i] = 0; if (origin[i] >= shape[i]) origin[i] = shape[i] - 1; if (fromto.Length < 2) count[i] = 1; else { if (!int.TryParse(fromto[fromto.Length - 1], out count[i])) count[i] = 0; if (count[i] == 0 || count[i] >= shape[i]) count[i] = shape[i] - 1; if (count[i] < origin[i]) count[i] = origin[i]; if (fromto.Length > 2) { if (!int.TryParse(fromto[1], out stride[i])) stride[i] = 1; if (stride[i] <= 0) stride[i] = 1; } count[i] = 1 + (count[i] - origin[i]) / stride[i]; } } } // Now get the data Array data = v.GetData(origin, stride, count); if (data == null || (rank > 0 && data.Rank != rank)) Console.Error.WriteLine("No data"); else { int prefix = string.Join(",", Array.ConvertAll(shape, i => i.ToString())).Length + 2; // parse format int cell = 8; string frm = "{0,8}"; if (format != null) { int p = format.IndexOf(':'); if (p < 0) { if (!int.TryParse(format, out cell)) cell = 8; frm = "{0," + cell + "}"; } else { if (!int.TryParse(format.Substring(0, p), out cell)) cell = 8; frm = "{0," + cell + format.Substring(p) + "}"; } } // Now print the data if (rank == 0) // scalar variable { Console.WriteLine(); string item = string.Format(frm, data.GetValue(0)); // check that item fits a cell if (item.Length > cell) item = item.Substring(0, cell - 1) + "#"; Console.Write(" "); Console.Write(item); } else // has rank > 0 (array) { int[] indices = new int[data.Rank]; int[] varIndices = new int[rank]; int bufferWidth = Math.Max(80, Console.BufferWidth); // .. to allow for non-console output string lineSeparator = Environment.NewLine; while (indices[0] < data.GetLength(0)) { // print last dimension Console.Write(lineSeparator); lineSeparator = string.Empty; int lastIndex = data.Rank - 1; indices[lastIndex] = 0; while (indices[lastIndex] < data.GetLength(lastIndex)) { // print line of cells // print prefix of current indices for (int i = 0; i < rank; i++) varIndices[i] = origin[i] + indices[i] * stride[i]; string item = ("[" + string.Join(",", Array.ConvertAll(varIndices, i => i.ToString())) + "]").PadRight(prefix); Console.Write(item); int left = bufferWidth - item.Length; while (left > cell && indices[lastIndex] < data.GetLength(lastIndex)) { item = string.Format(frm, data.GetValue(indices)); // check that item fits a cell if (item.Length > cell) item = item.Substring(0, cell - 1) + "#"; Console.Write(" "); Console.Write(item); left -= item.Length + 1; indices[lastIndex]++; varIndices[lastIndex] += stride[lastIndex]; } Console.WriteLine(); if (indices[lastIndex] < data.GetLength(lastIndex)) lineSeparator = Environment.NewLine; } lastIndex -= 1; // increment recursively other dimensions while (lastIndex >= 0 && ++indices[lastIndex] >= data.GetLength(lastIndex)) { if (lastIndex > 0) indices[lastIndex] = 0; lastIndex -= 1; } } } } Console.WriteLine(); } catch (Exception e) { Console.Error.WriteLine("Error while printing data: " + e.Message); } }
public static Point[][] SplitFirstDimension(Variable variable) { if (variable == null) { throw new ArgumentNullException("variable"); } if (variable.Rank != 2) { throw new ArgumentException("Only 2d variables are supported"); } int n = variable.GetShape()[0]; int m = variable.GetShape()[1]; Array d = variable.GetData(); Point[][] pts = new Point[n][]; for (int i = 0; i < n; i++) { List <Point> tmp = new List <Point>(m); if (variable.TypeOfData == typeof(float)) { float[,] dt = (float[, ])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) { if (!Double.IsNaN(dt[i, j])) { tmp.Add(new Point(j, dt[i, j])); } } } else { float mv; try { mv = Convert.ToSingle(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to Double: " + exc.Message); mv = float.NaN; } for (int j = 0; j < m; j++) { if (dt[i, j] != mv) { tmp.Add(new Point(j, dt[i, j])); } } } } else if (variable.TypeOfData == typeof(double)) { double[,] dt = (double[, ])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) { if (!Double.IsNaN(dt[i, j])) { tmp.Add(new Point(j, dt[i, j])); } } } else { double mv = Convert.ToDouble(mvObj); try { mv = Convert.ToDouble(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to double: " + exc.Message); mv = double.NaN; } for (int j = 0; j < m; j++) { if (dt[i, j] != mv) { tmp.Add(new Point(j, dt[i, j])); } } } } else { object mv = variable.GetMissingValue(); for (int j = 0; j < m; j++) { object obj = d.GetValue(i, j); if (!Object.Equals(obj, mv)) { tmp.Add(new Point(j, Convert.ToDouble(obj))); } } } pts[i] = tmp.ToArray(); } return(pts); }