static void DoStuff(AMPLModel m) { // Get the number of variables int nvars = m.getNumVars(); GCB gcb = new GCB(); m.setCallback(gcb); double obj = m.optimize(); var sol = m.getSolutionVector().Where(a => a != 0).ToList(); Console.WriteLine($"Status: {m.getStatus().ToString()}"); Console.WriteLine($"Solution of {m.GetType().Name}={m.getObj()}, nnz={sol.Count()}"); var map = m.getVarMapInverse(); Console.WriteLine($"First 10 non zeroes:"); for (int i = 0; i < Math.Min(sol.Count, 10); i++) { Console.WriteLine($"{map[i]}: {sol[i]}"); } }
private void DoStuff(AMPLModel m, string name) { m.setAMPLsParameter(SolverParams.SolverParameters.DBL_MIPGap, 0.2); m.optimize(); // Print model solution result string s = Enum.GetName(typeof(Status.SolStatus), m.getStatus()); double obj = m.getObj(); Console.WriteLine($"\n{s} solution with {name}={obj}"); // Get the solution vector and count the non-zeros int nvars = m.getNumVars(); double[] solution = new double[nvars]; m.getSolution(0, nvars, solution); int nnz = solution.Where(x => x != 0).Count(); Console.WriteLine($"Number of non zeros = {nvars}"); string solFileName = $"{m.getFileName()}-{name}.sol"; Console.WriteLine($"Writing solution file to {solFileName}"); m.writeSol(solFileName); }