public static void Main (String[] args) { try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("[task]")); task.readdata(args[0]); task.putintparam(mosek.iparam.optimizer, mosek.Val.optimizer_concurrent); task.putintparam(mosek.iparam.concurrent_num_optimizers, 2); task.optimize(); task.solutionsummary(mosek.streamtype.msg); } } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } }
public static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Missing arguments, syntax is:"); Console.WriteLine(" opt_server_sync inputfile host port"); } else { String inputfile = args[0]; String host = args[1]; String port = args[2]; mosek.rescode trm; using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream(mosek.streamtype.log, new msgclass()); task.readdata(inputfile); task.optimizermt(host, port, out trm); task.solutionsummary(mosek.streamtype.log); } } } }
/** Example of how to use ``paropt``. * Optimizes tasks whose names were read from command line. */ public static void Main(string[] argv) { int n = argv.Length; mosek.Task[] t = new mosek.Task[n]; mosek.rescode[] res = new mosek.rescode[n]; mosek.rescode[] trm = new mosek.rescode[n]; using (var env = new mosek.Env()) { for (int i = 0; i < n; i++) { t[i] = new mosek.Task(env); t[i].readdata(argv[i]); // Each task will be single-threaded t[i].putintparam(mosek.iparam.intpnt_multi_thread, mosek.onoffkey.off); } paropt(t, res, trm); for (int i = 0; i < n; i++) { Console.WriteLine("Task {0} res {1} trm {2} obj_val {3} time {4}", i, res[i], trm[i], t[i].getdouinf(mosek.dinfitem.intpnt_primal_obj), t[i].getdouinf(mosek.dinfitem.optimizer_time)); } } }
public static void Main(String[] args) { try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.readdata(args[0]); task.putintparam(mosek.iparam.optimizer, mosek.Val.optimizer_concurrent); task.putintparam(mosek.iparam.concurrent_num_optimizers, 2); task.optimize(); task.solutionsummary(mosek.streamtype.msg); } } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); throw; } }
public static void Main(String[] args) { string filename = "../data/feasrepair.lp"; if (args.Length >= 1) { filename = args[0]; } using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env, 0, 0)) { task.set_Stream(mosek.streamtype.log, new msgclass()); task.readdata(filename); task.putintparam(mosek.iparam.log_feas_repair, 3); task.primalrepair(null, null, null, null); double sum_viol = task.getdouinf(mosek.dinfitem.primal_repair_penalty_obj); Console.WriteLine("Minimized sum of violations = %{0}", sum_viol); task.optimize(); task.solutionsummary(mosek.streamtype.msg); } } }
public static void Main(String[] args) { mosek.Env env = null; mosek.Task task = null; mosek.Task[] task_list = new mosek.Task[] { null }; try { // Create mosek environment. env = new mosek.Env(); // Create a task object linked with the environment env. task = new mosek.Task(env, 0, 0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("simplex: ")); task_list[0] = new mosek.Task(env, 0, 0); task_list[0].set_Stream(mosek.streamtype.log, new msgclass("intrpnt: ")); task.readdata(args[0]); // Assign different parameter values to each task. // In this case different optimizers. task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.primal_simplex); task_list[0].putintparam(mosek.iparam.optimizer, mosek.optimizertype.intpnt); // Optimize task and task_list[0] in parallel. // The problem data i.e. C, A, etc. // is copied from task to task_list[0]. task.optimizeconcurrent(task_list); task.solutionsummary(mosek.streamtype.log); } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); } if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } }
public static void Main (String[] args) { mosek.Env env = null; mosek.Task task = null; mosek.Task[] task_list = new mosek.Task[] { null }; try { // Create mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, 0,0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("simplex: ")); task_list[0] = new mosek.Task (env, 0,0); task_list[0].set_Stream (mosek.streamtype.log, new msgclass ("intrpnt: ")); task.readdata(args[0]); // Assign different parameter values to each task. // In this case different optimizers. task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.primal_simplex); task_list[0].putintparam(mosek.iparam.optimizer, mosek.optimizertype.intpnt); // Optimize task and task_list[0] in parallel. // The problem data i.e. C, A, etc. // is copied from task to task_list[0]. task.optimizeconcurrent(task_list); task.solutionsummary(mosek.streamtype.log); } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); } if (task != null) task.Dispose (); if (env != null) env.Dispose (); }
public static void Main() { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env, 0, 0)) { Console.WriteLine("Test MOSEK parameter get/set functions"); // Set log level (integer parameter) task.putintparam(mosek.iparam.log, 1); // Select interior-point optimizer... (integer parameter) task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.intpnt); // ... without basis identification (integer parameter) task.putintparam(mosek.iparam.intpnt_basis, mosek.basindtype.never); // Set relative gap tolerance (double parameter) task.putdouparam(mosek.dparam.intpnt_co_tol_rel_gap, 1.0e-7); // The same using explicit string names task.putparam("MSK_DPAR_INTPNT_CO_TOL_REL_GAP", "1.0e-7"); task.putnadouparam("MSK_DPAR_INTPNT_CO_TOL_REL_GAP", 1.0e-7); // Incorrect value try { task.putdouparam(mosek.dparam.intpnt_co_tol_rel_gap, -1.0); } catch (mosek.Error) { Console.WriteLine("Wrong parameter value"); } double param = task.getdouparam(mosek.dparam.intpnt_co_tol_rel_gap); Console.WriteLine("Current value for parameter intpnt_co_tol_rel_gap = " + param); /* Define and solve an optimization problem here */ /* task.optimize() */ /* After optimization: */ Console.WriteLine("Get MOSEK information items"); double tm = task.getdouinf(mosek.dinfitem.optimizer_time); int iter = task.getintinf(mosek.iinfitem.intpnt_iter); Console.WriteLine("Time: " + tm); Console.WriteLine("Iterations: " + iter); } } }
public static void Main(String[] args) { mosek.Env env = null; mosek.Task task = null; try { // Create mosek environment. env = new mosek.Env(); // Direct the env log stream to the user specified // method env_msg_obj.streamCB env.set_Stream(mosek.streamtype.log, new msgclass("[env]")); // Initialize the environment. env.init(); // Create a task object linked with the environment env. task = new mosek.Task(env, 0, 0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.readdata(args[0]); task.putintparam(mosek.iparam.optimizer, mosek.Val.optimizer_concurrent); task.putintparam(mosek.iparam.concurrent_num_optimizers, 2); task.optimize(); task.solutionsummary(mosek.streamtype.msg); } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); } if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } }
static void doSparsecholesky(mosek.Env env, int n, int[] anzc, int[] asubc, long[] aptrc, double[] avalc, out int[] perm, out double[] diag, out int[] lnzc, out long[] lptrc, out long lensubnval, out int[] lsubc, out double[] lvalc) { const int multiworker = 0; const int order_meth = 1; double tolsingular = 1.0e-14; env.computesparsecholesky(multiworker, order_meth, tolsingular, anzc, aptrc, asubc, avalc, out perm, out diag, out lnzc, out lptrc, out lensubnval, out lsubc, out lvalc); Console.WriteLine("L and D. Length={0}\n", lensubnval); for (int i = 0; i < n; ++i) { Console.Write("{0} perm={1} diag={2}:", i, perm[i], diag[i]); for (int s = 0; s < lnzc[i]; ++s) { Console.Write(" {0}[{1}]", lvalc[lptrc[i] + s], lsubc[lptrc[i] + s]); } Console.Write("\n"); } }
public static void Main() { mosek.Env env = new mosek.Env(); mosek.Task task = new mosek.Task(env, 0, 0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB MsgClass task_msg_obj = new MsgClass(); task.set_Stream(mosek.streamtype.log, task_msg_obj); task.appendvars(6); task.appendcons(3); task.putvarboundsliceconst(0, 6, mosek.boundkey.fr, -0.0, 0.0); // Integrality constraints task.putvartypelist(new int[] { 1, 2 }, new mosek.variabletype[] { mosek.variabletype.type_int, mosek.variabletype.type_int }); // Set up the three auxiliary linear constraints task.putaijlist(new int[] { 0, 0, 1, 2, 2 }, new int[] { 1, 3, 4, 2, 5 }, new double[] { -1, 1, 1, 1, -1 }); task.putconboundslice(0, 3, new mosek.boundkey[] { mosek.boundkey.fx, mosek.boundkey.fx, mosek.boundkey.fx }, new double[] { -3.8, 1, 0 }, new double[] { -3.8, 1, 0 }); // Objective task.putobjsense(mosek.objsense.minimize); task.putcj(0, 1); // Conic part of the problem task.appendconeseq(mosek.conetype.quad, 0, 3, 0); task.appendconeseq(mosek.conetype.pexp, 0, 3, 3); // Optimize the task task.optimize(); task.solutionsummary(mosek.streamtype.msg); double[] xx = { 0, 0 }; task.getxxslice(mosek.soltype.itg, 1, 3, xx); Console.WriteLine("x = {0}, y = {1}", xx[0], xx[1]); }
public static void Main (string[] args) { if (args.Length == 0) { Console.WriteLine ("Missing argument, syntax is:"); Console.WriteLine (" simple inputfile [ solutionfile ]"); } else { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream (mosek.streamtype.log, new msgclass ()); // We assume that a problem file was given as the first command // line argument (received in `args') task.readdata (args[0]); // Solve the problem task.optimize (); // Print a summary of the solution task.solutionsummary (mosek.streamtype.log); // If an output file was specified, write a solution if (args.Length >= 2) { // We define the output format to be OPF, and tell MOSEK to // leave out parameters and problem data from the output file. task.putintparam (mosek.iparam.write_data_format, mosek.dataformat.op); task.putintparam (mosek.iparam.opf_write_solutions, mosek.onoffkey.on); task.putintparam (mosek.iparam.opf_write_hints, mosek.onoffkey.off); task.putintparam (mosek.iparam.opf_write_parameters, mosek.onoffkey.off); task.putintparam (mosek.iparam.opf_write_problem, mosek.onoffkey.off); task.writedata(args[1]); } } } } }
public static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Missing argument, syntax is:"); Console.WriteLine(" simple inputfile [ solutionfile ]"); } else { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream(mosek.streamtype.log, new msgclass()); // We assume that a problem file was given as the first command // line argument (received in `args') task.readdata(args[0]); // Solve the problem task.optimize(); // Print a summary of the solution task.solutionsummary(mosek.streamtype.log); // If an output file was specified, write a solution if (args.Length >= 2) { // We define the output format to be OPF, and tell MOSEK to // leave out parameters and problem data from the output file. task.putintparam(mosek.iparam.write_data_format, mosek.dataformat.op); task.putintparam(mosek.iparam.opf_write_solutions, mosek.onoffkey.on); task.putintparam(mosek.iparam.opf_write_hints, mosek.onoffkey.off); task.putintparam(mosek.iparam.opf_write_parameters, mosek.onoffkey.off); task.putintparam(mosek.iparam.opf_write_problem, mosek.onoffkey.off); task.writedata(args[1]); } } } } }
public static void Main() { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env, 0, 0)) { int param; Console.WriteLine("Test MOSEK parameter get/set functions"); param = task.getintparam(mosek.iparam.log); Console.WriteLine("Default value for parameter task.ipar.log= " + param); Console.WriteLine(" setting to 1 using putintparam..."); task.putintparam(mosek.iparam.log, 1); Console.WriteLine(" setting to -1 using putintparam..."); try { task.putintparam(mosek.iparam.log, -1); } catch (mosek.Error e) { Console.WriteLine(" -1 rejected as not a valid value"); } Console.WriteLine(" setting to 2 using putparam..."); task.putparam("MSK_IPAR_LOG", "2"); Console.WriteLine(" setting to 3 using putnaintparam..."); task.putnaintparam("MSK_IPAR_LOG", 3); Console.WriteLine(" selecting the dual simplex algorithm..."); task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.dual_simplex); } } }
public static void Main () { const double inf = 0.0; /* We don't actually need any value for infinity */ const int numcon = 1; /* Number of constraints. */ const int numvar = 3; /* Number of variables. */ mosek.boundkey[] bkc = { mosek.boundkey.lo }, bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; int[][] asub = { new int[] {0}, new int[] {0}, new int[] {0} }; double[][] aval = { new double[]{1.0}, new double[]{1.0}, new double[]{1.0} }; double[] blc = { 1.0 }, buc = { inf }, c = { 0.0, -1.0, 0.0 }, blx = { 0.0, 0.0, 0.0 }, bux = { inf, inf, inf }, xx = new double[numvar]; try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream (mosek.streamtype.log, new msgclass ("")); /* Give MOSEK an estimate of the size of the input data. This is done to increase the speed of inputting data. However, it is optional. */ /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for(int j=0; j<numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j,c[j]); /* Set the bounds on variable j. blx[j] <= x_j <= bux[j] */ task.putvarbound(j,bkx[j],blx[j],bux[j]); /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for(int i=0; i<numcon; ++i) task.putconbound(i,bkc[i],blc[i],buc[i]); /* * The lower triangular part of the Q * matrix in the objective is specified. */ { int[] qsubi = { 0, 1, 2, 2 }, qsubj = { 0, 1, 0, 2 }; double[] qval = { 2.0, 0.2, -1.0, 2.0 }; /* Input the Q for the objective. */ task.putqobj(qsubi,qsubj,qval); } /* * The lower triangular part of the Q^0 * matrix in the first constraint is specified. * This corresponds to adding the term * - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2 */ { int[] qsubi = { 0, 1, 2, 2 }, qsubj = { 0, 1, 2, 0 }; double[] qval = { -2.0, -2.0, -0.2, 0.2 }; /* put Q^0 in constraint with index 0. */ task.putqconk (0, qsubi, qsubj, qval); } task.putobjsense(mosek.objsense.minimize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); task.getxx(mosek.soltype.itr, // Basic solution. xx); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:",xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } } catch (mosek.Exception e) { Console.WriteLine (e); throw; } } /* Main */
public static void Main() { const int numcon = 1; const int numvar = 3; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; mosek.boundkey bkc = mosek.boundkey.fx; double blc = 1.0; double buc = 1.0; mosek.boundkey[] bkx = { mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr }; double[] blx = { -infinity, -infinity, -infinity }; double[] bux = { +infinity, +infinity, +infinity }; double[] c = { 1.0, 1.0, 0.0 }; double[] a = { 1.0, 1.0, 1.0 }; int[] asub = { 0, 1, 2 }; int[] csub = new int[3]; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Append 'numcon' empty constraints. * The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. * The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); /* Set up the linear part of the problem */ task.putcslice(0, numvar, c); task.putarow(0, asub, a); task.putconbound(0, bkc, blc, buc); task.putvarboundslice(0, numvar, bkx, blx, bux); /* Define the exponential cone */ csub[0] = 0; csub[1] = 1; csub[2] = 2; task.appendcone(mosek.conetype.pexp, 0.0, /* For future use only, can be set to 0.0 */ csub); task.putobjsense(mosek.objsense.minimize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, // Basic solution. xx); switch (solsta) { case mosek.solsta.optimal: Console.WriteLine("Optimal primal solution\n"); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]: {1}", j, xx[j]); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } }
public static void Main() { const double infinity = 0; mosek.boundkey[] bkc = new mosek.boundkey[] { mosek.boundkey.up, mosek.boundkey.up, mosek.boundkey.up, mosek.boundkey.fx, mosek.boundkey.fx, mosek.boundkey.fx, mosek.boundkey.fx }; mosek.boundkey[] bkx = new mosek.boundkey[] { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; int[] ptrb = new int[] { 0, 2, 4, 6, 8, 10, 12 }; int[] ptre = new int[] { 2, 4, 6, 8, 10, 12, 14 }; int[] sub = new int[] { 0, 3, 0, 4, 1, 5, 1, 6, 2, 3, 2, 5, 2, 6 }; double[] blc = new double[] { -infinity, -infinity, -infinity, 800, 100, 500, 500 }; double[] buc = new double[] { 400, 1200, 1000, 800, 100, 500, 500 }; double[] c = new double[] { 1.0, 2.0, 5.0, 2.0, 1.0, 2.0, 1.0 }; double[] blx = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; double[] bux = new double[] { infinity, infinity, infinity, infinity, infinity, infinity, infinity }; double[] val = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; int numcon = 7; /* Number of constraints. */ int numvar = 7; /* Number of variables. */ try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.inputdata(numcon, numvar, c, 0.0, ptrb, ptre, sub, val, bkc, blc, buc, bkx, blx, bux); /* A maximization problem */ task.putobjsense(mosek.objsense.minimize); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine(w.Code); Console.WriteLine(w); } /* Analyze upper bound on c1 and the equality constraint on c4 */ int[] subi = new int [] { 0, 3 }; mosek.mark[] marki = new mosek.mark[] { mosek.mark.up, mosek.mark.up }; /* Analyze lower bound on the variables x12 and x31 */ int[] subj = new int [] { 1, 4 }; mosek.mark[] markj = new mosek.mark[] { mosek.mark.lo, mosek.mark.lo }; double[] leftpricei = new double[2]; double[] rightpricei = new double[2]; double[] leftrangei = new double[2]; double[] rightrangei = new double[2]; double[] leftpricej = new double[2]; double[] rightpricej = new double[2]; double[] leftrangej = new double[2]; double[] rightrangej = new double[2]; task.primalsensitivity(subi, marki, subj, markj, leftpricei, rightpricei, leftrangei, rightrangei, leftpricej, rightpricej, leftrangej, rightrangej); Console.Write("Results from sensitivity analysis on bounds:\n"); Console.Write("For constraints:\n"); for (int i = 0; i < 2; ++i) { Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n", leftpricei[i], rightpricei[i], leftrangei[i], rightrangei[i]); } Console.Write("For variables:\n"); for (int i = 0; i < 2; ++i) { Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n", leftpricej[i], rightpricej[i], leftrangej[i], rightrangej[i]); } double[] leftprice = new double[2]; double[] rightprice = new double[2]; double[] leftrange = new double[2]; double[] rightrange = new double[2]; int[] subc = new int[] { 2, 5 }; task.dualsensitivity(subc, leftprice, rightprice, leftrange, rightrange ); Console.Write("Results from sensitivity analysis on objective coefficients:"); for (int i = 0; i < 2; ++i) { Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange = {3}\n", leftprice[i], rightprice[i], leftrange[i], rightrange[i]); } } } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); throw; } }
public static double[] markowitz_with_card(int n, double[] x0, double w, double gamma, double[] mu, double[,] GT, int k) { // Total initial wealth double U = w; for (int i = 0; i < n; i++) { U += x0[i]; } int offsetx = 0; int offsets = offsetx + n; int offsett = offsets + 1; int offsetz = offsett + n; int offsety = offsetz + n; int numvar = 4 * n + 1; int offset_con_budget = 0; int offset_con_gx_t = offset_con_budget + 1; int offset_con_abs1 = offset_con_gx_t + n; int offset_con_abs2 = offset_con_abs1 + n; int offset_con_ind = offset_con_abs2 + n; int offset_con_card = offset_con_ind + n; int numcon = 4 * n + 2; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); //Set up constraint bounds, names and variable coefficients task.appendcons(numcon); for (int i = 0; i < n; ++i) { task.putconbound(offset_con_gx_t + i, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_gx_t + i, "GT[" + (i + 1) + "]"); task.putconbound(offset_con_abs1 + i, mosek.boundkey.lo, -x0[i], infinity); task.putconname(offset_con_abs1 + i, "zabs1[" + (i + 1) + "]"); task.putconbound(offset_con_abs2 + i, mosek.boundkey.lo, x0[i], infinity); task.putconname(offset_con_abs2 + i, "zabs2[" + (i + 1) + "]"); task.putconbound(offset_con_ind + i, mosek.boundkey.up, -infinity, 0.0); task.putconname(offset_con_ind + i, "ind[" + (i + 1) + "]"); } // e x = w + e x0 task.putconbound(offset_con_budget, mosek.boundkey.fx, U, U); task.putconname(offset_con_budget, "budget"); // sum(y) <= k task.putconbound(offset_con_card, mosek.boundkey.up, -infinity, k); task.putconname(offset_con_card, "cardinality"); //Variables. task.appendvars(numvar); //the objective function coefficients for (int i = 0; i < n; i++) { task.putcj(offsetx + i, mu[i]); } double[] one_m_one = { 1.0, -1.0 }; double[] one_one = { 1.0, 1.0 }; //set up variable bounds and names for (int i = 0; i < n; ++i) { task.putvarbound(offsetx + i, mosek.boundkey.lo, 0.0, infinity); task.putvarbound(offsett + i, mosek.boundkey.fr, -infinity, infinity); task.putvarbound(offsetz + i, mosek.boundkey.fr, -infinity, infinity); task.putvarbound(offsety + i, mosek.boundkey.ra, 0.0, 1.0); task.putvartype(offsety + i, mosek.variabletype.type_int); task.putvarname(offsetx + i, "x[" + (i + 1) + "]"); task.putvarname(offsett + i, "t[" + (i + 1) + "]"); task.putvarname(offsetz + i, "z[" + (i + 1) + "]"); task.putvarname(offsety + i, "y[" + (i + 1) + "]"); for (int j = i; j < n; ++j) { task.putaij(offset_con_gx_t + i, j, GT[i, j]); } task.putaij(offset_con_gx_t + i, offsett + i, -1.0); task.putaij(offset_con_budget, offsetx + i, 1.0); // z_j - x_j >= -x0_j int[] indx1 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs1 + i, indx1, one_m_one); // z_j + x_j >= +x0_j int[] indx2 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs2 + i, indx2, one_one); // z_j - U*y_j <= 0 int[] indx3 = { offsetz + i, offsety + i }; double[] va = { 1.0, -U }; task.putarow(offset_con_ind + i, indx3, va); // sum(y) task.putaij(offset_con_card, offsety + i, 1.0); } task.putvarbound(offsets, mosek.boundkey.fx, gamma, gamma); task.putvarname(offsets, "s"); // Quaadratic cone int[] csub = new int[n + 1]; csub[0] = offsets; for (int i = 0; i < n; i++) { csub[i + 1] = offsett + i; } task.appendcone(mosek.conetype.quad, 0.0, csub); task.putconename(0, "stddev"); /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); //Turn all log output off. task.putintparam(mosek.iparam.log, 0); //task.writedata("dump.opf"); /* Solve the problem */ task.optimize(); task.solutionsummary(mosek.streamtype.log); double[] xx = new double[n]; task.getxxslice(mosek.soltype.itg, offsetx, offsetx + n, xx); return(xx); } } }
public static void Main(String[] args) { const int n = 3; // Since the value infinity is never used, we define // 'infinity' for symbolic purposes only double infinity = 0.0; double gamma = 0.05; double[] mu = { 0.1073, 0.0737, 0.0627 }; double[,] GT = { { 0.1667, 0.0232, 0.0013 }, { 0.0000, 0.1033, -0.0022 }, { 0.0000, 0.0000, 0.0338 } }; double[] x0 = { 0.0, 0.0, 0.0 }; double w = 1.0; double totalBudget; int numvar = 2 * n + 1; int numcon = n + 1; // Offset of variables into the API variable. int offsetx = 0; int offsets = n; int offsett = n + 1; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream task.set_Stream(mosek.streamtype.log, new msgclass("")); // Constraints. task.appendcons(numcon); // Constraint bounds. Compute total budget. totalBudget = w; for (int i = 0; i < n; ++i) { totalBudget += x0[i]; /* Constraint bounds c^l = c^u = 0 */ task.putconbound(i + 1, mosek.boundkey.fx, 0.0, 0.0); task.putconname(i + 1, "GT[" + (i + 1) + "]"); } /* The total budget constraint c^l = c^u = totalBudget in first row of A. */ task.putconbound(0, mosek.boundkey.fx, totalBudget, totalBudget); task.putconname(0, "budget"); // Variables. task.appendvars(numvar); /* x variables. */ for (int j = 0; j < n; ++j) { /* Return of asset j in the objective */ task.putcj(offsetx + j, mu[j]); /* Coefficients in the first row of A */ task.putaij(0, offsetx + j, 1.0); /* No short-selling - x^l = 0, x^u = inf */ task.putvarbound(offsetx + j, mosek.boundkey.lo, 0.0, infinity); task.putvarname(offsetx + j, "x[" + (j + 1) + "]"); } /* s variable is a constant equal to gamma. */ task.putvarbound(offsets, mosek.boundkey.fx, gamma, gamma); task.putvarname(offsets, "s"); /* t variables (t = GT*x). */ for (int j = 0; j < n; ++j) { /* Copying the GT matrix in the appropriate block of A */ for (int k = 0; k < n; ++k) { if (GT[k, j] != 0.0) { task.putaij(1 + k, offsetx + j, GT[k, j]); } } /* Diagonal -1 entries in a block of A */ task.putaij(1 + j, offsett + j, -1.0); /* Free - no bounds */ task.putvarbound(offsett + j, mosek.boundkey.fr, -infinity, infinity); task.putvarname(offsett + j, "t[" + (j + 1) + "]"); } /* Define the cone spanned by (s, t), i.e. of dimension n + 1 */ int[] csub = new int[n + 1]; csub[0] = offsets; for (int j = 0; j < n; j++) { csub[j + 1] = offsett + j; } task.appendcone(mosek.conetype.quad, 0.0, /* For future use only, can be set to 0.0 */ csub); task.putconename(0, "stddev"); /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); task.optimize(); /* Display solution summary for quick inspection of results */ task.solutionsummary(mosek.streamtype.log); task.writedata("dump.opf"); /* Read the results */ double expret = 0.0; double[] xx = new double[n + 1]; task.getxxslice(mosek.soltype.itr, 0, offsets + 1, xx); for (int j = 0; j < n; ++j) { expret += mu[j] * xx[j + offsetx]; } Console.WriteLine("\nExpected return {0:E} for gamma {1:E}", expret, xx[offsets]); } } }
public static void Main() { const int numcon = 2; const int numvar = 2; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; double[] c = { 1.0, 1.0 }; int[] ptrb = { 0, 2 }; int[] ptre = { 2, 3 }; int[] asub = { 0, 1, 0, 1 }; double[] aval = { 1.0, 1.0, 2.0, 1.0 }; mosek.boundkey[] bkc = { mosek.boundkey.up, mosek.boundkey.up }; double[] blc = { -infinity, -infinity }; double[] buc = { 2.0, 6.0 }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo }; double[] blx = { 0.0, 0.0 }; double[] bux = { +infinity, +infinity }; double[] w1 = { 2.0, 6.0 }; double[] w2 = { 1.0, 0.0 }; try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.inputdata(numcon, numvar, c, 0.0, ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux); task.putobjsense(mosek.objsense.maximize); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine(w.Code); Console.WriteLine(w); } int[] basis = new int[numcon]; task.initbasissolve(basis); //List basis variables corresponding to columns of B int[] varsub = { 0, 1 }; for (int i = 0; i < numcon; i++) { if (basis[varsub[i]] < numcon) { Console.WriteLine("Basis variable no {0} is xc{1}", i, basis[i]); } else { Console.WriteLine("Basis variable no {0} is x{1}", i, basis[i] - numcon); } } // solve Bx = w1 // varsub contains index of non-zeros in b. // On return b contains the solution x and // varsub the index of the non-zeros in x. int nz = 2; task.solvewithbasis(0, ref nz, varsub, w1); Console.WriteLine("nz = {0}", nz); Console.WriteLine("Solution to Bx = w1:\n"); for (int i = 0; i < nz; i++) { if (basis[varsub[i]] < numcon) { Console.WriteLine("xc {0} = {1}", basis[varsub[i]], w1[varsub[i]]); } else { Console.WriteLine("x{0} = {1}", basis[varsub[i]] - numcon, w1[varsub[i]]); } } // Solve B^Tx = w2 nz = 1; varsub[0] = 0; // Only w2[0] is nonzero. task.solvewithbasis(1, ref nz, varsub, w2); Console.WriteLine("\nSolution to B^Ty = w2:\n"); for (int i = 0; i < nz; i++) { Console.WriteLine("y {0} = {1}", varsub[i], w2[varsub[i]]); } } } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); throw; } }
public static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Missing argument, syntax is:"); Console.WriteLine(" opt_server inputfile host port numpolls"); } else { string inputfile = args[0]; string host = args[1]; string port = args[2]; int numpolls = Convert.ToInt32(args[3]); using (mosek.Env env = new mosek.Env()) { string token; using (mosek.Task task = new mosek.Task(env)) { task.readdata(inputfile); token = task.asyncoptimize(host, port); } using (mosek.Task task = new mosek.Task(env)) { task.readdata(inputfile); task.set_Stream(mosek.streamtype.log, new msgclass()); Console.WriteLine("Starting polling loop..."); int i = 0; while (true) { Thread.Sleep(500); Console.WriteLine("poll {0}...\n", i); mosek.rescode trm; mosek.rescode resp; int respavailable = task.asyncpoll(host, port, token, out resp, out trm); Console.WriteLine("polling done"); if (respavailable != 0) { Console.WriteLine("solution available!"); task.asyncgetresult(host, port, token, out resp, out trm); task.solutionsummary(mosek.streamtype.log); break; } if (i == numpolls) { Console.WriteLine("max num polls reached, stopping host."); task.asyncstop(host, port, token); break; } i++; } task.solutionsummary(mosek.streamtype.log); } } } }
public static void Main () { const int numcon = 1; const int numvar = 6; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; mosek.boundkey[] bkc = { mosek.boundkey.fx }; double[] blc = { 1.0 }; double[] buc = { 1.0 }; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr}; double[] blx = { 0.0, 0.0, 0.0, -infinity, -infinity, -infinity}; double[] bux = { +infinity, +infinity, +infinity, +infinity, +infinity, +infinity}; double[] c = { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0}; double[][] aval = {new double[] {1.0}, new double[] {1.0}, new double[] {2.0}}; int[][] asub = {new int[] {0}, new int[] {0}, new int[] {0}}; int[] csub = new int[3]; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env,0,0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("")); /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for(int j=0; j<numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j,c[j]); /* Set the bounds on variable j. blx[j] <= x_j <= bux[j] */ task.putvarbound(j,bkx[j],blx[j],bux[j]); } for(int j=0; j<aval.Length; ++j) /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ /* Set the bounds on constraints. for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for(int i=0; i<numcon; ++i) task.putconbound(i,bkc[i],blc[i],buc[i]); csub[0] = 3; csub[1] = 0; csub[2] = 1; task.appendcone(mosek.conetype.quad, 0.0, /* For future use only, can be set to 0.0 */ csub); csub[0] = 4; csub[1] = 5; csub[2] = 2; task.appendcone(mosek.conetype.rquad,0.0,csub); task.putobjsense(mosek.objsense.minimize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, // Basic solution. xx); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]: {1}",j,xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } }
public static void Main () { const int numcon = 2; const int numvar = 2; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; mosek.boundkey[] bkc = { mosek.boundkey.up, mosek.boundkey.lo }; double[] blc = { -infinity, -4.0 }; double[] buc = { 250.0, infinity }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo }; double[] blx = { 0.0, 0.0 }; double[] bux = { infinity, infinity }; double[] c = {1.0, 0.64 }; int[][] asub = { new int[] {0, 1}, new int[] {0, 1} }; double[][] aval = { new double[] {50.0, 3.0},new double[] {31.0, -2.0} }; double[] xx = new double[numvar]; mosek.Env env = null; mosek.Task task = null; try { // Make mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, numcon,numvar); // Directs the log task stream to the user specified // method task_msg_obj.streamCB MsgClass task_msg_obj = new MsgClass (); task.set_Stream (mosek.streamtype.log,task_msg_obj); /* Give MOSEK an estimate of the size of the input data. This is done to increase the speed of inputting data. However, it is optional. */ /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); /* Optionally add a constant term to the objective. */ task.putcfix(0.0); for(int j=0; j<numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j,c[j]); /* Set the bounds on variable j. blx[j] <= x_j <= bux[j] */ task.putvarbound(j,bkx[j],blx[j],bux[j]); /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for(int i=0; i<numcon; ++i) task.putconbound(i,bkc[i],blc[i],buc[i]); /* Specify integer variables. */ for(int j=0; j<numvar; ++j) task.putvartype(j,mosek.variabletype.type_int); task.putobjsense(mosek.objsense.maximize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itg, out solsta); task.getxx(mosek.soltype.itg, // Integer solution. xx); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:",xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: mosek.prosta prosta; task.getprosta(mosek.soltype.itg,out prosta); switch(prosta) { case mosek.prosta.prim_infeas_or_unbounded: Console.WriteLine("Problem status Infeasible or unbounded"); break; case mosek.prosta.prim_infeas: Console.WriteLine("Problem status Infeasible."); break; case mosek.prosta.unknown: Console.WriteLine("Problem status unknown."); break; default: Console.WriteLine("Other problem status."); break; } break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } finally { if (task != null) task.Dispose (); if (env != null) env.Dispose (); } }
public static void Main(string[] args) { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env,0,0)) { task.set_Stream( mosek.streamtype.log,new msgclass()); int numvar = 6; int numcon = 5; mosek.boundkey[] bkc = new mosek.boundkey[] { mosek.boundkey.up, mosek.boundkey.fx, mosek.boundkey.fx, mosek.boundkey.fx, mosek.boundkey.fx }; double[] blc = new double[] { 0.0, 0.0, 0.0, 1.3862944, 0.0 }, buc = new double[] { 1.0, 0.0, 0.0, 1.3862944, 0.0 }; mosek.boundkey[] bkx = new mosek.boundkey[] { mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr }; double[] blx = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, bux = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; long[] aptrb = new long[] { 0, 0, 3, 6, 8 }, aptre = new long[] { 0, 3, 6, 8, 10 }; int[] asubi = new int[] { 0, 1, 2, 3, 4 }, asubj = new int[] { 0, 1, 2, 0, 1, 3, 0, 4, 1, 5 }; double[] aval = new double[] { 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 0.5, -1.0, 1.0, -1.0 }; task.appendvars(numvar); task.appendcons(numcon); task.putobjsense(mosek.objsense.minimize); task.putvarboundslice(0, numvar, bkx, blx, bux); task.putconboundslice(0, numcon, bkc, blc, buc); task.putarowlist(asubi, aptrb, aptre, asubj, aval ); mosek.scopr[] opro = new mosek.scopr[] { mosek.scopr.exp, mosek.scopr.exp }; int[] oprjo = new int[] { 2, 3 }; double[] oprfo = new double[] { 1.0, 1.0 }, oprgo = new double[] { 1.0, 1.0 }, oprho = new double[] { 0.0, 0.0 }; mosek.scopr[] oprc = new mosek.scopr[] { mosek.scopr.exp, mosek.scopr.exp }; int[] opric = new int[] { 0, 0 }, oprjc = new int[] { 4, 5 }; double[] oprfc = new double[] { 1.0, 1.0 }, oprgc = new double[] { 1.0, 1.0 }, oprhc = new double[] { 0.0, 0.0 }; task.putSCeval(opro, oprjo, oprfo, oprgo, oprho, oprc, opric, oprjc, oprfc, oprgc, oprhc); task.optimize(); task.solutionsummary(mosek.streamtype.msg); double[] res = new double[numvar]; task.getsolutionslice(mosek.soltype.itr, mosek.solitem.xx, 0, numvar, res); System.Console.Write("Solution is: [ " + res[0]); for (int i = 1; i < numvar; ++i) System.Console.Write(", " + res[i]); System.Console.Write(" ]\n"); } } }
public static void Main() { // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); int numvar = 2; int numcon = 2; double inf = 0.0; mosek.boundkey[] bkc = { mosek.boundkey.up, mosek.boundkey.lo }; double[] blc = { -inf, 0.0 }; double[] buc = { 0.0, inf }; mosek.boundkey[] bkx = { mosek.boundkey.ra, mosek.boundkey.ra }; double[] blx = { 0.5, 0.5 }; double[] bux = { 1.0, 1.0 }; task.appendvars(numvar); task.appendcons(numcon); task.putvarboundslice(0, numvar, bkx, blx, bux); task.putconboundslice(0, numcon, bkc, blc, buc); task.putaij(1, 1, -1.0); mosek.scopr[] opro = { mosek.scopr.log, mosek.scopr.exp }; int[] oprjo = { 0, 1 }; double[] oprfo = { -1.0, 1.0 }; double[] oprgo = { 1.0, 1.0 }; double[] oprho = { 0.0, 0.0 }; mosek.scopr[] oprc = new mosek.scopr[] { mosek.scopr.ent, mosek.scopr.pow }; int[] opric = { 0, 1 }; int[] oprjc = { 1, 0 }; double[] oprfc = { 1.0, 1.0 }; double[] oprgc = { .0, 0.5 }; double[] oprhc = { .0, 0.0 }; task.putSCeval(opro, oprjo, oprfo, oprgo, oprho, oprc, opric, oprjc, oprfc, oprgc, oprhc); task.putintparam(mosek.iparam.write_ignore_incompatible_items, 1); task.writeSC("scopt1.sco", "scopt1.opf"); task.optimize(); double[] res = new double[numvar]; task.getsolutionslice( mosek.soltype.itr, mosek.solitem.xx, 0, numvar, res); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]: {1}", j, res[j]); } } } }
public static void Main () { const int numcon = 3; const int numvar = 4; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; double[] c = {3.0, 1.0, 5.0, 1.0}; int[][] asub = { new int[] {0,1,2}, new int[] {0,1,2,3}, new int[] {1,3} }; double[][] aval = { new double[] {3.0,1.0,2.0}, new double[] {2.0,1.0,3.0,1.0}, new double[] {2.0,3.0} }; mosek.boundkey[] bkc = {mosek.boundkey.fx, mosek.boundkey.lo, mosek.boundkey.up}; double[] blc = {30.0, 15.0, -infinity}; double[] buc = {30.0, +infinity, 25.0}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.ra, mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0, 0.0, 0.0}; double[] bux = {+infinity, 10.0, +infinity, +infinity}; mosek.Task task = null; mosek.Env env = null; double[] xx = new double[numvar]; try { // Make mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, 0,0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("")); /* Give MOSEK an estimate of the size of the input data. This is done to increase the speed of inputting data. However, it is optional. */ /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); /* Optionally add a constant term to the objective. */ task.putcfix(0.0); for(int j=0; j<numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j,c[j]); /* Set the bounds on variable j. blx[j] <= x_j <= bux[j] */ task.putvarbound(j,bkx[j],blx[j],bux[j]); } /* Set the bounds on constraints. for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for(int i=0; i<numcon; ++i) { task.putconbound(i,bkc[i],blc[i],buc[i]); /* Input row i of A */ task.putarow(i, /* Row index.*/ asub[i], /* Column indexes of non-zeros in row i.*/ aval[i]); /* Non-zero Values of row i. */ } task.putobjsense(mosek.objsense.maximize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.bas, out solsta); task.getxx(mosek.soltype.bas, // Basic solution. xx); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:",xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } finally { if (task != null) task.Dispose (); if (env != null) env.Dispose (); } }
public static void Main () { mosek.Env env = null; mosek.Task task = null; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; int numvar = 4; int numcon = 1; int NUMINTVAR = 3; double[] c = { 7.0, 10.0, 1.0, 5.0 }; mosek.boundkey[] bkc = {mosek.boundkey.up}; double[] blc = {-infinity}; double[] buc = {2.5}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0, 0.0, 0.0}; double[] bux = {infinity, infinity, infinity, infinity}; int[] ptrb = {0, 1, 2, 3}; int[] ptre = {1, 2, 3, 4}; double[] aval = {1.0, 1.0, 1.0, 1.0}; int[] asub = {0, 0, 0, 0 }; int[] intsub = {0, 1, 2}; double[] xx = new double[numvar]; try { // Make mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, numcon,numvar); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("[task]")); task.inputdata(numcon,numvar, c, 0.0, ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux); for(int j=0 ; j<NUMINTVAR ; ++j) task.putvartype(intsub[j],mosek.variabletype.type_int); task.putobjsense(mosek.objsense.maximize); // Construct an initial feasible solution from the // values of the integer valuse specified task.putintparam(mosek.iparam.mio_construct_sol, mosek.onoffkey.on); // Set status of all variables to unknown //task.makesolutionstatusunknown(mosek.soltype.itg); // Assign values 0,2,0 to integer variables. Important to // assign a value to all integer constrained variables. double[] values={0.0, 2.0, 0.0}; task.putxxslice(mosek.soltype.itg, 0, 3, values); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine (w.Code); Console.WriteLine (w); } task.getsolutionslice(mosek.soltype.itg, /* Basic solution. */ mosek.solitem.xx, /* Which part of solution. */ 0, /* Index of first variable. */ numvar, /* Index of last variable+1 */ xx); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:{1}", j,xx[j]); } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } if (task != null) task.Dispose (); if (env != null) env.Dispose (); }
public static void Main (String[] args) { const int n = 3; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0.0; double gamma = 0.05; double[] mu = {0.1073, 0.0737, 0.0627}; double[,] GT={ {0.1667, 0.0232, 0.0013}, {0.0000, 0.1033, -0.0022}, {0.0000, 0.0000, 0.0338} }; double[] x0 = {0.0, 0.0, 0.0}; double w = 1.0; int numvar = 2*n +1; int numcon = n+1; //Offset of variables into the API variable. int offsetx = 0; int offsets = n; int offsett = n+1; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env,0,0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("")); //Constraints. task.appendcons(numcon); for( int i=1; i<=n; ++i) { w+=x0[i-1]; task.putconbound(i, mosek.boundkey.fx, infinity,infinity); task.putconname(i,"GT["+i+"]"); } task.putconbound(0, mosek.boundkey.fx,w,w); task.putconname(0,"budget"); //Variables. task.appendvars(numvar); int[] xindx={offsetx+0,offsetx+1,offsetx+2}; task.putclist(xindx,mu); for( int i=0; i<n; ++i) { for( int j=i; j<n;++j) task.putaij(i+1,offsetx+j, GT[i,j]); task.putaij(i+1, offsett+i,-1.0); task.putvarbound(offsetx+i, mosek.boundkey.lo,infinity,infinity); task.putvarname(offsetx+i,"x["+(i+1)+"]"); task.putvarname(offsett+i,"t["+(i+1)+"]"); task.putvarbound(offsett+i, mosek.boundkey.fr,infinity,infinity); } task.putvarbound(offsets, mosek.boundkey.fx,gamma,gamma); task.putvarname(offsets,"s"); double[] e={1.0,1.0,1.0}; task.putarow(0,xindx,e); //Cones. int[] csub = {offsets,offsett+0,offsett+1,offsett+2}; task.appendcone( mosek.conetype.quad, 0.0, /* For future use only, can be set to 0.0 */ csub); task.putconename(0,"stddev"); /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); task.solutionsummary(mosek.streamtype.log); task.writedata("dump.opf"); task.optimize(); double expret=0.0,stddev=0.0; double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr,xx); for(int j=0; j<n; ++j) expret += mu[j]*xx[j+offsetx]; Console.WriteLine("\neglected return {0:E} for gamma {1:E}\n",expret, xx[offsets]); } } }
public static void Main() { mosek.Env env = null; mosek.Task task = null; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; int NUMVAR = 4; int NUMCON = 1; int NUMINTVAR = 3; double[] c = { 7.0, 10.0, 1.0, 5.0 }; mosek.boundkey[] bkc = { mosek.boundkey.up }; double[] blc = { -infinity }; double[] buc = { 2.5 }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; double[] blx = { 0.0, 0.0, 0.0, 0.0 }; double[] bux = { infinity, infinity, infinity, infinity }; int[] ptrb = { 0, 1, 2, 3 }; int[] ptre = { 1, 2, 3, 4 }; double[] aval = { 1.0, 1.0, 1.0, 1.0 }; int[] asub = { 0, 0, 0, 0 }; int[] intsub = { 0, 1, 2 }; double[] xx = new double[NUMVAR]; try { // Make mosek environment. env = new mosek.Env(); // Direct the env log stream to the user specified // method env_msg_obj.streamCB env.set_Stream(mosek.streamtype.log, new msgclass("[env]")); // Initialize the environment. env.init(); // Create a task object linked with the environment env. task = new mosek.Task(env, NUMCON, NUMVAR); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.inputdata(NUMCON, NUMVAR, c, 0.0, ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux); for (int j = 0; j < NUMINTVAR; ++j) { task.putvartype(intsub[j], mosek.variabletype.type_int); } task.putobjsense(mosek.objsense.maximize); // Construct an initial feasible solution from the // values of the integer valuse specified task.putintparam(mosek.iparam.mio_construct_sol, mosek.onoffkey.on); // Set status of all variables to unknown task.makesolutionstatusunknown(mosek.soltype.itg); // Assign values 1,1,0 to integer variables task.putsolutioni( mosek.accmode.var, 0, mosek.soltype.itg, mosek.stakey.supbas, 0.0, 0.0, 0.0, 0.0); task.putsolutioni( mosek.accmode.var, 1, mosek.soltype.itg, mosek.stakey.supbas, 2.0, 0.0, 0.0, 0.0); task.putsolutioni( mosek.accmode.var, 2, mosek.soltype.itg, mosek.stakey.supbas, 0.0, 0.0, 0.0, 0.0); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine(w.Code); Console.WriteLine(w); } task.getsolutionslice(mosek.soltype.itg, /* Basic solution. */ mosek.solitem.xx, /* Which part of solution. */ 0, /* Index of first variable. */ NUMVAR, /* Index of last variable+1 */ xx); for (int j = 0; j < NUMVAR; ++j) { Console.WriteLine("x[{0}]:{1}", j, xx[j]); } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); } if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } }
public static void Main () { const double infinity = 0; mosek.boundkey[] bkc = new mosek.boundkey[]{ mosek.boundkey.up,mosek.boundkey.up, mosek.boundkey.up,mosek.boundkey.fx, mosek.boundkey.fx,mosek.boundkey.fx, mosek.boundkey.fx }; mosek.boundkey[] bkx = new mosek.boundkey[]{ mosek.boundkey.lo,mosek.boundkey.lo, mosek.boundkey.lo,mosek.boundkey.lo, mosek.boundkey.lo,mosek.boundkey.lo, mosek.boundkey.lo}; int[] ptrb= new int[]{0,2,4,6,8,10,12}; int[] ptre= new int[]{2,4,6,8,10,12,14}; int[] sub = new int[]{0,3,0,4,1,5,1,6,2,3,2,5,2,6}; double[] blc = new double[]{ -infinity,-infinity, -infinity,800,100,500,500}; double[] buc = new double[]{400,1200,1000,800,100,500,500}; double[] c = new double[]{1.0,2.0,5.0,2.0,1.0,2.0,1.0}; double[] blx = new double[]{0.0,0.0,0.0,0.0,0.0,0.0,0.0}; double[] bux = new double[]{infinity, infinity, infinity, infinity, infinity, infinity, infinity}; double[] val = new double[]{1.0,1.0,1.0,1.0,1.0,1.0,1.0, 1.0,1.0,1.0,1.0,1.0,1.0,1.0}; int numcon = 7; /* Number of constraints. */ int numvar = 7; /* Number of variables. */ try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass ("[task]")); task.inputdata(numcon,numvar, c, 0.0, ptrb, ptre, sub, val, bkc, blc, buc, bkx, blx, bux); /* A maximization problem */ task.putobjsense(mosek.objsense.minimize); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine (w.Code); Console.WriteLine (w); } /* Analyze upper bound on c1 and the equality constraint on c4 */ int[] subi = new int []{0,3}; mosek.mark[] marki = new mosek.mark[]{mosek.mark.up, mosek.mark.up}; /* Analyze lower bound on the variables x12 and x31 */ int[] subj = new int []{1,4}; mosek.mark[] markj = new mosek.mark[] {mosek.mark.lo, mosek.mark.lo}; double[] leftpricei = new double[2]; double[] rightpricei = new double[2]; double[] leftrangei = new double[2]; double[] rightrangei = new double[2]; double[] leftpricej = new double[2]; double[] rightpricej = new double[2]; double[] leftrangej = new double[2]; double[] rightrangej = new double[2]; task.primalsensitivity( subi, marki, subj, markj, leftpricei, rightpricei, leftrangei, rightrangei, leftpricej, rightpricej, leftrangej, rightrangej); Console.Write("Results from sensitivity analysis on bounds:\n"); Console.Write("For constraints:\n"); for (int i=0;i<2;++i) Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n", leftpricei[i], rightpricei[i], leftrangei[i], rightrangei[i]); Console.Write("For variables:\n"); for (int i=0;i<2;++i) Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n", leftpricej[i], rightpricej[i], leftrangej[i], rightrangej[i]); double[] leftprice = new double[2]; double[] rightprice = new double[2]; double[] leftrange = new double[2]; double[] rightrange = new double[2]; int[] subc = new int[]{2,5}; task.dualsensitivity( subc, leftprice, rightprice, leftrange, rightrange ); Console.Write("Results from sensitivity analysis on objective coefficients:"); for (int i=0;i<2;++i) Console.Write( "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange = {3}\n", leftprice[i], rightprice[i], leftrange[i], rightrange[i]); } } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } }
public static void Main() { // Since the value infinity is never used, we define // 'infinity' symbolic purposes only const double infinity = 0; const int numcon = 1; /* Number of constraints. */ const int numvar = 3; /* Number of variables. */ double[] c = { 0.0, -1.0, 0.0 }; mosek.boundkey[] bkc = { mosek.boundkey.lo }; double[] blc = { 1.0 }; double[] buc = { infinity }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; double[] blx = { 0.0, 0.0, 0.0 }; double[] bux = { +infinity, +infinity, +infinity }; int[][] asub = { new int[] { 0 }, new int[] { 0 }, new int[] { 0 } }; double[][] aval = { new double[] { 1.0 }, new double[] { 1.0 }, new double[] { 1.0 } }; mosek.Task task = null; mosek.Env env = null; double[] xx = new double[numvar]; try { // Make mosek environment. env = new mosek.Env(); // Create a task object linked with the environment env. task = new mosek.Task(env, 0, 0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Give MOSEK an estimate of the size of the input data. * This is done to increase the speed of inputting data. * However, it is optional. */ /* Append 'numcon' empty constraints. * The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. * The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for (int j = 0; j < numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j, c[j]); /* Set the bounds on variable j. * blx[j] <= x_j <= bux[j] */ task.putvarbound(j, bkx[j], blx[j], bux[j]); /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. * for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for (int i = 0; i < numcon; ++i) { task.putconbound(i, bkc[i], blc[i], buc[i]); } /* * The lower triangular part of the Q * matrix in the objective is specified. */ int[] qsubi = { 0, 1, 2, 2 }; int[] qsubj = { 0, 1, 0, 2 }; double[] qval = { 2.0, 0.2, -1.0, 2.0 }; /* Input the Q for the objective. */ task.putobjsense(mosek.objsense.minimize); task.putqobj(qsubi, qsubj, qval); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); switch (solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: task.getxx(mosek.soltype.itr, // Interior point solution. xx); Console.WriteLine("Optimal primal solution\n"); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]:", xx[j]); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine(e); throw; } finally { if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } } } /* Main */
public void computeForceDiagram(List<leaf> _listLeaf, List<node> _listNode) { //variable settings int numvar = _listNode.Count * 2; int numcon = 0; double infinity = 0; numcon += _listNode.Count; //finite element matrix foreach (var leaf in _listLeaf) { leaf.conOffset = numcon; leaf.varOffset = numvar; numcon += leaf.tuples.Length * 1; //grad(detF)dx>-detF } mosek.boundkey[] bkx = new mosek.boundkey[numvar]; double[] blx = new double[numvar]; double[] bux = new double[numvar]; for (int i = 0; i < _listNode.Count; i++) { if (_listNode[i].forceNodeType== node.type.fx) { bkx[i * 2 + 0] = mosek.boundkey.fx; blx[i * 2 + 0] = 0;// _listNode[i].X; bux[i * 2 + 0] = 0;//_listNode[i].X; bkx[i * 2 + 1] = mosek.boundkey.fx; blx[i * 2 + 1] = 0;//_listNode[i].Y; bux[i * 2 + 1] = 0;//_listNode[i].Y; } else { bkx[i * 2 + 0] = mosek.boundkey.fr; blx[i * 2 + 0] = -infinity; bux[i * 2 + 0] = infinity; bkx[i * 2 + 1] = mosek.boundkey.fr; blx[i * 2 + 1] = -infinity; bux[i * 2 + 1] = infinity; } } for (int t = 0; t < 10; t++) { init(_listLeaf); double[] x = new double[_listNode.Count * 2]; foreach (var leaf in _listLeaf) { for (int j = 0; j < leaf.nV; j++) { for (int i = 0; i < leaf.nU; i++) { int indexX = leaf.globalIndex[i + j * leaf.nU] * 2 + 0; int indexY = leaf.globalIndex[i + j * leaf.nU] * 2 + 1; var Q = leaf.forceSrf.Points.GetControlPoint(i, j); x[indexX] = Q.Location.X; x[indexY] = Q.Location.Y; } } } using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); task.appendcons(numcon); task.appendvars(numvar); for (int j = 0; j < numvar; ++j) { task.putvarbound(j, bkx[j], blx[j], bux[j]); } ShoNS.Array.SparseDoubleArray mat = new SparseDoubleArray(_listNode.Count, _listNode.Count * 2); foreach (var leaf in _listLeaf) { foreach (var tup in leaf.tuples) { for(int i = 0; i < tup.nNode; i++) { int indexI = leaf.globalIndex[tup.internalIndex[i]]; for (int j = 0; j < tup.nNode; j++) { int indexJx = leaf.globalIndex[tup.internalIndex[j]] * 2 + 0; int indexJy = leaf.globalIndex[tup.internalIndex[j]] * 2 + 1; var d0 = tup.d0; var d1 = tup.d1; var G1 = tup.refGi[0]; var G2 = tup.refGi[1]; var val0 = (d0[j] * d1[0][i] * G1[1] + d0[j] * d1[1][i] * G2[1]) * tup.refDv * tup.area; var val1 = -(d0[j] * d1[0][i] * G1[0] + d0[j] * d1[1][i] * G2[0]) * tup.refDv * tup.area; mat[indexI, indexJx] += val0; mat[indexI, indexJy] += val1; } } } } for (int i = 0; i < _listNode.Count; i++) { var val = 0d; for (int j = 0; j < _listNode.Count * 2; j++) { val += mat[i, j] * x[j]; } task.putconbound(i, mosek.boundkey.fx, -val, -val); for (int j = 0; j < _listNode.Count*2; j++) { task.putaij(i, j, mat[i, j]); } } foreach (var leaf in _listLeaf) { int offsetC = 0; foreach (var tup in leaf.tuples) { //compute current detF //x,y; referece, X,Y; forceDiagram var FXx = tup.gi[0][0] * tup.refGi[0][0] + tup.gi[1][0] * tup.refGi[1][0]; var FYy = tup.gi[0][1] * tup.refGi[0][1] + tup.gi[1][1] * tup.refGi[1][1]; var FXy = tup.gi[0][0] * tup.refGi[0][1] + tup.gi[1][0] * tup.refGi[1][1]; var FYx = tup.gi[0][1] * tup.refGi[0][0] + tup.gi[1][1] * tup.refGi[1][0]; var detF = FXx * FYy- FXy * FYx; task.putconbound(leaf.conOffset + offsetC, mosek.boundkey.lo, -detF,infinity); var G1 = tup.refGi[0]; var G2 = tup.refGi[1]; for (int i = 0; i < tup.nNode; i++) { int indexX = leaf.globalIndex[tup.internalIndex[i]] * 2 + 0; int indexY = leaf.globalIndex[tup.internalIndex[i]] * 2 + 1; double val1 = (tup.d1[0][i] * G1[0] + tup.d1[1][i] * G2[0]) * FYy; double val2 = (tup.d1[0][i] * G1[1] + tup.d1[1][i] * G2[1]) * FYx; task.putaij(leaf.conOffset + offsetC, indexX, -val2); val1 = (tup.d1[0][i] * G1[1] + tup.d1[1][i] * G2[1]) * FXx; val2 = (tup.d1[0][i] * G1[0] + tup.d1[1][i] * G2[0]) * FXy; task.putaij(leaf.conOffset + offsetC, indexY, val1- val2); } offsetC ++; } } for (int i = 0; i < _listNode.Count; i++) { task.putqobjij(i * 2 + 0, i * 2 + 0, 1); task.putqobjij(i * 2 + 1, i * 2 + 1, 1); } task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; task.getsolsta(mosek.soltype.itr, out solsta); double[] dx = new double[numvar]; task.getxx(mosek.soltype.itr, // Basic solution. dx); switch (solsta) { case mosek.solsta.optimal: System.Windows.Forms.MessageBox.Show("Optimal primal solution\n"); break; case mosek.solsta.near_optimal: System.Windows.Forms.MessageBox.Show("Near Optimal primal solution\n"); break; case mosek.solsta.dual_infeas_cer: System.Windows.Forms.MessageBox.Show("Primal or dual infeasibility.\n"); break; case mosek.solsta.prim_infeas_cer: System.Windows.Forms.MessageBox.Show("Primal or dual infeasibility.\n"); break; case mosek.solsta.near_dual_infeas_cer: System.Windows.Forms.MessageBox.Show("Primal or dual infeasibility.\n"); break; case mosek.solsta.near_prim_infeas_cer: System.Windows.Forms.MessageBox.Show("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: System.Windows.Forms.MessageBox.Show("Unknown solution status\n"); break; default: System.Windows.Forms.MessageBox.Show("Other solution status\n"); break; } foreach (var leaf in _listLeaf) { for (int j = 0; j < leaf.nV; j++) { for (int i = 0; i < leaf.nU; i++) { int indexX = leaf.globalIndex[i + j * leaf.nU] * 2 + 0; int indexY = leaf.globalIndex[i + j * leaf.nU] * 2 + 1; var Q = leaf.forceSrf.Points.GetControlPoint(i, j); var P = new Point3d(Q.Location.X + dx[indexX] * 1d, Q.Location.Y + dx[indexY] * 1d, 0); leaf.forceSrf.Points.SetControlPoint(i, j, P); } } } } } ExpirePreview(true); } }
public static void Main () { const int numcon = 2; const int numvar = 2; double[][] aval = new double[numvar][]; aval[0] = new double[] {-1.0 }; aval[1] = new double[] {1.0, 1.0}; int[][] asub = new int[numvar][]; asub[0] = new int[] {1}; asub[1] = new int[] {0,1}; int [] ptrb = {0,1}; int [] ptre = {1,3}; int[] bsub = new int[numvar]; double[] b = new double[numvar]; int[] basis = new int[numvar]; try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass ("[task]")); /* Put A matrix and factor A. Call this function only once for a given task. */ put_a( task, aval, asub, ptrb, ptre, numvar, basis ); /* now solve rhs */ b[0] = 1; b[1] = -2; bsub[0] = 0; bsub[1] = 1; int nz = 2; task.solvewithbasis(0,ref nz,bsub,b); Console.WriteLine ("\nSolution to Bx = b:\n\n"); /* Print solution and show correspondents to original variables in the problem */ for (int i=0;i<nz;++i) { if (basis[bsub[i]] < numcon) Console.WriteLine ("This should never happen\n"); else Console.WriteLine ("x{0} = {1}\n",basis[bsub[i]] - numcon , b[bsub[i]] ); } b[0] = 7; bsub[0] = 0; nz = 1; task.solvewithbasis(0,ref nz,bsub,b); Console.WriteLine ("\nSolution to Bx = b:\n\n"); /* Print solution and show correspondents to original variables in the problem */ for (int i=0;i<nz;++i) { if (basis[bsub[i]] < numcon) Console.WriteLine ("This should never happen\n"); else Console.WriteLine ("x{0} = {1}\n",basis[bsub[i]] - numcon , b[bsub[i]] ); } } } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } }
public static void Main(String[] args) { const int n = 3; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0.0; double gamma = 0.05; double[] mu = { 0.1073, 0.0737, 0.0627 }; double[,] GT = { { 0.1667, 0.0232, 0.0013 }, { 0.0000, 0.1033, -0.0022 }, { 0.0000, 0.0000, 0.0338 } }; double[] x0 = { 0.0, 0.0, 0.0 }; double w = 1.0; int numvar = 2 * n + 1; int numcon = n + 1; //Offset of variables into the API variable. int offsetx = 0; int offsets = n; int offsett = n + 1; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); //Constraints. task.appendcons(numcon); for (int i = 1; i <= n; ++i) { w += x0[i - 1]; task.putconbound(i, mosek.boundkey.fx, infinity, infinity); task.putconname(i, "GT[" + i + "]"); } task.putconbound(0, mosek.boundkey.fx, w, w); task.putconname(0, "budget"); //Variables. task.appendvars(numvar); int[] xindx = { offsetx + 0, offsetx + 1, offsetx + 2 }; task.putclist(xindx, mu); for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { task.putaij(i + 1, offsetx + j, GT[i, j]); } task.putaij(i + 1, offsett + i, -1.0); task.putvarbound(offsetx + i, mosek.boundkey.lo, infinity, infinity); task.putvarname(offsetx + i, "x[" + (i + 1) + "]"); task.putvarname(offsett + i, "t[" + (i + 1) + "]"); task.putvarbound(offsett + i, mosek.boundkey.fr, infinity, infinity); } task.putvarbound(offsets, mosek.boundkey.fx, gamma, gamma); task.putvarname(offsets, "s"); double[] e = { 1.0, 1.0, 1.0 }; task.putarow(0, xindx, e); //Cones. int[] csub = { offsets, offsett + 0, offsett + 1, offsett + 2 }; task.appendcone(mosek.conetype.quad, 0.0, /* For future use only, can be set to 0.0 */ csub); task.putconename(0, "stddev"); /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); task.solutionsummary(mosek.streamtype.log); task.writedata("dump.opf"); task.optimize(); double expret = 0.0, stddev = 0.0; double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, xx); for (int j = 0; j < n; ++j) { expret += mu[j] * xx[j + offsetx]; } Console.WriteLine("\neglected return {0:E} for gamma {1:E}\n", expret, xx[offsets]); } } }
public static void Main(string[] args) { mosek.Env env = null; mosek.Task task = null; int numvar = 0; try { env = new mosek.Env(); env.init(); task = new mosek.Task(env, 0, 0); { double[] c = new double[numvar]; task.getc(c); } { double[] upper_bound = new double[8]; double[] lower_bound = new double[8]; mosek.boundkey[] bound_key = new mosek.boundkey[8]; task.getboundslice(mosek.accmode.con, 2, 10, bound_key, lower_bound, upper_bound); } { int[] bound_index = { 1, 6, 3, 9 }; mosek.boundkey[] bound_key = { mosek.boundkey.fr, mosek.boundkey.lo, mosek.boundkey.up, mosek.boundkey.fx }; double[] lower_bound = { 0.0, -10.0, 0.0, 5.0 }; double[] upper_bound = { 0.0, 0.0, 6.0, 5.0 }; task.putboundlist(mosek.accmode.con, bound_index, bound_key, lower_bound, upper_bound); } { int[] subi = { 1, 3, 5 }; int[] subj = { 2, 3, 4 }; double[] cof = { 1.1, 4.3, 0.2 }; task.putaijlist(subi, subj, cof); } { int[] rowsub = { 0, 1, 2, 3 }; int[] ptrb = { 0, 3, 5, 7 }; int[] ptre = { 3, 5, 7, 8 }; int[] sub = { 0, 2, 3, 1, 4, 0, 3, 2 }; double[] cof = { 1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4 }; task.putaveclist(mosek.accmode.con, rowsub, ptrb, ptre, sub, cof); } } catch (mosek.ArrayLengthException e) { Console.WriteLine("Error: An array was too short"); Console.WriteLine(e.ToString()); } catch (mosek.Exception e) /* Catch both mosek.Error and mosek.Warning */ { Console.WriteLine("An error or warning was encountered"); Console.WriteLine(e.ToString()); } if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } }
public void mosek1(List<leaf> _listLeaf, List<branch> _listBranch, Dictionary<string, slice> _listSlice,Dictionary<string,range>_listRange,Dictionary<string,range>_listRangeOpen,Dictionary<string,range> _listRangeLeaf, bool obj, double allow, bool obj2) { // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; int[] csub = new int[3];// for cones int numvar = 0; int numcon = 0; foreach (var leaf in _listLeaf) { leaf.varOffset = numvar; leaf.conOffset = numcon; numvar += (leaf.nU * leaf.nV) + leaf.r * 4; //z,H11,H22,H12 mean curvature numcon += leaf.r * 4;// H11,H22,H12 if (obj) numvar += leaf.r * 3; //z,target_z, z-_z if (obj) numcon += leaf.r * 2; //z, z-target_z } foreach (var branch in _listBranch) { branch.varOffset = numvar; branch.conOffset = numcon; numvar += branch.N + branch.tuples.Count(); //z,D if (branch.branchType == branch.type.kink) { numcon += 2 * branch.N;//branch->left and right sides } else if (branch.branchType == branch.type.reinforce||branch.branchType==branch.type.open) { numcon += 1 * branch.N; //z=-ax-by-d numcon += 1 * branch.N; //branch->edge(target) } else//free { numcon += 1 * branch.N; //branch->edge(target) } numcon += branch.tuples.Count();// D(kink angle) } foreach (var slice in _listSlice.Values) { slice.varOffset = numvar; slice.conOffset = numcon; numvar += 3; //a,b,d if (slice.sliceType == slice.type.fx) { numcon++; } } if (obj) { numvar++; } //variable settings mosek.boundkey[] bkx = new mosek.boundkey[numvar]; double[] blx = new double[numvar]; double[] bux = new double[numvar]; foreach (var leaf in _listLeaf) { //z for (int i = 0; i < leaf.nU * leaf.nV; i++) { bkx[i + leaf.varOffset] = mosek.boundkey.fr; blx[i + leaf.varOffset] = -infinity; bux[i + leaf.varOffset] = infinity; } //H11,H22,H12 for (int i = 0; i < leaf.r; i++) { int n = i * 3 + (leaf.nU * leaf.nV); bkx[n + leaf.varOffset] = mosek.boundkey.fr; blx[n + leaf.varOffset] = -infinity; bux[n + leaf.varOffset] = infinity; bkx[n + 1 + leaf.varOffset] = mosek.boundkey.fr; blx[n + 1 + leaf.varOffset] = -infinity; bux[n + 1 + leaf.varOffset] = infinity; bkx[n + 2 + leaf.varOffset] = mosek.boundkey.fr; blx[n + 2 + leaf.varOffset] = -infinity; bux[n + 2 + leaf.varOffset] = infinity; } //later mean curvature will be added here // for (int i = 0; i < leaf.r; i++) { int n = i + leaf.r*3+(leaf.nU * leaf.nV); if (leaf.range.rangeType == range.type.lo) { bkx[n + leaf.varOffset] = mosek.boundkey.lo; blx[n + leaf.varOffset] = leaf.range.lb; bux[n + leaf.varOffset] = 0; } else if (leaf.range.rangeType == range.type.up) { bkx[n + leaf.varOffset] = mosek.boundkey.up; blx[n + leaf.varOffset] = 0; bux[n + leaf.varOffset] = leaf.range.ub; } else { bkx[n + leaf.varOffset] = mosek.boundkey.ra; blx[n + leaf.varOffset] = leaf.range.lb; bux[n + leaf.varOffset] = leaf.range.ub; } } //////////////// //target z if (obj) { //z for (int i = 0; i < leaf.r; i++) { bkx[i + (leaf.nU * leaf.nV) + 4 * leaf.r + leaf.varOffset] = mosek.boundkey.fr; blx[i + (leaf.nU * leaf.nV) + 4 * leaf.r + leaf.varOffset] = 0; bux[i + (leaf.nU * leaf.nV) + 4 * leaf.r + leaf.varOffset] = 0; } //target_z for (int i = 0; i < leaf.r; i++) { bkx[i + (leaf.nU * leaf.nV) + 5 * leaf.r + leaf.varOffset] = mosek.boundkey.fx; //reference multiquadric surface blx[i + (leaf.nU * leaf.nV) + 5 * leaf.r + leaf.varOffset] = globalFunc(leaf.tuples[i].x, leaf.tuples[i].y); bux[i + (leaf.nU * leaf.nV) + 5 * leaf.r + leaf.varOffset] = globalFunc(leaf.tuples[i].x, leaf.tuples[i].y); } //z-target_z for (int i = 0; i < leaf.r; i++) { bkx[i + (leaf.nU * leaf.nV) + 6 * leaf.r + leaf.varOffset] = mosek.boundkey.fr; blx[i + (leaf.nU * leaf.nV) + 6 * leaf.r + leaf.varOffset] = 0; bux[i + (leaf.nU * leaf.nV) + 6 * leaf.r + leaf.varOffset] = 0; } } } foreach(var branch in _listBranch) { if (branch.branchType == branch.type.reinforce ) { for (int i = 0; i < branch.N; i++) { bkx[i + branch.varOffset] = mosek.boundkey.fr; blx[i + branch.varOffset] = 0; bux[i + branch.varOffset] = 0; } //kink angle parameter for (int i = 0; i < branch.tuples.Count(); i++) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.lo; blx[branch.N + i + branch.varOffset] = 0.0; bux[branch.N + i + branch.varOffset] = 0; } } else if (branch.branchType == branch.type.open) { for (int i = 0; i < branch.N; i++) { bkx[i + branch.varOffset] = mosek.boundkey.fr; blx[i + branch.varOffset] = 0; bux[i + branch.varOffset] = 0; } //kink angle parameter for (int i = 0; i < branch.tuples.Count(); i++) { if (branch.range.rangeType == range.type.lo) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.lo; blx[branch.N + i + branch.varOffset] = branch.range.lb; bux[branch.N + i + branch.varOffset] = 0; } else if (branch.range.rangeType == range.type.up) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.up; blx[branch.N + i + branch.varOffset] = 0; bux[branch.N + i + branch.varOffset] = branch.range.ub; } else { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.ra; blx[branch.N + i + branch.varOffset] = branch.range.lb; bux[branch.N + i + branch.varOffset] = branch.range.ub; } //bkx[branch.N + i + branch.varOffset] = mosek.boundkey.ra; //blx[branch.N + i + branch.varOffset] = 0; //bux[branch.N + i + branch.varOffset] = 0; } } else if (branch.branchType == branch.type.kink) { for (int i = 0; i < branch.N; i++) { bkx[i + branch.varOffset] = mosek.boundkey.fr; blx[i + branch.varOffset] = -infinity; bux[i + branch.varOffset] = infinity; } //kink angle parameter for (int i = 0; i < branch.tuples.Count(); i++) { if (branch.range.rangeType == range.type.lo) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.lo; blx[branch.N + i + branch.varOffset] = branch.range.lb; bux[branch.N + i + branch.varOffset] = 0; } else if(branch.range.rangeType == range.type.up) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.up; blx[branch.N + i + branch.varOffset] = 0; bux[branch.N + i + branch.varOffset] = branch.range.ub; } else { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.ra; blx[branch.N + i + branch.varOffset] = branch.range.lb; bux[branch.N + i + branch.varOffset] = branch.range.ub; } } } else//free { for (int i = 0; i < branch.N; i++) { bkx[i + branch.varOffset] = mosek.boundkey.fr; blx[i + branch.varOffset] = -infinity; bux[i + branch.varOffset] = infinity; } //kink angle parameter for (int i = 0; i < branch.tuples.Count(); i++) { bkx[branch.N + i + branch.varOffset] = mosek.boundkey.fr; blx[branch.N + i + branch.varOffset] = -infinity; bux[branch.N + i + branch.varOffset] = infinity; } } } foreach (var slice in _listSlice.Values) { if (slice.sliceType == slice.type.fx) { //add something! bkx[slice.varOffset] = mosek.boundkey.fx; blx[slice.varOffset] = slice.a; bux[slice.varOffset] = slice.a; bkx[slice.varOffset + 1] = mosek.boundkey.fx; blx[slice.varOffset + 1] = slice.b; bux[slice.varOffset + 1] = slice.b; bkx[slice.varOffset + 2] = mosek.boundkey.fx; blx[slice.varOffset + 2] = slice.d; bux[slice.varOffset + 2] = slice.d; } else { bkx[slice.varOffset] = mosek.boundkey.fr; blx[slice.varOffset] = -infinity; bux[slice.varOffset] = infinity; bkx[slice.varOffset + 1] = mosek.boundkey.fr; blx[slice.varOffset + 1] = -infinity; bux[slice.varOffset + 1] = infinity; bkx[slice.varOffset + 2] = mosek.boundkey.fr; blx[slice.varOffset + 2] = -infinity; bux[slice.varOffset + 2] = infinity; } } if (obj) { bkx[numvar - 1] = mosek.boundkey.fx; blx[numvar - 1] = allow; bux[numvar - 1] = allow; //bkx[numvar - 1] = mosek.boundkey.fr; //blx[numvar - 1] = -infinity; //bux[numvar - 1] = infinity; } // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for (int j = 0; j < numvar; ++j) { task.putvarbound(j, bkx[j], blx[j], bux[j]); } double root2 = Math.Sqrt(2); foreach (var leaf in listLeaf) { double[] grad = new double[leaf.tuples[0].nNode]; double[] grad0 = new double[leaf.tuples[0].nNode]; double[] grad1i = new double[leaf.tuples[0].nNode]; double[] grad1j = new double[leaf.tuples[0].nNode]; //define H11,H12,H22 for (int i = 0; i < leaf.r; i++) { int N11 = i * 3; //condition number int N22 = i * 3 + 1; int N12 = i * 3 + 2; int target = i * 3 + (leaf.nU * leaf.nV) + leaf.varOffset; //variable number task.putaij(N11+leaf.conOffset, target, -1); task.putconbound(N11 + leaf.conOffset, mosek.boundkey.fx, 0, 0); task.putaij(N22 + leaf.conOffset, target + 1, -1); task.putconbound(N22 + leaf.conOffset, mosek.boundkey.fx, 0, 0); task.putaij(N12 + leaf.conOffset, target + 2, -1); task.putconbound(N12 + leaf.conOffset, mosek.boundkey.fx, 0, 0); //N11 leaf.tuples[i].d2[0, 0].CopyTo(grad, 0); leaf.tuples[i].d0.CopyTo(grad0, 0); leaf.tuples[i].d1[0].CopyTo(grad1i, 0); leaf.tuples[i].d1[0].CopyTo(grad1j, 0); for (int k = 0; k < leaf.tuples[i].nNode; k++) { for (int j = 0; j < leaf.tuples[i].elemDim; j++) { grad[k] -= leaf.tuples[i].Gammaijk[0, 0, j] * leaf.tuples[i].d1[j][k]; } double val = 0; val += grad[k]; task.putaij(N11 + leaf.conOffset, leaf.tuples[i].internalIndex[k] + leaf.varOffset, -val / root2); } //N22 leaf.tuples[i].d2[1, 1].CopyTo(grad, 0); leaf.tuples[i].d0.CopyTo(grad0, 0); leaf.tuples[i].d1[1].CopyTo(grad1i, 0); leaf.tuples[i].d1[1].CopyTo(grad1j, 0); for (int k = 0; k < leaf.tuples[i].nNode; k++) { for (int j = 0; j < leaf.tuples[i].elemDim; j++) { grad[k] -= leaf.tuples[i].Gammaijk[1, 1, j] * leaf.tuples[i].d1[j][k]; } double val = 0; val += grad[k]; task.putaij(N22 + leaf.conOffset, leaf.tuples[i].internalIndex[k] + leaf.varOffset, -val / root2); } //N12 leaf.tuples[i].d2[0, 1].CopyTo(grad, 0); leaf.tuples[i].d0.CopyTo(grad0, 0); leaf.tuples[i].d1[0].CopyTo(grad1i, 0); leaf.tuples[i].d1[1].CopyTo(grad1j, 0); for (int k = 0; k < leaf.tuples[i].nNode; k++) { for (int j = 0; j < leaf.tuples[i].elemDim; j++) { grad[k] -= leaf.tuples[i].Gammaijk[0, 1, j] * leaf.tuples[i].d1[j][k]; } double val = 0; val += grad[k]; task.putaij(N12 + leaf.conOffset, leaf.tuples[i].internalIndex[k] + leaf.varOffset, -val); } } // mean curvature will be added here // // for (int i = 0; i < leaf.r; i++) { int target = i * 3 + (leaf.nU * leaf.nV) + leaf.varOffset; //variable number int target2 = i + leaf.r*3+(leaf.nU * leaf.nV) + leaf.varOffset; //variable number int NH= leaf.r*3+i; //condition number task.putaij(NH + leaf.conOffset, target, 1); task.putaij(NH + leaf.conOffset, target + 1, 1); task.putaij(NH + leaf.conOffset, target2, -1); task.putconbound(NH+leaf.conOffset, mosek.boundkey.fx, 0, 0); } //if (leaf.leafType == leaf.type.convex) for (int i = 0; i < leaf.r; i++) { int N11 = i * 3 + (leaf.nU * leaf.nV); //variable number int N22 = i * 3 + 1 + (leaf.nU * leaf.nV); int N12 = i * 3 + 2 + (leaf.nU * leaf.nV); csub[0] = N11 + leaf.varOffset; csub[1] = N22 + leaf.varOffset; csub[2] = N12 + leaf.varOffset; task.appendcone(mosek.conetype.rquad, 0.0, // For future use only, can be set to 0.0 csub); /*if (obj2) { task.putcj(N11, leaf.tuples[i].Gij[0, 0]); task.putcj(N22, leaf.tuples[i].Gij[1, 1]); task.putcj(N12, 2*leaf.tuples[i].Gij[0, 1]); }*/ } if (obj) { double[] grad00 = new double[leaf.tuples[0].nNode]; for (int i = 0; i < leaf.r; i++) { leaf.tuples[i].d0.CopyTo(grad00, 0); for (int k = 0; k < leaf.tuples[i].nNode; k++) { task.putaij(leaf.conOffset + leaf.r * 4 + i, leaf.varOffset + leaf.tuples[i].internalIndex[k], grad00[k]); } task.putaij(leaf.conOffset + leaf.r * 4 + i, leaf.varOffset + leaf.nU*leaf.nV+leaf.r*4+i,-1); task.putconbound(leaf.conOffset + leaf.r * 4 + i, mosek.boundkey.fx, 0, 0); } for (int i = 0; i < leaf.tuples.Count(); i++) { task.putaij(leaf.conOffset + leaf.r * 5 + i, leaf.varOffset + leaf.nU * leaf.nV + leaf.r * 4 + i, 1); task.putaij(leaf.conOffset + leaf.r * 5 + i, leaf.varOffset + leaf.nU * leaf.nV + leaf.r * 5 + i, -1); task.putaij(leaf.conOffset + leaf.r * 5 + i, leaf.varOffset + leaf.nU * leaf.nV + leaf.r * 6 + i, -1); task.putconbound(leaf.conOffset + leaf.r * 5 + i, mosek.boundkey.fx, 0, 0); } } } if (obj) { List<int> dsub=new List<int>(); dsub.Add(numvar-1); foreach (var leaf in _listLeaf) { for (int i = 0; i < leaf.r; i++) { dsub.Add(leaf.varOffset + leaf.nU * leaf.nV + leaf.r * 6 + i); } } task.appendcone(mosek.conetype.quad, 0.0, dsub.ToArray()); } foreach (var branch in _listBranch) { if (branch.branchType == branch.type.kink) { tieBranchD1(branch, branch.left, task, 2, 0); tieBranchD1(branch, branch.right, task, 2, 1); defineKinkAngle2(branch,branch.left,branch.right,task, branch.conOffset + branch.N*2, branch.varOffset + branch.N); /*if (branch.obj) { for (int i = 0; i < branch.tuples.Count(); i++) { task.putcj(branch.N + i + branch.varOffset, 1); } }*/ } else if (branch.branchType == branch.type.reinforce || branch.branchType == branch.type.open) { int iA = _listSlice[branch.sliceKey].varOffset; int iB = _listSlice[branch.sliceKey].varOffset + 1; int iD = _listSlice[branch.sliceKey].varOffset + 2; //height parameter for (int i = 0; i < branch.N; i++) { double x = branch.crv.Points[i].Location.X; double y = branch.crv.Points[i].Location.Y; task.putconbound(branch.conOffset + branch.N + branch.tuples.Count() + i, mosek.boundkey.fx, 0, 0); task.putaij(branch.conOffset + branch.N + branch.tuples.Count() + i, branch.varOffset + i, 1);//z task.putaij(branch.conOffset + branch.N + branch.tuples.Count() + i, iA, x);//ax task.putaij(branch.conOffset + branch.N + branch.tuples.Count() + i, iB, y);//by task.putaij(branch.conOffset + branch.N + branch.tuples.Count() + i, iD, 1);//d } tieBranchD1(branch, branch.target, task, 1, 0); defineKinkAngleC(branch, branch.target, task, branch.conOffset + branch.N, branch.varOffset + branch.N); } else { tieBranchD1(branch, branch.target, task, 1, 0); defineKinkAngle(branch,branch.target, task, branch.conOffset + branch.N, branch.varOffset + branch.N); } } //task.putcj(numvar - 1, 1); task.putintparam(mosek.iparam.intpnt_max_iterations, 200000000);//20000000 task.putintparam(mosek.iparam.intpnt_solve_form, mosek.solveform.dual); task.putobjsense(mosek.objsense.minimize); //task.writedata("c:/out/mosek_task_dump.opf"); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, // Basic solution. xx); switch (solsta) { case mosek.solsta.optimal: System.Windows.Forms.MessageBox.Show("Optimal primal solution\n"); break; case mosek.solsta.near_optimal: System.Windows.Forms.MessageBox.Show("Near Optimal primal solution\n"); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: System.Windows.Forms.MessageBox.Show("Unknown solution status\n"); break; default: System.Windows.Forms.MessageBox.Show("Other solution status\n"); break; } //store airy potential System.Windows.Forms.MessageBox.Show(string.Format("error={0}", xx[numvar - 1])); foreach (var leaf in listLeaf) { double[] x = new double[leaf.nU * leaf.nV]; for (int j = 0; j < leaf.nV; j++) { for (int i = 0; i < leaf.nU; i++) { x[i + j * leaf.nU] = xx[i + j * leaf.nU + leaf.varOffset]; } } leaf.myMasonry.setupAiryPotentialFromList(x); } foreach (var leaf in listLeaf) { foreach (var tup in leaf.tuples) { leaf.myMasonry.elemList[tup.index].computeStressFunction(tup); } } foreach (var branch in _listBranch) { double[] x = new double[branch.N]; for (int i = 0; i < branch.N; i++) { x[i] = xx[i + branch.varOffset]; } branch.myArch.setupAiryPotentialFromList(x); } foreach (var slice in _listSlice.Values) { slice.a = xx[slice.varOffset]; slice.b = xx[slice.varOffset + 1]; slice.d = xx[slice.varOffset + 2]; double norm = Math.Sqrt(slice.a * slice.a + slice.b * slice.b + 1); var pl = new Rhino.Geometry.Plane(slice.a, slice.b, 1d, slice.d / norm); slice.update(pl); } foreach (var branch in listBranch) { foreach (var tup in branch.tuples) { if (branch.branchType == branch.type.kink) { branch.left.myMasonry.elemList[tup.left.index].computeTangent(tup.left); branch.right.myMasonry.elemList[tup.right.index].computeTangent(tup.right); } else if (branch.branchType == branch.type.fix) { branch.target.myMasonry.elemList[tup.target.index].computeTangent(tup.target); } else { branch.target.myMasonry.elemList[tup.target.index].computeTangent(tup.target); var vars = branch.slice.pl.GetPlaneEquation(); branch.target.myMasonry.elemList[tup.target.index].computeTangent(tup.target, vars[0], vars[1], vars[2], vars[3]); //valDc } } } foreach (var leaf in _listLeaf) { for (int i = 0; i < leaf.r; i++) { int target = i + leaf.r*3+(leaf.nU * leaf.nV) + leaf.varOffset; leaf.tuples[i].NH = xx[target]; } } foreach (var branch in _listBranch) { branch.airyCrv = branch.crv.Duplicate() as NurbsCurve; for (int j = 0; j < branch.N; j++) { var P = branch.crv.Points[j]; branch.airyCrv.Points.SetPoint(j, new Point3d(P.Location.X, P.Location.Y, xx[j + branch.varOffset])); } for (int i = 0; i < branch.tuples.Count(); i++) { //branch.tuples[i].z = branch.airyCrv.PointAt(branch.tuples[i].t).Z; //int D = i + branch.N; if (branch.branchType == branch.type.open) { branch.tuples[i].H[0, 0] = branch.tuples[i].target.valD - branch.tuples[i].target.valDc; } else if (branch.branchType == branch.type.reinforce) { branch.tuples[i].H[0, 0] = branch.tuples[i].target.valD - branch.tuples[i].target.valDc; } else if (branch.branchType == branch.type.fix) { branch.tuples[i].H[0, 0] = 0; } else { //afterwards, check why these two values do not match. //branch.tuples[i].H[0, 0] = branch.tuples[i].left.valD + branch.tuples[i].right.valD; branch.tuples[i].H[0, 0] = xx[branch.N + i + branch.varOffset]; } } } foreach (var range in _listRangeLeaf.Values) { double min = 10000d, max = -10000d; foreach (var leaf in range.lL) { for (int i = 0; i < leaf.tuples.Count(); i++) { if (leaf.tuples[i].NH > max) max = leaf.tuples[i].NH; if (leaf.tuples[i].NH < min) min = leaf.tuples[i].NH; } } range.lastMin = min; range.lastMax = max; range.firstPathDone = true; } foreach (var range in _listRange.Values) { double min=10000d,max=-10000d; foreach (var branch in range.lB) { for(int i=0;i<branch.tuples.Count();i++) { if (branch.tuples[i].H[0, 0] > max) max = branch.tuples[i].H[0, 0]; if (branch.tuples[i].H[0, 0] < min) min = branch.tuples[i].H[0, 0]; } } range.lastMin = min; range.lastMax = max; range.firstPathDone = true; } foreach (var range in _listRangeOpen.Values) { double min = 10000d, max = -10000d; foreach (var branch in range.lB) { for (int i = 0; i < branch.tuples.Count(); i++) { if (branch.tuples[i].H[0, 0] > max) max = branch.tuples[i].H[0, 0]; if (branch.tuples[i].H[0, 0] < min) min = branch.tuples[i].H[0, 0]; } } range.lastMin = min; range.lastMax = max; range.firstPathDone = true; } foreach (var leaf in _listLeaf) { leaf.airySrf = leaf.srf.Duplicate() as NurbsSurface; for (int j = 0; j < leaf.nV; j++) { for (int i = 0; i < leaf.nU; i++) { var P = leaf.srf.Points.GetControlPoint(i, j); leaf.airySrf.Points.SetControlPoint(i, j, new ControlPoint(P.Location.X, P.Location.Y, xx[i + j * leaf.nU + leaf.varOffset])); } } } } } }
public static void Main(string[] args) { int numcon = 2; /* Number of constraints. */ int numvar = 3; /* Number of conic quadratic variables */ int[] dimbarvar = { 3 }; /* Dimensions of semidefinite cones */ int[] lenbarvar = { 3 * (3 + 1) / 2 }; /* Number of scalar SD variables */ mosek.boundkey[] bkc = { mosek.boundkey.fx, mosek.boundkey.fx }; double[] blc = { 1.0, 0.5 }; double[] buc = { 1.0, 0.5 }; int[] barc_i = { 0, 1, 1, 2, 2 }, barc_j = { 0, 0, 1, 1, 2 }; double[] barc_v = { 2.0, 1.0, 2.0, 1.0, 2.0 }; int[][] asub = { new int[] { 0 }, new int[] { 1, 2 } }; /* column subscripts of A */ double[][] aval = { new double[] { 1.0 }, new double[] { 1.0, 1.0 } }; int[][] bara_i = { new int[] { 0, 1, 2 }, new int[] { 0, 1, 2, 1, 2, 2 } }, bara_j = { new int[] { 0, 1, 2 }, new int[] { 0, 0, 0, 1, 1, 2 } }; double[][] bara_v = { new double[] { 1.0, 1.0, 1.0 }, new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } }; int[] conesub = { 0, 1, 2 }; using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Append 'NUMCON' empty constraints. * The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'NUMVAR' variables. * The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); /* Append 'NUMBARVAR' semidefinite variables. */ task.appendbarvars(dimbarvar); /* Optionally add a constant term to the objective. */ task.putcfix(0.0); /* Set the linear term c_j in the objective.*/ task.putcj(0, 1.0); for (int j = 0; j < numvar; ++j) { task.putvarbound(j, mosek.boundkey.fr, -0.0, 0.0); } /* Set the linear term barc_j in the objective.*/ { long[] idx = new long[1]; double[] falpha = { 1.0 }; idx[0] = task.appendsparsesymmat(dimbarvar[0], barc_i, barc_j, barc_v); task.putbarcj(0, idx, falpha); } /* Set the bounds on constraints. * for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for (int i = 0; i < numcon; ++i) { task.putconbound(i, /* Index of constraint.*/ bkc[i], /* Bound key.*/ blc[i], /* Numerical value of lower bound.*/ buc[i]); /* Numerical value of upper bound.*/ } /* Input A row by row */ for (int i = 0; i < numcon; ++i) { task.putarow(i, asub[i], aval[i]); } /* Append the conic quadratic cone */ task.appendcone(mosek.conetype.quad, 0.0, conesub); /* Add the first row of barA */ { long[] idx = new long[1]; double[] falpha = { 1.0 }; task.appendsparsesymmat(dimbarvar[0], bara_i[0], bara_j[0], bara_v[0], out idx[0]); task.putbaraij(0, 0, idx, falpha); } { long[] idx = new long[1]; double[] falpha = { 1.0 }; /* Add the second row of barA */ task.appendsparsesymmat(dimbarvar[0], bara_i[1], bara_j[1], bara_v[1], out idx[0]); task.putbaraij(1, 0, idx, falpha); } /* Run optimizer */ task.optimize(); /* Print a summary containing information * about the solution for debugging purposes*/ task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; task.getsolsta(mosek.soltype.itr, out solsta); switch (solsta) { case mosek.solsta.optimal: double[] xx = new double[numvar]; double[] barx = new double[lenbarvar[0]]; task.getxx(mosek.soltype.itr, xx); task.getbarxj(mosek.soltype.itr, /* Request the interior solution. */ 0, barx); Console.WriteLine("Optimal primal solution"); for (int i = 0; i < numvar; ++i) { Console.WriteLine("x[{0}] : {1}", i, xx[i]); } for (int i = 0; i < lenbarvar[0]; ++i) { Console.WriteLine("barx[{0}]: {1}", i, barx[i]); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility certificate found."); break; case mosek.solsta.unknown: Console.WriteLine("The status of the solution could not be determined."); break; default: Console.WriteLine("Other solution status."); break; } } } }
public static void Main() { mosek.Env env = null; mosek.Task task = null; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; int numvar = 4; int numcon = 1; int NUMINTVAR = 3; double[] c = { 7.0, 10.0, 1.0, 5.0 }; mosek.boundkey[] bkc = { mosek.boundkey.up }; double[] blc = { -infinity }; double[] buc = { 2.5 }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; double[] blx = { 0.0, 0.0, 0.0, 0.0 }; double[] bux = { infinity, infinity, infinity, infinity }; int[] ptrb = { 0, 1, 2, 3 }; int[] ptre = { 1, 2, 3, 4 }; double[] aval = { 1.0, 1.0, 1.0, 1.0 }; int[] asub = { 0, 0, 0, 0 }; int[] intsub = { 0, 1, 2 }; double[] xx = new double[numvar]; try { // Make mosek environment. env = new mosek.Env(); // Create a task object linked with the environment env. task = new mosek.Task(env, numcon, numvar); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); task.inputdata(numcon, numvar, c, 0.0, ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux); for (int j = 0; j < NUMINTVAR; ++j) { task.putvartype(intsub[j], mosek.variabletype.type_int); } task.putobjsense(mosek.objsense.maximize); // Assign values to integer variables. // We only set a slice of xx double[] values = { 1.0, 1.0, 0.0 }; task.putxxslice(mosek.soltype.itg, 0, 3, values); try { task.optimize(); task.solutionsummary(mosek.streamtype.log); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine(w.Code); Console.WriteLine(w); } task.getxx(mosek.soltype.itg, xx); Console.WriteLine("Solution:"); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]:{1}", j, xx[j]); } // Was the initial solution used? int constr = task.getintinf(mosek.iinfitem.mio_construct_solution); double constrVal = task.getdouinf(mosek.dinfitem.mio_construct_solution_obj); Console.WriteLine("Initial solution utilization: " + constr); Console.WriteLine("Initial solution objective: " + constrVal); } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); throw; } if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } }
public static void Main (String[] args) { const int n = 3; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; double gamma = 0.05; double[] mu = {0.1073, 0.0737, 0.0627}; double[,] GT={ {0.1667, 0.0232, 0.0013}, {0.0000, 0.1033, -0.0022}, {0.0000, 0.0000, 0.0338} }; double[] x0 = {0.0, 0.0, 0.0}; double w = 1.0; double[] m={0.01, 0.01, 0.01}; int offsetx = 0; int offsets = offsetx + n; int offsett = offsets + 1; int offsetc = offsett + n; int offsetv = offsetc + n; int offsetz = offsetv + n; int offsetf = offsetz + n; int offsetg = offsetf + 3*n; int numvar = offsetg + 3*n; int offset_con_budget = 0; int offset_con_gx_t = offset_con_budget+1; int offset_con_abs1 = offset_con_gx_t+n; int offset_con_abs2 = offset_con_abs1+n; int offset_con_f= offset_con_abs2+n; int offset_con_g= offset_con_f + 3*n; int numcon = 1 + 3*n + 2*3*n; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); //Set up constraint bounds, names and variable coefficients task.appendcons(numcon); for (int i = 0; i < n; ++i) { w += x0[i]; task.putconbound(offset_con_gx_t + i, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_gx_t + i, "GT[" + (i + 1) + "]"); task.putconbound(offset_con_abs1 + i, mosek.boundkey.lo, -x0[i], infinity); task.putconname(offset_con_abs1 + i, "zabs1[" + (i + 1) + "]"); task.putconbound(offset_con_abs2 + i, mosek.boundkey.lo, x0[i], infinity); task.putconname(offset_con_abs2 + i, "zabs2[" + (i + 1) + "]"); for (int j = 0; j < 3; ++j) { task.putconbound(offset_con_f + 3 * i + j, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_f + 3 * i + j, "f[" + (i + 1) + "," + (j + 1) + "]"); task.putconbound(offset_con_g + 3 * i + j, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_g + 3 * i + j, "g[" + (i + 1) + "," + (j + 1) + "]"); } task.putconbound(offset_con_g + 3 * i + 1, mosek.boundkey.fx, -1.0 / 8.0, -1.0 / 8.0); } // e x = w + e x0 task.putconbound(offset_con_budget, mosek.boundkey.fx, w, w); task.putconname(offset_con_budget, "budget"); //Variables. task.appendvars(numvar); //the objective function coefficients int[] xindx = { offsetx + 0, offsetx + 1, offsetx + 2 }; task.putclist(xindx, mu); double[] one_m_one = { 1.0, -1.0 }; double[] one_one = { 1.0, 1.0 }; //set up variable bounds and names for (int i = 0; i < n; ++i) { task.putvarbound(offsetx + i, mosek.boundkey.lo, 0.0, infinity); task.putvarbound(offsett + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetc + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetz + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetv + i, mosek.boundkey.fr, infinity, infinity); for (int j = 0; j < 3; ++j) { task.putvarbound(offsetf + j + i * 3, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetg + j + i * 3, mosek.boundkey.fr, infinity, infinity); } task.putvarname(offsetx + i, "x[" + (i + 1) + "]"); task.putvarname(offsett + i, "t[" + (i + 1) + "]"); task.putvarname(offsetc + i, "c[" + (i + 1) + "]"); task.putvarname(offsetz + i, "z[" + (i + 1) + "]"); task.putvarname(offsetv + i, "v[" + (i + 1) + "]"); for (int j = 0; j < 3; ++j) { task.putvarname(offsetf + j + i * 3, "f[" + (i + 1) + "," + (j + 1) + "]"); task.putvarname(offsetg + j + i * 3, "g[" + (i + 1) + "," + (j + 1) + "]"); } for (int j = i; j < n; ++j) task.putaij(offset_con_gx_t + i, j, GT[i, j]); task.putaij(offset_con_gx_t + i, offsett + i, -1.0); task.putaij(offset_con_budget, offsetx + i, 1.0); task.putaij(offset_con_budget, offsetc + i, m[i]); // z_j - x_j >= -x0_j int[] indx1 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs1 + i, indx1, one_m_one); // z_j + x_j >= +x0_j int[] indx2 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs2 + i, indx2, one_one); int[] indxf1 = { offsetv + i, offsetf + i * 3 }; task.putarow(offset_con_f + 3 * i, indxf1, one_m_one); int[] indxf2 = { offsetc + i, offsetf + i * 3 + 1 }; task.putarow(offset_con_f + 1 + 3 * i, indxf2, one_m_one); int[] indxf3 = { offsetz + i, offsetf + i * 3 + 2 }; task.putarow(offset_con_f + 2 + 3 * i, indxf3, one_m_one); int[] indxg1 = { offsetz + i, offsetg + i * 3 }; task.putarow(offset_con_g + 3 * i, indxg1, one_m_one); task.putaij(offset_con_g + 3 * i + 1, offsetg + i * 3 + 1, -1.0); int[] indxg3 = { offsetv + i, offsetg + i * 3 + 2 }; task.putarow(offset_con_g + 3 * i + 2, indxg3, one_m_one); } task.putvarbound(offsets, mosek.boundkey.fx, gamma, gamma); task.putvarname(offsets, "s"); //Cones. int conecount = 0; int[] csub = { offsets, offsett + 0, offsett + 1, offsett + 2 }; task.appendcone(mosek.conetype.quad, 0.0, csub); task.putconename(conecount, "stddev"); ++conecount; for (int j = 0; j < n; ++j, ++conecount) { int[] coneindx = { offsetf + j * 3, offsetf + j * 3 + 1, offsetf + j * 3 + 2 }; task.appendcone(mosek.conetype.rquad, 0.0, coneindx); task.putconename(conecount, "f[" + (j + 1) + "]"); } for (int j = 0; j < n; ++j, ++conecount) { int[] coneindx = { offsetg + j * 3, offsetg + j * 3 + 1, offsetg + j * 3 + 2 }; task.appendcone(mosek.conetype.rquad, 0.0, coneindx); task.putconename(conecount, "g[" + (j + 1) + "]"); } /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); //Turn all log output off. //task.putintparam(mosek.iparam.log,0); //task.writedata("dump.opf"); /* Solve the problem */ task.optimize(); task.solutionsummary(mosek.streamtype.log); double expret = 0.0; double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, xx); for (int j = 0; j < n; ++j) expret += mu[j] * xx[j + offsetx]; Console.WriteLine("Expected return {0:E6} for gamma {1:E6}\n\n", expret, xx[offsets]); } } }
public static void Main() { const int numcon = 1; const int numvar = 6; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; mosek.boundkey[] bkc = { mosek.boundkey.fx }; double[] blc = { 1.0 }; double[] buc = { 1.0 }; mosek.boundkey[] bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr }; double[] blx = { 0.0, 0.0, 0.0, -infinity, -infinity, -infinity }; double[] bux = { +infinity, +infinity, +infinity, +infinity, +infinity, +infinity }; double[] c = { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 }; double[][] aval = { new double[] { 1.0 }, new double[] { 1.0 }, new double[] { 2.0 } }; int[][] asub = { new int[] { 0 }, new int[] { 0 }, new int[] { 0 } }; int[] csub = new int[3]; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Append 'numcon' empty constraints. * The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. * The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for (int j = 0; j < numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j, c[j]); /* Set the bounds on variable j. * blx[j] <= x_j <= bux[j] */ task.putvarbound(j, bkx[j], blx[j], bux[j]); } for (int j = 0; j < aval.Length; ++j) { /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. * for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for (int i = 0; i < numcon; ++i) { task.putconbound(i, bkc[i], blc[i], buc[i]); } csub[0] = 3; csub[1] = 0; csub[2] = 1; task.appendcone(mosek.conetype.quad, 0.0, /* For future use only, can be set to 0.0 */ csub); csub[0] = 4; csub[1] = 5; csub[2] = 2; task.appendcone(mosek.conetype.rquad, 0.0, csub); task.putobjsense(mosek.objsense.minimize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, // Basic solution. xx); switch (solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine("Optimal primal solution\n"); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]: {1}", j, xx[j]); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } }
public static void Main(String[] args) { const double infinity = 0.0; mosek.Env env = null; mosek.Task task = null; mosek.Task task_relaxprimal = null; double[] wlc = { 1.0, 1.0, 1.0, 1.0 }; double[] wuc = { 1.0, 1.0, 1.0, 1.0 }; double[] wlx = { 1.0, 1.0 }; double[] wux = { 1.0, 1.0 }; double sum_violation; try { // Make mosek environment. env = new mosek.Env(); // Direct the env log stream to the user specified // method env_msg_obj.streamCB env.set_Stream(mosek.streamtype.log, new msgclass("[env]")); // Initialize the environment. env.init(); // Create a task object linked with the environment env. task = new mosek.Task(env, 0, 0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("[task]")); /* read file from current dir */ task.readdata(args[0]); task.putintparam(mosek.iparam.feasrepair_optimize, mosek.Val.feasrepair_optimize_penalty); Console.WriteLine("Start relax primal"); task.relaxprimal(out task_relaxprimal, wlc, wuc, wlx, wux); Console.WriteLine("End relax primal"); task_relaxprimal.getprimalobj(mosek.soltype.bas, out sum_violation); Console.WriteLine("Minimized sum of violations = {0}", sum_violation); /* modified bound returned in wlc,wuc,wlx,wux */ for (int i = 0; i < 4; ++i) { if (wlc[i] == -infinity) { Console.WriteLine("lbc[{0}] = -inf, ", i); } else { Console.WriteLine("lbc[{0}] = {1}, ", i, wlc[i]); } if (wuc[i] == infinity) { Console.WriteLine("ubc[{0}] = inf\n", i); } else { Console.WriteLine("ubc[{0}] = {1}\n", i, wuc[i]); } } for (int i = 0; i < 2; ++i) { if (wlx[i] == -infinity) { Console.WriteLine("lbx[{0}] = -inf, ", i); } else { Console.WriteLine("lbx[{0}] = {1}, ", i, wlx[i]); } if (wux[i] == infinity) { Console.WriteLine("ubx[{0}] = inf\n", i); } else { Console.WriteLine("ubx[{0}] = {1}\n", i, wux[i]); } } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); } if (task != null) { task.Dispose(); } if (task_relaxprimal != null) { task_relaxprimal.Dispose(); } if (env != null) { env.Dispose(); } }
public static void Main () { // Since the value infinity is never used, we define // 'infinity' symbolic purposes only const double infinity = 0; const int numcon = 1; /* Number of constraints. */ const int numvar = 3; /* Number of variables. */ double[] c = {0.0,-1.0,0.0}; mosek.boundkey[] bkc = {mosek.boundkey.lo}; double[] blc = {1.0}; double[] buc = {infinity}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0, 0.0}; double[] bux = {+infinity, +infinity, +infinity}; int[][] asub = { new int[] {0}, new int[] {0}, new int[] {0}}; double[][] aval = { new double[] {1.0}, new double[] {1.0}, new double[] {1.0}}; mosek.Task task = null; mosek.Env env = null; double[] xx = new double[numvar]; try { // Make mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, 0,0); // Directs the log task stream to the user specified // method task_msg_obj.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("")); /* Give MOSEK an estimate of the size of the input data. This is done to increase the speed of inputting data. However, it is optional. */ /* Append 'numcon' empty constraints. The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for(int j=0; j<numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j,c[j]); /* Set the bounds on variable j. blx[j] <= x_j <= bux[j] */ task.putvarbound(j,bkx[j],blx[j],bux[j]); /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for(int i=0; i<numcon; ++i) task.putconbound(i,bkc[i],blc[i],buc[i]); /* * The lower triangular part of the Q * matrix in the objective is specified. */ int[] qsubi = {0, 1, 2, 2 }; int[] qsubj = {0, 1, 0, 2 }; double[] qval = {2.0, 0.2, -1.0, 2.0}; /* Input the Q for the objective. */ task.putobjsense(mosek.objsense.minimize); task.putqobj(qsubi,qsubj,qval); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: task.getxx(mosek.soltype.itr, // Interior point solution. xx); Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:",xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine (e); throw; } finally { if (task != null) task.Dispose (); if (env != null) env.Dispose (); } } /* Main */
public void Optimize(Story story, PositionTable <double> position, PositionTable <int> segment) { int count = -1; List <double> X = new List <double>(); int[,] index = new int[story.Characters.Count, story.FrameCount]; for (int i = 0; i < story.Characters.Count; ++i) { for (int frame = 0; frame < story.FrameCount; ++frame) { index[i, frame] = -2; } } for (int i = 0; i < story.Characters.Count; ++i) { for (int frame = 0; frame < story.FrameCount; ++frame) { if (story.SessionTable[i, frame] != -1) { if (frame > 0 && story.SessionTable[i, frame - 1] != -1 && segment[i, frame] == segment[i, frame - 1]) { index[i, frame] = count; } else { index[i, frame] = ++count; X.Add(position[i, frame]); } } } } Dictionary <Tuple <int, int>, double> Q = new Dictionary <Tuple <int, int>, double>(); for (int i = 0; i < X.Count; ++i) { Q.Add(new Tuple <int, int>(i, i), 1); } //int[,] Q = new int[X.Count, X.Count]; for (int i = 0; i < story.Characters.Count; ++i) { for (int frame = 0; frame < story.FrameCount - 1; ++frame) { int left = frame; int right = frame + 1; if (story.SessionTable[i, left] != -1 && story.SessionTable[i, right] != -1 && segment[i, left] != segment[i, right]) { if (!Q.ContainsKey(new Tuple <int, int>(index[i, left], index[i, left]))) { Q.Add(new Tuple <int, int>(index[i, left], index[i, left]), 0); } if (!Q.ContainsKey(new Tuple <int, int>(index[i, right], index[i, right]))) { Q.Add(new Tuple <int, int>(index[i, right], index[i, right]), 0); } if (!Q.ContainsKey(new Tuple <int, int>(index[i, left], index[i, right]))) { Q.Add(new Tuple <int, int>(index[i, left], index[i, right]), 0); } if (!Q.ContainsKey(new Tuple <int, int>(index[i, right], index[i, left]))) { Q.Add(new Tuple <int, int>(index[i, right], index[i, left]), 0); } Q[new Tuple <int, int>(index[i, left], index[i, left])] += 2; Q[new Tuple <int, int>(index[i, right], index[i, right])] += 2; Q[new Tuple <int, int>(index[i, left], index[i, right])] -= 2; Q[new Tuple <int, int>(index[i, right], index[i, left])] -= 2; } } } List <int> li = new List <int>(); List <int> lj = new List <int>(); List <double> lv = new List <double>(); foreach (KeyValuePair <Tuple <int, int>, double> pair in Q) { if (pair.Key.Item1 >= pair.Key.Item2 && pair.Value != 0) { li.Add(pair.Key.Item1); lj.Add(pair.Key.Item2); lv.Add((double)pair.Value); } } int[] qsubi = li.ToArray(); int[] qsubj = lj.ToArray(); double[] qval = lv.ToArray(); List <Tuple <int, int> > listInner = new List <Tuple <int, int> >(); List <Tuple <int, int> > listOuter = new List <Tuple <int, int> >(); for (int frame = 0; frame < story.FrameCount; ++frame) { List <Tuple <int, double> > l = new List <Tuple <int, double> >(); for (int i = 0; i < story.Characters.Count; ++i) { if (story.SessionTable[i, frame] != -1) { l.Add(new Tuple <int, double>(i, position[i, frame])); } } l.Sort((a, b) => a.Item2.CompareTo(b.Item2)); for (int k = 0; k < l.Count - 1; ++k) { int x = l[k].Item1; int y = l[k + 1].Item1; if (story.SessionTable[x, frame] == story.SessionTable[y, frame]) { if (!listInner.Contains(new Tuple <int, int>(index[x, frame], index[y, frame]))) { listInner.Add(new Tuple <int, int>(index[x, frame], index[y, frame])); } } else { if (!listOuter.Contains(new Tuple <int, int>(index[x, frame], index[y, frame]))) { listOuter.Add(new Tuple <int, int>(index[x, frame], index[y, frame])); } } } } Debug.Assert(!ExistingCircle(listInner, X.Count)); Debug.Assert(!ExistingCircle(listOuter, X.Count)); foreach (Tuple <int, int> tuple in listInner) { Debug.Write(tuple.Item1.ToString() + "->" + tuple.Item2.ToString() + ", "); } const double infinity = 0; int NumConstraint = listInner.Count + listOuter.Count; int NumVariable = X.Count; double[] blx = new double[NumVariable]; double[] bux = new double[NumVariable]; mosek.boundkey[] bkx = new mosek.boundkey[NumVariable]; for (int i = 0; i < NumVariable; ++i) { bkx[i] = mosek.boundkey.ra; blx[i] = -12000; bux[i] = 12000; } bkx[0] = mosek.boundkey.fx; bkx[0] = 0; bkx[0] = 0; int[][] asub = new int[NumVariable][]; double[][] aval = new double[NumVariable][]; List <int>[] asubList = new List <int> [NumVariable]; List <double>[] avalList = new List <double> [NumVariable]; for (int i = 0; i < NumVariable; ++i) { asubList[i] = new List <int>(); avalList[i] = new List <double>(); } for (int i = 0; i < listInner.Count; ++i) { Tuple <int, int> pair = listInner[i]; int x = pair.Item1; int y = pair.Item2; asubList[x].Add(i); avalList[x].Add(-1); asubList[y].Add(i); avalList[y].Add(1); } for (int i = 0; i < listOuter.Count; ++i) { int j = i + listInner.Count; Tuple <int, int> pair = listOuter[i]; int x = pair.Item1; int y = pair.Item2; asubList[x].Add(j); avalList[x].Add(-1); asubList[y].Add(j); avalList[y].Add(1); } for (int i = 0; i < NumVariable; ++i) { asub[i] = asubList[i].ToArray(); aval[i] = avalList[i].ToArray(); } mosek.boundkey[] bkc = new mosek.boundkey[NumConstraint]; double[] blc = new double[NumConstraint]; double[] buc = new double[NumConstraint]; for (int i = 0; i < listInner.Count; ++i) { bkc[i] = mosek.boundkey.fx; //blc[i] = 28; //buc[i] = 28; blc[i] = _app.Status.Config.Style.DefaultInnerGap; buc[i] = _app.Status.Config.Style.DefaultInnerGap; } for (int i = listInner.Count; i < listInner.Count + listOuter.Count; ++i) { bkc[i] = mosek.boundkey.lo; //blc[i] = 84; blc[i] = _app.Status.Config.Style.OuterGap; buc[i] = 1000; } mosek.Task task = null; mosek.Env env = null; double[] xx = new double[NumVariable]; DateTime start = DateTime.Now; try { env = new mosek.Env(); env.set_Stream(mosek.streamtype.log, new msgclass("")); env.init(); task = new mosek.Task(env, 0, 0); task.set_Stream(mosek.streamtype.log, new msgclass("")); task.putmaxnumvar(NumVariable); task.putmaxnumcon(NumConstraint); //task.putdouparam(mosek.dparam.intpnt_nl_tol_pfeas, 1.0e-1); //task.putdouparam(mosek.dparam.intpnt_tol_dfeas, 1.0e-1); //task.putdouparam(mosek.dparam.intpnt_nl_tol_rel_gap, 1.0e-1); //task.putdouparam(mosek.dparam.intpnt_co_tol_infeas, 1.0e-13); //task.putdouparam(mosek.dparam.intpnt_nl_tol_mu_red, 1.0e-13); task.append(mosek.accmode.con, NumConstraint); task.append(mosek.accmode.var, NumVariable); task.putcfix(0.0); for (int j = 0; j < NumVariable; ++j) { task.putcj(j, 0); task.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]); task.putavec(mosek.accmode.var, j, asub[j], aval[j]); } for (int i = 0; i < NumConstraint; ++i) { task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]); } task.putobjsense(mosek.objsense.minimize); task.putqobj(qsubi, qsubj, qval); task.optimize(); task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; mosek.prosta prosta; task.getsolutionstatus(mosek.soltype.itr, out prosta, out solsta); task.getsolutionslice(mosek.soltype.itr, mosek.solitem.xx, 0, NumVariable, xx); switch (solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine("Optimal primal solution\n"); //for (int j = 0; j < NumVariable; ++j) // Console.WriteLine("x[{0}]:", xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine(e.Code); Console.WriteLine(e); } finally { if (task != null) { task.Dispose(); } if (env != null) { env.Dispose(); } } Console.WriteLine("///{0}", DateTime.Now - start); for (int i = 0; i < story.Characters.Count; ++i) { for (int frame = 0; frame < story.FrameCount; ++frame) { if (story.SessionTable[i, frame] != -1) { position[i, frame] = xx[index[i, frame]]; } } } }
public static void Main(String[] args) { if (args.Length == 0) { Console.WriteLine("Missing argument, syntax is:"); Console.WriteLine(" solutionquality inputfile"); } else { using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { task.set_Stream(mosek.streamtype.log, new msgclass("")); try { // We assume that a problem file was given as the first command // line argument (received in `args') task.readdata(args[0]); // Solve the problem task.optimize(); // Console.WriteLine (a summary of the solution task.solutionsummary(mosek.streamtype.log); mosek.solsta solsta; task.getsolsta(mosek.soltype.bas, out solsta); double pobj, pviolcon, pviolvar, pviolbarvar, pviolcones, pviolitg; double dobj, dviolcon, dviolvar, dviolbarvar, dviolcones; task.getsolutioninfo(mosek.soltype.bas, out pobj, out pviolcon, out pviolvar, out pviolbarvar, out pviolcones, out pviolitg, out dobj, out dviolcon, out dviolvar, out dviolbarvar, out dviolcones); switch (solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: double abs_obj_gap = Math.Abs(dobj - pobj); double rel_obj_gap = abs_obj_gap / (1.0 + Math.Min(Math.Abs(pobj), Math.Abs(dobj))); double max_primal_viol = Math.Max(pviolcon, pviolvar); max_primal_viol = Math.Max(max_primal_viol, pviolbarvar); max_primal_viol = Math.Max(max_primal_viol, pviolcones); double max_dual_viol = Math.Max(dviolcon, dviolvar); max_dual_viol = Math.Max(max_dual_viol, dviolbarvar); max_dual_viol = Math.Max(max_dual_viol, dviolcones); // Assume the application needs the solution to be within // 1e-6 ofoptimality in an absolute sense. Another approach // would be looking at the relative objective gap Console.WriteLine("Customized solution information.\n"); Console.WriteLine(" Absolute objective gap: " + abs_obj_gap); Console.WriteLine(" Relative objective gap: " + rel_obj_gap); Console.WriteLine(" Max primal violation : " + max_primal_viol); Console.WriteLine(" Max dual violation : " + max_dual_viol); bool accepted = true; if (rel_obj_gap > 1e-6) { Console.WriteLine("Warning: The relative objective gap is LARGE."); accepted = false; } // We will accept a primal infeasibility of 1e-8 and // dual infeasibility of 1e-6. These number should chosen problem // dependent. if (max_primal_viol > 1e-8) { Console.WriteLine("Warning: Primal violation is too LARGE"); accepted = false; } if (max_dual_viol > 1e-6) { Console.WriteLine("Warning: Dual violation is too LARGE."); accepted = false; } if (accepted) { int numvar = task.getnumvar(); double[] xx = new double[numvar]; Console.WriteLine("Optimal primal solution"); task.getxx(mosek.soltype.bas, xx); for (int j = 0; j < numvar; j++) { Console.WriteLine("x[{0}]: {1}", j, xx[j]); } } else { // print etailed information about the solution task.analyzesolution(mosek.streamtype.log, mosek.soltype.bas); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility certificate found."); break; case mosek.solsta.unknown: Console.WriteLine("The status of the solution is unknown."); break; default: Console.WriteLine("Other solution status"); break; } } catch (mosek.Exception e) { Console.WriteLine("\nAn error occourred: " + e.Code); Console.WriteLine(e); //throw; } } } } }
public static void Main () { // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; const int numcon = 3; const int numvar = 3; double[] c = {1.5, 2.5, 3.0}; mosek.boundkey[] bkc = {mosek.boundkey.up, mosek.boundkey.up, mosek.boundkey.up}; double[] blc = {-infinity, -infinity, -infinity}; double[] buc = {100000, 50000, 60000}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0, 0.0}; double[] bux = {+infinity, +infinity, +infinity}; int[][] asub = new int[numvar][]; asub[0] = new int[] {0, 1, 2}; asub[1] = new int[] {0, 1, 2}; asub[2] = new int[] {0, 1, 2}; double[][] aval = new double[numvar][]; aval[0] = new double[] { 2.0, 3.0, 2.0 }; aval[1] = new double[] { 4.0, 2.0, 3.0 }; aval[2] = new double[] { 3.0, 3.0, 2.0 }; double[] xx = new double[numvar]; mosek.Task task = null; mosek.Env env = null; try { // Create mosek environment. env = new mosek.Env (); // Create a task object linked with the environment env. task = new mosek.Task (env, numcon,numvar); /* Append the constraints. */ task.appendcons(numcon); /* Append the variables. */ task.appendvars(numvar); /* Put C. */ task.putcfix(0.0); for(int j=0; j<numvar; ++j) task.putcj(j,c[j]); /* Put constraint bounds. */ for(int i=0; i<numcon; ++i) task.putbound(mosek.accmode.con,i,bkc[i],blc[i],buc[i]); /* Put variable bounds. */ for(int j=0; j<numvar; ++j) task.putbound(mosek.accmode.var,j,bkx[j],blx[j],bux[j]); /* Put A. */ if ( numcon>0 ) { for(int j=0; j<numvar; ++j) task.putacol(j, asub[j], aval[j]); } task.putobjsense(mosek.objsense.maximize); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine (w.Code); Console.WriteLine (w); } task.getxx(mosek.soltype.bas, // Request the basic solution. xx); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:{1}", j,xx[j]); /* Make a change to the A matrix */ task.putaij(0, 0, 3.0); task.optimize(); /* Get index of new variable. */ int varidx; task.getnumvar(out varidx); /* Append a new varaible x_3 to the problem */ task.appendvars(1); /* Set bounds on new varaible */ task.putbound(mosek.accmode.var, varidx, mosek.boundkey.lo, 0, +infinity); /* Change objective */ task.putcj(varidx,1.0); /* Put new values in the A matrix */ int[] acolsub = new int[] {0, 2}; double[] acolval = new double[] {4.0, 1.0}; task.putacol(varidx, /* column index */ acolsub, acolval); /* Change optimizer to simplex free and reoptimize */ task.putintparam(mosek.iparam.optimizer,mosek.optimizertype.free_simplex); task.optimize(); /* Get index of new constraint */ int conidx; task.getnumcon(out conidx); /* Append a new constraint */ task.appendcons(1); /* Set bounds on new constraint */ task.putbound( mosek.accmode.con, conidx, mosek.boundkey.up, -infinity, 30000); /* Put new values in the A matrix */ int[] arowsub = new int[] {0, 1, 2, 3 }; double[] arowval = new double[] {1.0, 2.0, 1.0, 1.0}; task.putarow(conidx, /* row index */ arowsub, arowval); task.optimize(); } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); } if (task != null) task.Dispose (); if (env != null) env.Dispose (); }
public static void Main(String[] args) { const int n = 3; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; double gamma = 0.05; double[] mu = { 0.1073, 0.0737, 0.0627 }; double[,] GT = { { 0.1667, 0.0232, 0.0013 }, { 0.0000, 0.1033, -0.0022 }, { 0.0000, 0.0000, 0.0338 } }; double[] x0 = { 0.0, 0.0, 0.0 }; double w = 1.0; double[] m = { 0.01, 0.01, 0.01 }; int offsetx = 0; int offsets = offsetx + n; int offsett = offsets + 1; int offsetc = offsett + n; int offsetv = offsetc + n; int offsetz = offsetv + n; int offsetf = offsetz + n; int offsetg = offsetf + 3 * n; int numvar = offsetg + 3 * n; int offset_con_budget = 0; int offset_con_gx_t = offset_con_budget + 1; int offset_con_abs1 = offset_con_gx_t + n; int offset_con_abs2 = offset_con_abs1 + n; int offset_con_f = offset_con_abs2 + n; int offset_con_g = offset_con_f + 3 * n; int numcon = 1 + 3 * n + 2 * 3 * n; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env, 0, 0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream(mosek.streamtype.log, new msgclass("")); //Set up constraint bounds, names and variable coefficients task.appendcons(numcon); for (int i = 0; i < n; ++i) { w += x0[i]; task.putconbound(offset_con_gx_t + i, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_gx_t + i, "GT[" + (i + 1) + "]"); task.putconbound(offset_con_abs1 + i, mosek.boundkey.lo, -x0[i], infinity); task.putconname(offset_con_abs1 + i, "zabs1[" + (i + 1) + "]"); task.putconbound(offset_con_abs2 + i, mosek.boundkey.lo, x0[i], infinity); task.putconname(offset_con_abs2 + i, "zabs2[" + (i + 1) + "]"); for (int j = 0; j < 3; ++j) { task.putconbound(offset_con_f + 3 * i + j, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_f + 3 * i + j, "f[" + (i + 1) + "," + (j + 1) + "]"); task.putconbound(offset_con_g + 3 * i + j, mosek.boundkey.fx, 0.0, 0.0); task.putconname(offset_con_g + 3 * i + j, "g[" + (i + 1) + "," + (j + 1) + "]"); } task.putconbound(offset_con_g + 3 * i + 1, mosek.boundkey.fx, -1.0 / 8.0, -1.0 / 8.0); } // e x = w + e x0 task.putconbound(offset_con_budget, mosek.boundkey.fx, w, w); task.putconname(offset_con_budget, "budget"); //Variables. task.appendvars(numvar); //the objective function coefficients int[] xindx = { offsetx + 0, offsetx + 1, offsetx + 2 }; task.putclist(xindx, mu); double[] one_m_one = { 1.0, -1.0 }; double[] one_one = { 1.0, 1.0 }; //set up variable bounds and names for (int i = 0; i < n; ++i) { task.putvarbound(offsetx + i, mosek.boundkey.lo, 0.0, infinity); task.putvarbound(offsett + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetc + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetz + i, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetv + i, mosek.boundkey.fr, infinity, infinity); for (int j = 0; j < 3; ++j) { task.putvarbound(offsetf + j + i * 3, mosek.boundkey.fr, infinity, infinity); task.putvarbound(offsetg + j + i * 3, mosek.boundkey.fr, infinity, infinity); } task.putvarname(offsetx + i, "x[" + (i + 1) + "]"); task.putvarname(offsett + i, "t[" + (i + 1) + "]"); task.putvarname(offsetc + i, "c[" + (i + 1) + "]"); task.putvarname(offsetz + i, "z[" + (i + 1) + "]"); task.putvarname(offsetv + i, "v[" + (i + 1) + "]"); for (int j = 0; j < 3; ++j) { task.putvarname(offsetf + j + i * 3, "f[" + (i + 1) + "," + (j + 1) + "]"); task.putvarname(offsetg + j + i * 3, "g[" + (i + 1) + "," + (j + 1) + "]"); } for (int j = i; j < n; ++j) { task.putaij(offset_con_gx_t + i, j, GT[i, j]); } task.putaij(offset_con_gx_t + i, offsett + i, -1.0); task.putaij(offset_con_budget, offsetx + i, 1.0); task.putaij(offset_con_budget, offsetc + i, m[i]); // z_j - x_j >= -x0_j int[] indx1 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs1 + i, indx1, one_m_one); // z_j + x_j >= +x0_j int[] indx2 = { offsetz + i, offsetx + i }; task.putarow(offset_con_abs2 + i, indx2, one_one); int[] indxf1 = { offsetv + i, offsetf + i * 3 }; task.putarow(offset_con_f + 3 * i, indxf1, one_m_one); int[] indxf2 = { offsetc + i, offsetf + i * 3 + 1 }; task.putarow(offset_con_f + 1 + 3 * i, indxf2, one_m_one); int[] indxf3 = { offsetz + i, offsetf + i * 3 + 2 }; task.putarow(offset_con_f + 2 + 3 * i, indxf3, one_m_one); int[] indxg1 = { offsetz + i, offsetg + i * 3 }; task.putarow(offset_con_g + 3 * i, indxg1, one_m_one); task.putaij(offset_con_g + 3 * i + 1, offsetg + i * 3 + 1, -1.0); int[] indxg3 = { offsetv + i, offsetg + i * 3 + 2 }; task.putarow(offset_con_g + 3 * i + 2, indxg3, one_m_one); } task.putvarbound(offsets, mosek.boundkey.fx, gamma, gamma); task.putvarname(offsets, "s"); //Cones. int conecount = 0; int[] csub = { offsets, offsett + 0, offsett + 1, offsett + 2 }; task.appendcone(mosek.conetype.quad, 0.0, csub); task.putconename(conecount, "stddev"); ++conecount; for (int j = 0; j < n; ++j, ++conecount) { int[] coneindx = { offsetf + j * 3, offsetf + j * 3 + 1, offsetf + j * 3 + 2 }; task.appendcone(mosek.conetype.rquad, 0.0, coneindx); task.putconename(conecount, "f[" + (j + 1) + "]"); } for (int j = 0; j < n; ++j, ++conecount) { int[] coneindx = { offsetg + j * 3, offsetg + j * 3 + 1, offsetg + j * 3 + 2 }; task.appendcone(mosek.conetype.rquad, 0.0, coneindx); task.putconename(conecount, "g[" + (j + 1) + "]"); } /* A maximization problem */ task.putobjsense(mosek.objsense.maximize); //Turn all log output off. //task.putintparam(mosek.iparam.log,0); //task.writedata("dump.opf"); /* Solve the problem */ task.optimize(); task.solutionsummary(mosek.streamtype.log); double expret = 0.0; double[] xx = new double[numvar]; task.getxx(mosek.soltype.itr, xx); for (int j = 0; j < n; ++j) { expret += mu[j] * xx[j + offsetx]; } Console.WriteLine("Expected return {0:E6} for gamma {1:E6}\n\n", expret, xx[offsets]); } } }
public static void Main () { const int numcon = 3; const int numvar = 4; // Since the value of infinity is ignored, we define it solely // for symbolic purposes double infinity = 0; double[] c = {3.0, 1.0, 5.0, 1.0}; int[][] asub = { new int[] {0, 1}, new int[] {0, 1, 2}, new int[] {0, 1}, new int[] {1, 2}}; double[][] aval = { new double[] {3.0, 2.0}, new double[] {1.0, 1.0, 2.0}, new double[] {2.0, 3.0}, new double[] {1.0, 3.0}}; mosek.boundkey[] bkc = {mosek.boundkey.fx, mosek.boundkey.lo, mosek.boundkey.up}; double[] blc = {30.0, 15.0, -infinity}; double[] buc = {30.0, +infinity, 25.0}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.ra, mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0, 0.0, 0.0}; double[] bux = {+infinity, 10.0, +infinity, +infinity}; // Make mosek environment. using (mosek.Env env = new mosek.Env()) { // Create a task object. using (mosek.Task task = new mosek.Task(env,0,0)) { // Directs the log task stream to the user specified // method msgclass.streamCB task.set_Stream (mosek.streamtype.log, new msgclass ("")); // Append 'numcon' empty constraints. // The constraints will initially have no bounds. task.appendcons(numcon); // Append 'numvar' variables. // The variables will initially be fixed at zero (x=0). task.appendvars(numvar); for(int j=0; j<numvar; ++j) { // Set the linear term c_j in the objective. task.putcj(j,c[j]); // Set the bounds on variable j. // blx[j] <= x_j <= bux[j] task.putvarbound(j,bkx[j],blx[j],bux[j]); // Input column j of A task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } // Set the bounds on constraints. // blc[i] <= constraint_i <= buc[i] for(int i=0; i<numcon; ++i) task.putconbound(i,bkc[i],blc[i],buc[i]); // Input the objective sense (minimize/maximize) task.putobjsense(mosek.objsense.maximize); // Solve the problem task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); // Get status information about the solution mosek.solsta solsta; task.getsolsta(mosek.soltype.bas, out solsta); switch(solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: double[] xx = new double[numvar]; task.getxx(mosek.soltype.bas, // Request the basic solution. xx); Console.WriteLine ("Optimal primal solution\n"); for(int j = 0; j < numvar; ++j) Console.WriteLine ("x[{0}]:",xx[j]); break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility certificate found.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } }
public static void Main() { const double inf = 0.0; /* We don't actually need any value for infinity */ const int numcon = 1; /* Number of constraints. */ const int numvar = 3; /* Number of variables. */ mosek.boundkey[] bkc = { mosek.boundkey.lo }, bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo }; int[][] asub = { new int[] { 0 }, new int[] { 0 }, new int[] { 0 } }; double[][] aval = { new double[] { 1.0 }, new double[] { 1.0 }, new double[] { 1.0 } }; double[] blc = { 1.0 }, buc = { inf }, c = { 0.0, -1.0, 0.0 }, blx = { 0.0, 0.0, 0.0 }, bux = { inf, inf, inf }, xx = new double[numvar]; try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream(mosek.streamtype.log, new msgclass("")); /* Give MOSEK an estimate of the size of the input data. * This is done to increase the speed of inputting data. * However, it is optional. */ /* Append 'numcon' empty constraints. * The constraints will initially have no bounds. */ task.appendcons(numcon); /* Append 'numvar' variables. * The variables will initially be fixed at zero (x=0). */ task.appendvars(numvar); for (int j = 0; j < numvar; ++j) { /* Set the linear term c_j in the objective.*/ task.putcj(j, c[j]); /* Set the bounds on variable j. * blx[j] <= x_j <= bux[j] */ task.putvarbound(j, bkx[j], blx[j], bux[j]); /* Input column j of A */ task.putacol(j, /* Variable (column) index.*/ asub[j], /* Row index of non-zeros in column j.*/ aval[j]); /* Non-zero Values of column j. */ } /* Set the bounds on constraints. * for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */ for (int i = 0; i < numcon; ++i) { task.putconbound(i, bkc[i], blc[i], buc[i]); } /* * The lower triangular part of the Q * matrix in the objective is specified. */ { int[] qsubi = { 0, 1, 2, 2 }, qsubj = { 0, 1, 0, 2 }; double[] qval = { 2.0, 0.2, -1.0, 2.0 }; /* Input the Q for the objective. */ task.putqobj(qsubi, qsubj, qval); } /* * The lower triangular part of the Q^0 * matrix in the first constraint is specified. * This corresponds to adding the term * - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2 */ { int[] qsubi = { 0, 1, 2, 2 }, qsubj = { 0, 1, 2, 0 }; double[] qval = { -2.0, -2.0, -0.2, 0.2 }; /* put Q^0 in constraint with index 0. */ task.putqconk(0, qsubi, qsubj, qval); } task.putobjsense(mosek.objsense.minimize); task.optimize(); // Print a summary containing information // about the solution for debugging purposes task.solutionsummary(mosek.streamtype.msg); mosek.solsta solsta; /* Get status information about the solution */ task.getsolsta(mosek.soltype.itr, out solsta); task.getxx(mosek.soltype.itr, // Basic solution. xx); switch (solsta) { case mosek.solsta.optimal: case mosek.solsta.near_optimal: Console.WriteLine("Optimal primal solution\n"); for (int j = 0; j < numvar; ++j) { Console.WriteLine("x[{0}]:", xx[j]); } break; case mosek.solsta.dual_infeas_cer: case mosek.solsta.prim_infeas_cer: case mosek.solsta.near_dual_infeas_cer: case mosek.solsta.near_prim_infeas_cer: Console.WriteLine("Primal or dual infeasibility.\n"); break; case mosek.solsta.unknown: Console.WriteLine("Unknown solution status.\n"); break; default: Console.WriteLine("Other solution status"); break; } } } } catch (mosek.Exception e) { Console.WriteLine(e); throw; } } /* Main */
public static void Main () { const int numcon = 2; const int numvar = 2; // Since the value infinity is never used, we define // 'infinity' symbolic purposes only double infinity = 0; double[] c = {1.0, 1.0}; int[] ptrb = {0, 2}; int[] ptre = {2, 3}; int[] asub = {0, 1, 0, 1}; double[] aval = {1.0, 1.0, 2.0, 1.0}; mosek.boundkey[] bkc = {mosek.boundkey.up, mosek.boundkey.up}; double[] blc = {-infinity, -infinity}; double[] buc = {2.0, 6.0}; mosek.boundkey[] bkx = {mosek.boundkey.lo, mosek.boundkey.lo}; double[] blx = {0.0, 0.0}; double[] bux = {+infinity, +infinity}; double[] w1 = {2.0, 6.0}; double[] w2 = {1.0, 0.0}; try { using (mosek.Env env = new mosek.Env()) { using (mosek.Task task = new mosek.Task(env)) { task.set_Stream (mosek.streamtype.log, new msgclass ("[task]")); task.inputdata(numcon,numvar, c, 0.0, ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux); task.putobjsense(mosek.objsense.maximize); try { task.optimize(); } catch (mosek.Warning w) { Console.WriteLine("Mosek warning:"); Console.WriteLine (w.Code); Console.WriteLine (w); } int[] basis = new int[numcon]; task.initbasissolve(basis); //List basis variables corresponding to columns of B int[] varsub = {0,1}; for (int i = 0; i < numcon; i++) { if (basis[varsub[i]] < numcon) Console.WriteLine ("Basis variable no {0} is xc{1}", i, basis[i]); else Console.WriteLine ("Basis variable no {0} is x{1}", i, basis[i] - numcon); } // solve Bx = w1 // varsub contains index of non-zeros in b. // On return b contains the solution x and // varsub the index of the non-zeros in x. int nz = 2; task.solvewithbasis(0, ref nz, varsub, w1); Console.WriteLine ("nz = {0}", nz); Console.WriteLine ("Solution to Bx = w1:\n"); for (int i = 0; i < nz; i++) { if (basis[varsub[i]] < numcon) Console.WriteLine ("xc {0} = {1}", basis[varsub[i]], w1[varsub[i]] ); else Console.WriteLine ("x{0} = {1}", basis[varsub[i]] - numcon, w1[varsub[i]]); } // Solve B^Tx = w2 nz = 1; varsub[0] = 0; // Only w2[0] is nonzero. task.solvewithbasis(1, ref nz, varsub, w2); Console.WriteLine ("\nSolution to B^Ty = w2:\n"); for (int i = 0; i < nz; i++) { Console.WriteLine ("y {0} = {1}", varsub[i], w2[varsub[i]]); } } } } catch (mosek.Exception e) { Console.WriteLine (e.Code); Console.WriteLine (e); throw; } }