当前位置: 首页>>代码示例>>C#>>正文


C# Model.Variable方法代码示例

本文整理汇总了C#中Model.Variable方法的典型用法代码示例。如果您正苦于以下问题:C# Model.Variable方法的具体用法?C# Model.Variable怎么用?C# Model.Variable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Model的用法示例。


在下文中一共展示了Model.Variable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main(String[] args)
        {
          using (Model M = new Model("alan"))
          {
            Variable x = M.Variable("x",        numsec, Domain.GreaterThan(0.0));
            Variable t = M.Variable("variance", Domain.GreaterThan(0.0));
            M.Objective("minvar", ObjectiveSense.Minimize, t.AsExpr());
            
            // sum securities to 1.0
            M.Constraint("wealth",  Expr.Sum(x), Domain.EqualsTo(1.0));
            // define target expected return 
            M.Constraint("dmean", Expr.Dot(mean, x), Domain.GreaterThan(target));
            
            M.Constraint(Expr.Vstack(Expr.ConstTerm(1,0.5),
                                     t.AsExpr(), 
                                     Expr.Mul(U,x)), 
                         Domain.InRotatedQCone());
            Console.WriteLine("Solve...");
            M.Solve();
            Console.WriteLine("... Solved.");
            double[] solx = x.Level();

            Console.WriteLine("Primal solution = {0}", solx[0]);
            for (int i = 1; i < numsec; ++i)
              Console.Write(", {0}", solx[i]);
            Console.WriteLine("");
          }
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:28,代码来源:alan.cs

示例2: Main

 public static void Main(string[] args)
 {
   using (Model M  = new Model("sdo1"))
   {
     // Setting up the variables
     Variable X  = M.Variable("X", Domain.InPSDCone(3));
     Variable x  = M.Variable("x", Domain.InQCone(3));
     
     DenseMatrix C  = new DenseMatrix ( new double[][] { new double[] {2,1,0}, new double[] {1,2,1}, new double[] {0,1,2}} );
     DenseMatrix A1 = new DenseMatrix ( new double[][] { new double[] {1,0,0}, new double[] {0,1,0}, new double[] {0,0,1}} );
     DenseMatrix A2 = new DenseMatrix ( new double[][] { new double[] {1,1,1}, new double[] {1,1,1}, new double[] {1,1,1}} );
     
     // Objective
     M.Objective(ObjectiveSense.Minimize, Expr.Add(Expr.Dot(C, X), x.Index(0)));
     
     // Constraints
     M.Constraint("c1", Expr.Add(Expr.Dot(A1, X), x.Index(0)), Domain.EqualsTo(1.0));
     M.Constraint("c2", Expr.Add(Expr.Dot(A2, X), Expr.Sum(x.Slice(1,3))), Domain.EqualsTo(0.5));
     
     M.Solve();
     
     Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(X.Level()).ToString());
     Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(x.Level()).ToString());
   }
 }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:25,代码来源:sdo1.cs

示例3: Main

        public static void Main(string[] args)
        {
          double[][] A =
            { new double[] { 3.0, 2.0, 0.0, 1.0 },
              new double[] { 2.0, 3.0, 1.0, 1.0 },
              new double[] { 0.0, 0.0, 3.0, 2.0 } };
          double[] c = { 3.0, 5.0, 1.0, 1.0  };

          // Create a model with the name 'lo1'
          Model M = new Model("lo1");
            
          // Create variable 'x' of length 3
          Variable x = M.Variable("x", 3, Domain.GreaterThan(0.0));
          // Create variable 'y' of length 1
          Variable y = M.Variable("y", 1, Domain.InRange(0.0, 10.0));
          // Create a variable vector consisting of x and y
          Variable z = Variable.Vstack(x,y);
            
          // Create three constraints
          M.Constraint("c1", Expr.Dot(A[0], z), Domain.EqualsTo(30.0));
          M.Constraint("c2", Expr.Dot(A[1], z), Domain.GreaterThan(15.0));
          M.Constraint("c3", Expr.Dot(A[2], z), Domain.LessThan(25.0));
            
          // Set the objective function to (c^t * x)
          M.Objective("obj", ObjectiveSense.Maximize, Expr.Dot(c, z));
                  
          // Solve the problem
          M.Solve();
            
          // Get the solution values
          double[] sol = z.Level();
          Console.WriteLine("x1,x2,x3,y = {0}, {1}, {2}, {3}",sol[0],sol[1],sol[2],sol[3]);
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:33,代码来源:lo1_fusion.cs

示例4: Main

        public static void Main(string[] argv)
        {
          int N = 5;
          var A = new double[][]
              { new double[] { 0.0,  0.5,  -0.1,  -0.2,   0.5},
                new double[] { 0.5,  1.25, -0.05, -0.1,   0.25},
                new double[] {-0.1, -0.05,  0.51,  0.02, -0.05},
                new double[] {-0.2, -0.1,   0.02,  0.54, -0.1},
                new double[] { 0.5,  0.25, -0.05, -0.1,   1.25} };

          // Create a model with the name 'NearestCorrelation
          using (var M = new Model("NearestCorrelation"))
          {
            // Setting up the variables
            var X = M.Variable("X", Domain.InPSDCone(N));
            var t = M.Variable("t", 1, Domain.Unbounded());

            // (t, vec (A-X)) \in Q
            M.Constraint( Expr.Vstack(t, Vec(Expr.Sub(new DenseMatrix(A),X))), Domain.InQCone() );

            // diag(X) = e
            M.Constraint(X.Diag(), Domain.EqualsTo(1.0));

            // Objective: Minimize t
            M.Objective(ObjectiveSense.Minimize, t);
                              
            // Solve the problem
            M.Solve();

            // Get the solution values
            Console.WriteLine("X = {0}",arrtostr(X.Level()));
            
            Console.WriteLine("t = {0}", arrtostr(t.Level()));
          } 
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:35,代码来源:nearestcorr.cs

示例5: Main

 public static void Main(string[] args)
 {
   using (Model M = new Model("cqo1"))
   {
           
     Variable x = M.Variable("x", 3, Domain.GreaterThan(0.0));
     Variable y = M.Variable("y", 3, Domain.Unbounded());
           
     // Create the aliases 
     //      z1 = [ y[0],x[0],x[1] ]
     //  and z2 = [ y[1],y[2],x[2] ]
     Variable z1 = Variable.Vstack(y.Index(0),  x.Slice(0,2));
     Variable z2 = Variable.Vstack(y.Slice(1,3),x.Index(2));
         
     // Create the constraint
     //      x[0] + x[1] + 2.0 x[2] = 1.0
     double[] aval = new double[] {1.0, 1.0, 2.0};
     M.Constraint("lc", Expr.Dot(aval, x), Domain.EqualsTo(1.0));
         
     // Create the constraints
     //      z1 belongs to C_3
     //      z2 belongs to K_3
     // where C_3 and K_3 are respectively the quadratic and
     // rotated quadratic cone of size 3, i.e.
     //                 z1[0] > sqrt(z1[1]^2 + z1[2]^2)
     //  and  2.0 z2[0] z2[1] > z2[2]^2
     Constraint qc1 = M.Constraint("qc1", z1.AsExpr(), Domain.InQCone());
     Constraint qc2 = M.Constraint("qc2", z2.AsExpr(), Domain.InRotatedQCone());
         
     // Set the objective function to (y[0] + y[1] + y[2])
     M.Objective("obj", ObjectiveSense.Minimize, Expr.Sum(y));
         
     // Solve the problem
     M.Solve();
         
     // Get the linearsolution values
     double[] solx = x.Level();
     double[] soly = y.Level();
         
     Console.WriteLine("x1,x2,x3 = {0}, {1}, {2}",solx[0],solx[1],solx[2]);
     Console.WriteLine("y1,y2,y3 = {0}, {1}, {2}",soly[0],soly[1],soly[2]);
     // Get conic solution of qc1
     double[] qc1lvl = qc1.Level();
     double[] qc1sn  = qc1.Dual();
                 
     Console.Write("qc1 levels = {0}", qc1lvl[0]);
     for (int i = 1; i < qc1lvl.Length; ++i)
       Console.Write(", {0}", qc1lvl[i]);
     Console.WriteLine();
         
     Console.Write("qc1 dual conic var levels = {0}", qc1sn[0]);
     for (int i = 1; i < qc1sn.Length; ++i)
       Console.Write(", {0}", qc1sn[i]);
     Console.WriteLine();
   }
 }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:56,代码来源:cqo1_fusion.cs

示例6: nn_semiinf

        // Models the cone of nonnegative polynomials on the semi-infinite interval [0,inf)
        public static void nn_semiinf(Model M, Variable x)
        {
          int n = (int)x.Size()-1;
          int n1 = n/2, 
              n2 = (n-1)/2;
          
          // Setup variables
          Variable X1 = M.Variable(Domain.InPSDCone(n1+1));
          Variable X2 = M.Variable(Domain.InPSDCone(n2+1));

          // x_i = Tr H(n1, i) * X1 + Tr H(n2,i-1) * X2, i=0,...,n
          for (int i = 0; i < n+1; ++i)
            M.Constraint( Expr.Sub(x.Index(i), 
                                   Expr.Add(Expr.Dot(Hankel(n1,i,1.0),  X1), 
                                            Expr.Dot(Hankel(n2,i-1,1.0),X2))), Domain.EqualsTo(0.0) );
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:17,代码来源:sospoly.cs

示例7: Main

    public static void Main(string[] args)
    {
      double[][] A = 
        { new double[] { 50.0, 31.0 },
          new double[] { 3.0,  -2.0 } };
      double[] c = { 1.0, 0.64 };
      using (Model M = new Model("milo1"))
      {
        Variable x = M.Variable("x", 2, Domain.GreaterThan(0.0), Domain.IsInteger());

        // Create the constraints
        //      50.0 x[0] + 31.0 x[1] <= 250.0
        //       3.0 x[0] -  2.0 x[1] >= -4.0
        M.Constraint("c1", Expr.Dot(A[0], x), Domain.LessThan(250.0));
        M.Constraint("c2", Expr.Dot(A[1], x), Domain.GreaterThan(-4.0));

        // Set max solution time
        M.SetSolverParam("mioMaxTime", 60.0);
        // Set max relative gap (to its default value)
        M.SetSolverParam("mioTolRelGap", 1e-4);
        // Set max absolute gap (to its default value)
        M.SetSolverParam("mioTolAbsGap", 0.0);

        // Set the objective function to (c^T * x)
        M.Objective("obj", ObjectiveSense.Maximize, Expr.Dot(c, x));

        // Solve the problem
        M.Solve();

        // Get the solution values
        double[] sol = x.Level();
        Console.WriteLine("x1,x2 = {0}, {1}", sol[0], sol[1]);
        Console.WriteLine("MIP rel gap = {0} ({0})",M.GetSolverDoubleInfo("mioObjRelGap"),M.GetSolverDoubleInfo("mioObjAbsGap"));
      }
    }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:35,代码来源:milo1_fusion.cs

示例8: Main

 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]);
   }
 }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:30,代码来源:baker.cs

示例9: Main

   public static void Main(string[] args)
   {
     // Create a model object
     using (Model M = new Model("simple"))
     {
 
       // Add two variables
       Variable x = M.Variable("x",1,Domain.InRange(0.0,1.0));
       Variable y = M.Variable("y",1,Domain.Unbounded());
   
       // Add a constraint on the variables
       M.Constraint("bound y", Expr.Sub(y,x), Domain.InRange(-1.0, 2.0));
   
       // Define the objective
       M.Objective("obj", ObjectiveSense.Maximize, Expr.Add(x,y));
   
       // Solve the problem
       M.Solve();
   
       // Print the solution
       Console.WriteLine("Solution. x = {0}, y = {1}",x.Level()[0],y.Level()[0]);
     }
   }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:23,代码来源:simple.cs

示例10: nn_inf

        // Models the cone of nonnegative polynomials on the real axis
        public static void nn_inf(Model M, Variable x)
        {
          int m = (int)x.Size() - 1;
          int n = (m/2); // degree of polynomial is 2*n

          //assert(m == 2*n)    

          // Setup variables
          Variable X = M.Variable(Domain.InPSDCone(n+1));

          // x_i = Tr H(n, i) * X  i=0,...,m
          for (int i = 0; i < m+1; ++i)
            M.Constraint( Expr.Sub(x.Index(i), Expr.Dot(Hankel(n,i,1.0),X)), Domain.EqualsTo(0.0));
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:15,代码来源:sospoly.cs

示例11: Main

        public static void Main(string[] args)
        {
          using (Model M = new Model("FacilityLocation"))
          {
  // Variable holding the facility location
            Variable f = M.Variable("facility",new NDSet(1,2),Domain.Unbounded());
            // Variable defining the euclidian distances to each customer
            Variable d = M.Variable("dist", new NDSet(N,1), Domain.GreaterThan(0.0));
            // Variable defining the x and y differences to each customer;
            Variable t = M.Variable("t",new NDSet(N,2), Domain.Unbounded());
            M.Constraint("dist measure", 
                         Variable.Stack(new Variable[][] { new Variable[] { d, t }}), 
                         Domain.InQCone(N,3));

            Variable fxy = Variable.Repeat(f, N);
            M.Constraint("xy diff", Expr.Add(t, fxy), Domain.EqualsTo(customerloc));

            M.Objective("total_dist", ObjectiveSense.Minimize, Expr.Sum(d));
            
            M.Solve();
            double[] floc = f.Level();
            Console.WriteLine("Facility location = {0},{1}",floc[0],floc[1]);
          }
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:24,代码来源:facility_location.cs

示例12: Main

          public static void Main(string[] args) 
          {
              double[][] A =  new double[][] { new double[] { -0.5, 1.0 }  };
              double[]   b =  new double[] { 1.0 };
              double[]   c =  new double[] { 1.0, 1.0 };

              using (Model M = new Model("duality"))
              {
                Variable x = M.Variable("x", 2, Domain.GreaterThan(0.0));

                Constraint con = M.Constraint(Expr.Sub(b, Expr.Mul(new DenseMatrix(A), x)), Domain.EqualsTo(0.0));

                M.Objective("obj", ObjectiveSense.Minimize, Expr.Dot(c, x));

                M.Solve();
                double[] xsol = x.Level();
                double[] ysol = con.Dual();

                Console.WriteLine("x1,x2,y = {0}, {1}, {2}\n",xsol[0],xsol[1],ysol[0]);
              }
          }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:21,代码来源:duality.cs

示例13: so

        /*
            Description:
                Extends the basic Markowitz model with a market cost term.

            Input:
                n: Number of assets
                mu: An n dimmensional vector of expected returns
                GT: A matrix with n columns so (GT')*GT  = covariance matrix'
                x0: Initial holdings 
                w: Initial cash holding
                gamma: Maximum risk (=std. dev) accepted
                m: It is assumed that  market impact cost for the j'th asset is
                   m_j|x_j-x0_j|^3/2

            Output:
               Optimal expected return and the optimal portfolio     

        */
        public static void MarkowitzWithMarketImpact
          ( int n,
            double[] mu,
            DenseMatrix GT,
            double[] x0,
            double   w,
            double   gamma,
            double[] m,
            
            double[] xsol,
            double[] tsol)
        {
          using(Model M = new Model("Markowitz portfolio with market impact"))
          {

            //M.SetLogHandler(Console.Out);
          
            // Defines the variables. No shortselling is allowed.
            Variable x = M.Variable("x", n, Domain.GreaterThan(0.0));

            // Addtional "helper" variables 
            Variable t = M.Variable("t", n, Domain.Unbounded());
            Variable z = M.Variable("z", n, Domain.Unbounded());  
            Variable v = M.Variable("v", n, Domain.Unbounded());      

            //  Maximize expected return
            M.Objective("obj", ObjectiveSense.Maximize, Expr.Dot(mu,x));

            // Invested amount + slippage cost = initial wealth
            M.Constraint("budget", Expr.Add(Expr.Sum(x),Expr.Dot(m,t)), Domain.EqualsTo(w+sum(x0)));

            // Imposes a bound on the risk
            M.Constraint("risk", Expr.Vstack(Expr.ConstTerm(new double[] {gamma}),Expr.Mul(GT,x)), Domain.InQCone());

            // z >= |x-x0| 
            M.Constraint("buy", Expr.Sub(z,Expr.Sub(x,x0)),Domain.GreaterThan(0.0));
            M.Constraint("sell", Expr.Sub(z,Expr.Sub(x0,x)),Domain.GreaterThan(0.0));

            // t >= z^1.5, z >= 0.0. Needs two rotated quadratic cones to model this term
            M.Constraint("ta", Expr.Hstack(v.AsExpr(),t.AsExpr(),z.AsExpr()),Domain.InRotatedQCone());

            M.Constraint("tb", Expr.Hstack(z.AsExpr(),Expr.ConstTerm(n,1.0/8.0),v.AsExpr()),
                         Domain.InRotatedQCone());
            M.Solve();  
       
            if (xsol != null) 
              Array.Copy(x.Level(),xsol,n);
            if (tsol != null)
              Array.Copy(t.Level(),tsol,n);
          }
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:69,代码来源:portfolio.cs

示例14: to

        /** 
            Purpose: Models the convex set 

              S = { (x, t) \in R^n x R | x >= 0, t <= (x1 * x2 * ... *xn)^(1/n) }.

            as the intersection of rotated quadratic cones and affine hyperplanes,
            see [1, p. 105].  This set can be interpreted as the hypograph of the 
            geometric mean of x.

            We illustrate the modeling procedure using the following example.
            Suppose we have 

               t <= (x1 * x2 * x3)^(1/3)

            for some t >= 0, x >= 0. We rewrite it as

               t^4 <= x1 * x2 * x3 * x4,   x4 = t

            which is equivalent to (see [1])

               x11^2 <= 2*x1*x2,   x12^2 <= 2*x3*x4,

               x21^2 <= 2*x11*x12,

               sqrt(8)*x21 = t, x4 = t.

            References:
            [1] "Lectures on Modern Optimization", Ben-Tal and Nemirovski, 2000. 
        */ 
        public static void geometric_mean(Model M, Variable x, Variable t)
        {
          int n = (int)x.Size();
          int l = (int)System.Math.Ceiling(log2(n));
          int m = pow2(l) - n;
                  
          Variable x0;

          if (m == 0)
              x0 = x;
          else    
              x0 = Variable.Vstack(x, M.Variable(m, Domain.GreaterThan(0.0)));

          Variable z = x0;

          for (int i = 0; i < l-1; ++i)
          {
            Variable xi = M.Variable(pow2(l-i-1), Domain.GreaterThan(0.0));
            for (int k = 0; k < pow2(l-i-1); ++k)
              M.Constraint(Variable.Vstack(z.Index(2*k),z.Index(2*k+1),xi.Index(k)),
                           Domain.InRotatedQCone());
              z = xi;
          }
              
          Variable t0 = M.Variable(1, Domain.GreaterThan(0.0));
          M.Constraint(Variable.Vstack(z, t0), Domain.InRotatedQCone());

          M.Constraint(Expr.Sub(Expr.Mul(System.Math.Pow(2,0.5*l),t),t0), Domain.EqualsTo(0.0));

          for (int i = pow2(l-m); i < pow2(l); ++i)
            M.Constraint(Expr.Sub(x0.Index(i), t), Domain.EqualsTo(0.0));
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:61,代码来源:lownerjohn_ellipsoid.cs

示例15: lownerjohn_inner

        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;
          }
        }
开发者ID:edljk,项目名称:Mosek.jl,代码行数:38,代码来源:lownerjohn_ellipsoid.cs


注:本文中的Model.Variable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。