示例#1
0
        static void Main(string[] args)
        {
            GurobiDrv g = new GurobiDrv();

            try
            {
                var m = g.loadModel(@"D:\Development\AMPL\solvers-public\test\models\tsp.nl");

                int nvars = m.getNumVars();
                //CB cb = new CB();
                //m.setCallback(cb);

                GCB gcb = new GCB();
                m.setCallback(gcb);
                double obj = m.optimize();
                Console.WriteLine("Solution with CPLEX={0}", m.getObj());
                double[] sol = new double[nvars];
                m.getSolution(0, nvars, sol);
                m.writeSol();
                // var map = m.getVarMap();
                // foreach (var item in map)
                // {
                //    if (sol[item.Value] != 0)
                //        Console.WriteLine("{0}: {1}", item.Key, sol[item.Value]);
                // }
            }
            catch (Exception ex)
            {
                Console.WriteLine("exception caught!\r\n" + ex.Message);
            }
        }
示例#2
0
文件: Program.cs 项目: ampl/ampls-api
        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]}");
            }
        }