public static void Main(string[] args) { int nc = 10; int m = 50; int n = 3; int seed = 1; double variance = 1.0; double mean = 1.0; Random gen = new Random(seed); int nump = gen.Next(m); double [] y = new double[m]; for (int i = 0; i < nump; i++) { y[i] = 1.0; } for (int i = nump; i < m; i++) { y[i] = -1.0; } double [,] X = new double[m, n]; for (int i = 0; i < nump; i++) { for (int j = 0; j < n; j++) { double xx = Math.Sqrt(-2.0 * Math.Log(gen.NextDouble())); double yy = Math.Sin(2.0 * Math.PI * gen.NextDouble()); X[i, j] = (xx * yy) * variance + mean; } } Model M = new Model("Primal SVM"); try { Console.WriteLine("Number of data : {0}\n", m); Console.WriteLine("Number of features: {0}\n", n); Variable w = M.Variable(n, Domain.Unbounded()); Variable t = M.Variable(1, Domain.Unbounded()); Variable b = M.Variable(1, Domain.Unbounded()); Variable xi = M.Variable(m, Domain.GreaterThan(0.0)); M.Constraint(Expr.Add(Expr.MulElm(y, Expr.Sub(Expr.Mul(X, w), Var.Repeat(b, m))), xi), Domain.GreaterThan(1.0)); M.Constraint(Expr.Vstack(1.0, t, w), Domain.InRotatedQCone()); M.AcceptedSolutionStatus(AccSolutionStatus.NearOptimal); Console.WriteLine(" c | b | w"); for (int i = 0; i < nc; i++) { double c = 500.0 * i; //TAG:begin-svm-obj-fun M.Objective(ObjectiveSense.Minimize, Expr.Add(t, Expr.Mul(c, Expr.Sum(xi)))); //TAG:end-svm-obj-fun M.Solve(); Console.WriteLine("{0} | {1} |", c, b.Level()[0]); for (int j = 0; j < n; j++) { Console.Write(" {0}", w.Level()[j]); } Console.WriteLine("\n"); } } finally { M.Dispose(); } }
public static void Main(String[] argv) { Model M = new Model(); // (Optional) set a log stream // M.SetLogHandler(Console.Out); // (Optional) uncomment to see what happens when solution status is unknown // M.SetSolverParam("intpntMaxIterations", 1); // In this example we set up a small conic problem SetupExample(M); // Optimize try { M.Solve(); // We expect solution status OPTIMAL (this is also default) M.AcceptedSolutionStatus(AccSolutionStatus.Optimal); Variable x = M.GetVariable("x"); long xsize = x.GetSize(); double[] xVal = x.Level(); Console.Write("Optimal value of x = "); for (int i = 0; i < xsize; ++i) { Console.Write(xVal[i] + " "); } Console.WriteLine("\nOptimal primal objective: " + M.PrimalObjValue()); // .. continue analyzing the solution } catch (OptimizeError e) { Console.WriteLine("Optimization failed. Error: " + e.ToString()); } catch (SolutionError) { // The solution with at least the expected status was not available. // We try to diagnoze why. Console.WriteLine("Requested solution was not available."); ProblemStatus prosta = M.GetProblemStatus(); switch (prosta) { case ProblemStatus.DualInfeasible: Console.WriteLine("Dual infeasibility certificate found."); break; case ProblemStatus.PrimalInfeasible: Console.WriteLine("Primal infeasibility certificate found."); break; case ProblemStatus.Unknown: // The solutions status is unknown. The termination code // indicates why the optimizer terminated prematurely. Console.WriteLine("The solution status is unknown."); StringBuilder symname = new StringBuilder(); StringBuilder desc = new StringBuilder(); Env.getcodedesc((rescode)M.GetSolverIntInfo("optimizeResponse"), symname, desc); Console.WriteLine(" Termination code: {0} {1}", symname, desc); break; default: Console.WriteLine("Another unexpected problem status: " + prosta); break; } } catch (Exception e) { Console.WriteLine("Unexpected error: " + e.ToString()); } M.Dispose(); }