Example #1
0
 public static void Main(string[] args)
 {
   Matrix recipe = new DenseMatrix(recipe_data);
   using (Model M = new Model("Recipe"))
   {
     // "production" defines the amount of each product to bake.
     Variable production = M.Variable("production", 
                                      new StringSet(productnames), 
                                      Domain.GreaterThan(0.0));
     // The objective is to maximize the total revenue.
     M.Objective("revenue",
                 ObjectiveSense.Maximize, 
                 Expr.Dot(revenue, production));
     
     // The prodoction is constrained by stock:
     M.Constraint(Expr.Mul(recipe, production), Domain.LessThan(stock));
     M.SetLogHandler(Console.Out);
   
     // We solve and fetch the solution:
     M.Solve();
     double[] res = production.Level();
     Console.WriteLine("Solution:");
     for (int i = 0; i < res.Length; ++i)
     {
       Console.WriteLine(" Number of {0} : {1}", productnames[i], res[i]);
     }
     Console.WriteLine(" Revenue : ${0}", 
                       res[0] * revenue[0] + res[1] * revenue[1]);
   }
 }
Example #2
0
        public static double[][] lownerjohn_outer(double[][] x)
        {
          using( Model M = new Model("lownerjohn_outer") )
          {
            // Direct log output to the terminal for debugging. 
            M.SetLogHandler(Console.Out);  
            int m = x.Length;
            int n = x[0].Length;

            // Setup variables
            Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0));
            Variable P = M.Variable("P", new NDSet(n,n), Domain.Unbounded());
            Variable c = M.Variable("c", n, Domain.Unbounded());

            // (1, P(*xi+c)) \in Q 
            for (int i = 0; i < m; ++i)
              M.Constraint( "qc" + i, Expr.Vstack(Expr.Ones(1), Expr.Sub(Expr.Mul(P,x[i]), c)), Domain.InQCone() );

            // t <= det(P)^{1/n}
            det_rootn(M, P, t);
                             
            // Objective: Maximize t
            M.Objective(ObjectiveSense.Maximize, t);
            M.Solve();
          
            double[] Plvl = P.Level();
            double[] clvl = c.Level();

            double[][] Pc = new double[n+1][];
            for (int i = 0; i < n; ++i)
            {
              Pc[i] = new double[n];
              System.Array.Copy(Plvl,i*n, Pc[i],0, n);
            }
            Pc[n] = clvl;

            return Pc;
          } 
        }
Example #3
0
        public static double[][] lownerjohn_inner(double[][] A, double[] b)
        {
          using( Model M = new Model("lownerjohn_inner"))
          {
            // Direct log output to the terminal for debugging. 
            M.SetLogHandler(Console.Out);  
            int m = A.Length;
            int n = A[0].Length; 

            // Setup variables
            Variable t = M.Variable("t", 1, Domain.GreaterThan(0.0));   
            Variable C = M.Variable("C", new NDSet(n,n), Domain.Unbounded());
            Variable d = M.Variable("d", n, Domain.Unbounded());        
          
            // (bi - ai^T*d, C*ai) \in Q 
            for (int i = 0; i < m; ++i)
              M.Constraint( "qc" + i, Expr.Vstack(Expr.Sub(b[i],Expr.Dot(A[i],d)), Expr.Mul(C,A[i])), Domain.InQCone() );

            // t <= det(C)^{1/n}
            det_rootn(M, C, t);
                                 
            // Objective: Maximize t
            M.Objective(ObjectiveSense.Maximize, t);
            M.Solve();
          
            double[] Clvl = C.Level();
            double[] dlvl = d.Level();

            double[][] Cres_d = new double[n+1][];
            for (int i = 0; i < n; ++i)
            {
              Cres_d[i] = new double[n];
              System.Array.Copy(Clvl,i*n,Cres_d[i],0, n);
            }
            Cres_d[n] = dlvl;
            return Cres_d;
          }
        }