public static void Main(string[] args) { Matrix recipe = new DenseMatrix(recipe_data); using (Model M = new Model("Recipe")) { // "production" defines the amount of each product to bake. Variable production = M.Variable("production", new StringSet(productnames), Domain.GreaterThan(0.0)); // The objective is to maximize the total revenue. M.Objective("revenue", ObjectiveSense.Maximize, Expr.Dot(revenue, production)); // The prodoction is constrained by stock: M.Constraint(Expr.Mul(recipe, production), Domain.LessThan(stock)); M.SetLogHandler(Console.Out); // We solve and fetch the solution: M.Solve(); double[] res = production.Level(); Console.WriteLine("Solution:"); for (int i = 0; i < res.Length; ++i) { Console.WriteLine(" Number of {0} : {1}", productnames[i], res[i]); } Console.WriteLine(" Revenue : ${0}", res[0] * revenue[0] + res[1] * revenue[1]); } }
public static void Main(string[] args) { long timeout = 5; int n = 80; // number of binary variables int m = n / 5; // number of constraints int p = n / 5; // Each constraint picks p variables and requires that half of them are 1 Random R = new Random(1234); //TAG:begin-model using (Model M = new Model("SolveBinary")) { M.SetLogHandler(System.Console.Out); //Variable x = M.Variable("x", n, Domain.InRange(0,1)); Variable x = M.Variable("x", n, Domain.Binary()); M.Objective(ObjectiveSense.Minimize, Expr.Sum(x)); //M.SetSolverParam("numThreads",1); int[] idxs = new int[n]; for (int i = 0; i < n; ++i) { idxs[i] = i; } int[] cidxs = new int[p]; for (var i = 0; i < m; ++i) { nshuffle(R, idxs, p); Array.Copy(idxs, cidxs, p); M.Constraint(Expr.Sum(x.Pick(cidxs)), Domain.EqualsTo(p / 2)); } //TAG:end-model var B = new Breaker(M, timeout); //TAG:begin-create-thread var T = new Thread(new ThreadStart(B.run)); T.Start(); //TAG:end-create-thread M.Solve(); B.stopThread(); T.Join(); Console.WriteLine("End."); } }
//TAG:end-lj-inner //TAG:begin-lj-outer public static double[][] lownerjohn_outer(double[][] x) { using (Model M = new Model("lownerjohn_outer")) { // Direct log output to the terminal for debugging. M.SetLogHandler(Console.Out); int m = x.Length; int n = x[0].Length; // Setup variables Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0)); Variable P = M.Variable("P", new int[] { n, n }, Domain.Unbounded()); Variable c = M.Variable("c", n, Domain.Unbounded()); // (1, P(*xi+c)) \in Q for (int i = 0; i < m; ++i) { M.Constraint("qc" + i, Expr.Vstack(Expr.Ones(1), Expr.Sub(Expr.Mul(P, x[i]), c)), Domain.InQCone()); } // t <= det(P)^{1/n} det_rootn(M, P, t); // Objective: Maximize t M.Objective(ObjectiveSense.Maximize, t); M.Solve(); double[] Plvl = P.Level(); double[] clvl = c.Level(); double[][] Pc = new double[n + 1][]; for (int i = 0; i < n; ++i) { Pc[i] = new double[n]; System.Array.Copy(Plvl, i * n, Pc[i], 0, n); } Pc[n] = clvl; return(Pc); } }
//TAG:begin-lj-inner public static double[][] lownerjohn_inner(double[][] A, double[] b) { using (Model M = new Model("lownerjohn_inner")) { // Direct log output to the terminal for debugging. M.SetLogHandler(Console.Out); int m = A.Length; int n = A[0].Length; // Setup variables Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0)); Variable C = M.Variable("C", new int[] { n, n }, Domain.Unbounded()); Variable d = M.Variable("d", n, Domain.Unbounded()); // (bi - ai^T*d, C*ai) \in Q for (int i = 0; i < m; ++i) { M.Constraint("qc" + i, Expr.Vstack(Expr.Sub(b[i], Expr.Dot(A[i], d)), Expr.Mul(C, A[i])), Domain.InQCone()); } // t <= det(C)^{1/n} det_rootn(M, C, t); // Objective: Maximize t M.Objective(ObjectiveSense.Maximize, t); M.Solve(); double[] Clvl = C.Level(); double[] dlvl = d.Level(); double[][] Cres_d = new double[n + 1][]; for (int i = 0; i < n; ++i) { Cres_d[i] = new double[n]; System.Array.Copy(Clvl, i * n, Cres_d[i], 0, n); } Cres_d[n] = dlvl; return(Cres_d); } }
public static void Main(string[] args) { int m = 3; int n = m * m; //fixed cells in human readable (i.e. 1-based) format int[,] hr_fixed = { { 1, 5, 4 }, { 2, 2, 5 }, { 2, 3, 8 }, { 2, 6, 3 }, { 3, 2, 1 }, { 3, 4, 2 }, { 3, 5, 8 }, { 3, 7, 9 }, { 4, 2, 7 }, { 4, 3, 3 }, { 4, 4, 1 }, { 4, 7, 8 }, { 4, 8, 4 }, { 6, 2, 4 }, { 6, 3, 1 }, { 6, 6, 9 }, { 6, 7, 2 }, { 6, 8, 7 }, { 7, 3, 4 }, { 7, 5, 6 }, { 7, 6, 5 }, { 7, 8, 8 }, { 8, 4, 4 }, { 8, 7, 1 }, { 8, 8, 6 }, { 9, 5, 9 } }; int nf = hr_fixed.Length / 3; int[,] fixed_cells = new int[nf, 3]; for (int i = 0; i < nf; i++) { for (int d = 0; d < m; d++) { fixed_cells[i, d] = hr_fixed[i, d] - 1; } } using (Model M = new Model("SUDOKU")) { M.SetLogHandler(Console.Out); Variable x = M.Variable(new int[] { n, n, n }, Domain.Binary()); //each value only once per dimension for (int d = 0; d < m; d++) { M.Constraint(Expr.Sum(x, d), Domain.EqualsTo(1.0)); } //each number must appears only once in a block for (int k = 0; k < n; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { Variable block = x.Slice(new int[] { i *m, j *m, k }, new int[] { (i + 1) * m, (j + 1) * m, k + 1 }); M.Constraint(Expr.Sum(block), Domain.EqualsTo(1.0)); } } } M.Constraint(x.Pick(fixed_cells), Domain.EqualsTo(1.0)); M.Solve(); //print the solution, if any... if (M.GetPrimalSolutionStatus() == SolutionStatus.Optimal || M.GetPrimalSolutionStatus() == SolutionStatus.NearOptimal) { print_solution(m, x); } else { Console.WriteLine("No solution found!\n"); } } }
public static void Main(string[] args) { int ncols = 50; int nrows = 50; int seed = 0; double sigma = 1.0; int ncells = nrows * ncols; Random gen = new Random(seed); double[] f = new double[ncells]; for (int i = 0; i < ncells; i++) { double xx = Math.Sqrt(-2.0 * Math.Log(gen.NextDouble())); double yy = Math.Sin(2.0 * Math.PI * gen.NextDouble()); f[i] = Math.Max(Math.Min(1.0, xx * yy), .0); } //TAG:begin-tv-code //TAG:begin-tv-init Model M = new Model("TV"); try { Variable u = M.Variable(new int[] { nrows + 1, ncols + 1 }, Domain.InRange(0.0, 1.0)); Variable t = M.Variable(new int[] { nrows, ncols }, Domain.Unbounded()); //TAG:end-tv-init //TAG:begin-tv-core-grid Variable ucore = u.Slice(new int[] { 0, 0 }, new int[] { nrows, ncols }); //TAG:end-tv-core-grid //TAG:begin-tv-deltas Expression deltax = Expr.Sub(u.Slice(new int[] { 1, 0 }, new int[] { nrows + 1, ncols }), ucore); Expression deltay = Expr.Sub(u.Slice(new int[] { 0, 1 }, new int[] { nrows, ncols + 1 }), ucore); //TAG:end-tv-deltas //TAG:begin-tv-norms M.Constraint(Expr.Stack(2, t, deltax, deltay), Domain.InQCone().Axis(2)); //TAG:end-tv-norms //TAG:begin-tv-sigma Matrix mat_f = Matrix.Dense(nrows, ncols, f); M.Constraint(Expr.Vstack(sigma, Expr.Flatten(Expr.Sub(ucore, mat_f))), Domain.InQCone()); //TAG:end-tv-sigma /* * boundary conditions are not actually needed * M.constraint( Expr.sub( u.slice( [n-1,0] ,[n,m] ), u.slice([n,0],[n+1,m]) ), * Domain.equalsTo(0.)); * M.constraint( Expr.sub( u.slice( [0,n-1] ,[n,m] ), u.slice([0,n],[n,m+1]) ), * Domain.equalsTo(0.)); */ M.SetLogHandler(Console.Out); //TAG:begin-tv-obj-fun M.Objective(ObjectiveSense.Minimize, Expr.Sum(t)); //TAG:end-tv-obj-fun M.Solve(); } finally { M.Dispose(); } //TAG:end-tv-code }
public static double[][] lownerjohn_outer(double[][] x) { using( Model M = new Model("lownerjohn_outer") ) { // Direct log output to the terminal for debugging. M.SetLogHandler(Console.Out); int m = x.Length; int n = x[0].Length; // Setup variables Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0)); Variable P = M.Variable("P", new NDSet(n,n), Domain.Unbounded()); Variable c = M.Variable("c", n, Domain.Unbounded()); // (1, P(*xi+c)) \in Q for (int i = 0; i < m; ++i) M.Constraint( "qc" + i, Expr.Vstack(Expr.Ones(1), Expr.Sub(Expr.Mul(P,x[i]), c)), Domain.InQCone() ); // t <= det(P)^{1/n} det_rootn(M, P, t); // Objective: Maximize t M.Objective(ObjectiveSense.Maximize, t); M.Solve(); double[] Plvl = P.Level(); double[] clvl = c.Level(); double[][] Pc = new double[n+1][]; for (int i = 0; i < n; ++i) { Pc[i] = new double[n]; System.Array.Copy(Plvl,i*n, Pc[i],0, n); } Pc[n] = clvl; return Pc; } }
public static double[][] lownerjohn_inner(double[][] A, double[] b) { using( Model M = new Model("lownerjohn_inner")) { // Direct log output to the terminal for debugging. M.SetLogHandler(Console.Out); int m = A.Length; int n = A[0].Length; // Setup variables Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0)); Variable C = M.Variable("C", new NDSet(n,n), Domain.Unbounded()); Variable d = M.Variable("d", n, Domain.Unbounded()); // (bi - ai^T*d, C*ai) \in Q for (int i = 0; i < m; ++i) M.Constraint( "qc" + i, Expr.Vstack(Expr.Sub(b[i],Expr.Dot(A[i],d)), Expr.Mul(C,A[i])), Domain.InQCone() ); // t <= det(C)^{1/n} det_rootn(M, C, t); // Objective: Maximize t M.Objective(ObjectiveSense.Maximize, t); M.Solve(); double[] Clvl = C.Level(); double[] dlvl = d.Level(); double[][] Cres_d = new double[n+1][]; for (int i = 0; i < n; ++i) { Cres_d[i] = new double[n]; System.Array.Copy(Clvl,i*n,Cres_d[i],0, n); } Cres_d[n] = dlvl; return Cres_d; } }
public static void Main(string [] args) { double lb = 1.0; int m = 4; int n = 15; int seed = 0; double ub = 5.0; double[] T = new double[n]; Random gen = new Random(seed); for (int i = 0; i < n; i++) { T[i] = gen.NextDouble() * (ub - lb) + lb; } Array.Sort <double>(T); //TAG:begin-model Model M = new Model("Multi-processor scheduling"); Variable x = M.Variable("x", new int[] { m, n }, Domain.Binary()); Variable t = M.Variable("t", 1, Domain.Unbounded()); M.Constraint(Expr.Sum(x, 0), Domain.EqualsTo(1.0)); //TAG:begin-repeat M.Constraint(Expr.Sub(Var.Repeat(t, m), Expr.Mul(x, T)), Domain.GreaterThan(0.0)); //TAG:end-repeat M.Objective(ObjectiveSense.Minimize, t); //TAG:end-model //TAG:begin-lpt double [] init = new double[n * m]; double [] schedule = new double[m]; for (int i = 0; i < n; i++) { int next = 0; for (int j = 1; j < m; j++) { if (schedule[j] < schedule[next]) { next = j; } } schedule[next] += T[i]; init[next * n + i] = 1; } //TAG:end-lpt M.SetLogHandler(Console.Out); M.WriteTask("dump.opf"); M.SetSolverParam("mioTolRelGap", .01); M.Solve(); try { Console.WriteLine("\n\nInitial solution: "); for (int i = 0; i < m; i++) { Console.Write("M {0} [", i); for (int y = 0; y < n; y++) { Console.Write(init[i * n + y] + ", "); } Console.WriteLine("]"); } //TAG:begin-sol-input x.SetLevel(init); //TAG:end-sol-input Console.WriteLine("\n\nMOSEK solution:"); for (int i = 0; i < m; i++) { Console.Write("M {0} [", i); for (int y = 0; y < n; y++) { double value = x.Index(i, y).Level()[0]; Console.Write("{0}, ", (int)value); } Console.WriteLine("]"); } } catch (SolutionError e) { //must do something about.... } }
public static void Main(string[] args) { // Sample input data int n = 100; int d = 4; int k = 3; double[] b = { 9, 10, 11 }; double[][,] A = new double[n * k][, ]; var rand = new Random(); for (int i = 0; i < n * k; i++) { A[i] = new double[d, d]; for (int s1 = 0; s1 < d; s1++) { for (int s2 = 0; s2 <= s1; s2++) { A[i][s1, s2] = A[i][s2, s1] = rand.NextDouble(); } } } // Create a model with n semidefinite variables od dimension d x d using (Model M = new Model("sdo3")) { Variable X = M.Variable(Domain.InPSDCone(d, n)); // Pick indexes of diagonal entries for the objective int[,] alldiag = new int[d * n, 3]; for (int j = 0; j < n; j++) { for (int s = 0; s < d; s++) { alldiag[j * d + s, 0] = j; alldiag[j * d + s, 1] = alldiag[j * d + s, 2] = s; } } M.Objective(ObjectiveSense.Minimize, Expr.Sum(X.Pick(alldiag))); // Each constraint is a sum of inner products // Each semidefinite variable is a slice of X for (int i = 0; i < k; i++) { Expression[] addlist = new Expression[n]; for (int j = 0; j < n; j++) { addlist[j] = Expr.Dot(A[i * n + j], Slice(X, d, j)); } M.Constraint(Expr.Add(addlist), Domain.GreaterThan(b[i])); } // Solve M.SetLogHandler(Console.Out); // Add logging M.WriteTask("sdo3.ptf"); // Save problem in readable format M.Solve(); // Get results. Each variable is a slice of X Console.WriteLine("Contributing variables:"); for (int j = 0; j < n; j++) { double[] Xj = Slice(X, d, j).Level(); double maxval = 0; for (int s = 0; s < d * d; s++) { maxval = Math.Max(maxval, Xj[s]); } if (maxval > 1e-6) { Console.WriteLine("X" + j + "="); for (int s1 = 0; s1 < d; s1++) { for (int s2 = 0; s2 < d; s2++) { Console.Write(Xj[s1 * d + s1] + " "); } Console.WriteLine(); } } } } }
public static void Main(string [] args) { int n = 30; //Number of tasks int m = 6; //Number of processors double lb = 1.0; //The range of lengths of short tasks double ub = 5.0; double sh = 0.8; //The proportion of short tasks int n_short = (int)(n * sh); int n_long = n - n_short; double[] T = new double[n]; Random gen = new Random(0); for (int i = 0; i < n_short; i++) { T[i] = gen.NextDouble() * (ub - lb) + lb; } for (int i = n_short; i < n; i++) { T[i] = 20 * (gen.NextDouble() * (ub - lb) + lb); } Array.Sort <double>(T); Model M = new Model("Multi-processor scheduling"); Variable x = M.Variable("x", new int[] { m, n }, Domain.Binary()); Variable t = M.Variable("t", 1, Domain.Unbounded()); M.Constraint(Expr.Sum(x, 0), Domain.EqualsTo(1.0)); M.Constraint(Expr.Sub(Var.Repeat(t, m), Expr.Mul(x, T)), Domain.GreaterThan(0.0)); M.Objective(ObjectiveSense.Minimize, t); //Computing LPT solution double [] init = new double[n * m]; double [] schedule = new double[m]; for (int i = n - 1; i >= 0; i--) { int next = 0; for (int j = 1; j < m; j++) { if (schedule[j] < schedule[next]) { next = j; } } schedule[next] += T[i]; init[next * n + i] = 1; } //Comment this line to switch off feeding in the initial LPT solution x.SetLevel(init); M.SetLogHandler(Console.Out); M.SetSolverParam("mioTolRelGap", 0.01); M.Solve(); try { Console.Write("initial solution: \n"); for (int i = 0; i < m; i++) { Console.Write("M {0} [", i); for (int y = 0; y < n; y++) { Console.Write("{0}, ", (int)init[i * n + y]); } Console.Write("]\n"); } Console.Write("MOSEK solution:\n"); for (int i = 0; i < m; i++) { Console.Write("M {0} [", i); for (int y = 0; y < n; y++) { Console.Write("{0}, ", (int)x.Index(i, y).Level()[0]); } Console.Write("]\n"); } } catch (SolutionError e) {} }