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


C# Decision.SetBinding方法代码示例

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


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

示例1: CreateModel

        private void CreateModel()
        {
            context = SolverContext.GetContext();
            context.ClearModel();
            model = context.CreateModel();
            items = new Set(Domain.Any, "items");

            cost = new Parameter(Domain.Integer, "cost", items);
            cost.SetBinding(Round.Players, "Cost", "Name");

            score = new Parameter(Domain.Integer, "score", items);
            score.SetBinding(Round.Players, "Score", "Name");

            team = new Parameter(Domain.IntegerNonnegative, "team", items);
            team.SetBinding(Round.Players, "TeamId", "Name");

            dummy = new Parameter(Domain.Boolean, "dummy", items);
            dummy.SetBinding(Round.Players, "IsDummyPlayer", "Name");

            model.AddParameters(cost, score, team, dummy);

            choose = new Decision(Domain.IntegerRange(0, 1), "choose", items);
            choose.SetBinding(Round.Players, "SelectedValue", "Name");
            model.AddDecision(choose);
        }
开发者ID:ungood,项目名称:misc,代码行数:25,代码来源:TeamPicker.cs

示例2: Solve

    public void Solve(RvolSolveModel model_)
    {
      var context = SolverContext.GetContext();

      context.ClearModel();

      var ccyModel = context.CreateModel();

      buildCovariants(model_.Covar);

      {
        // create an integer set with ccys
        Set ccys = new Set(Domain.Integer, "ccys");

        //
        // parameters
        //

        Parameter minwt = new Parameter(Domain.Real, "minwt", ccys);
        minwt.SetBinding<CurrencyLine>(model_.CurrencyLines, "MinWeight", "Id");

        Parameter maxwt = new Parameter(Domain.Real, "maxwt", ccys);
        maxwt.SetBinding<CurrencyLine>(model_.CurrencyLines, "MaxWeight", "Id");

        Parameter expreturn = new Parameter(Domain.Real, "expreturn", ccys);
        expreturn.SetBinding<CurrencyLine>(model_.CurrencyLines, "ExpectedReturn", "Id");

        Parameter tvol = new Parameter(Domain.Real, "tvol");
        tvol.SetBinding(Math.Pow(0.01215914, 2d));

        Parameter pCovariants = new Parameter(Domain.Real, "Covariants", ccys, ccys);
        pCovariants.SetBinding(Covariants, "Variance", "CcyI", "CcyJ");

        ccyModel.AddParameters(minwt, maxwt, expreturn, tvol, pCovariants);

        //
        // decisions
        //

        Decision allocations = new Decision(Domain.Real, "wt", ccys);
        allocations.SetBinding(model_.CurrencyLines, "Weight", "Id");

        ccyModel.AddDecisions(allocations);

        //
        // constraints
        //

        // weight limits

        ccyModel.AddConstraint("wtbounds", Model.ForEach(ccys, s => minwt[s] <= allocations[s] <= maxwt[s]));

        // sum of weights constraint

        if (model_.SumOfWeightsMin.HasValue && model_.SumOfWeightsMax.HasValue && Math.Abs(model_.SumOfWeightsMin.Value - model_.SumOfWeightsMax.Value) < 0.000001)
        {
          ccyModel.AddConstraint("usdcontraint", Model.Equal(model_.SumOfWeightsMin.Value, Model.Sum(Model.ForEach(ccys, x => allocations[x]))));
        }
        else
        {
          if (model_.SumOfWeightsMin.HasValue)
            ccyModel.AddConstraint("usdconstraintMin",
              Model.LessEqual(model_.SumOfWeightsMin, Model.Sum(Model.ForEach(ccys, s => allocations[s]))));

          if (model_.SumOfWeightsMax.HasValue)
            ccyModel.AddConstraint("usdconstraintMax",
              Model.GreaterEqual(model_.SumOfWeightsMax, Model.Sum(Model.ForEach(ccys, s => allocations[s]))));
        }

        // targetting a specific volatility

        ccyModel.AddConstraint("variance",
          Model.GreaterEqual(tvol,
              //Model.Sqrt
              (
                Model.Sum
                  (
                    Model.ForEach
                      (
                        ccys, CcyI =>
                          Model.ForEach
                            (
                              ccys, CcyJ =>
                                Model.Product(pCovariants[CcyI, CcyJ], allocations[CcyI], allocations[CcyJ])
                            )
                      )
                  )
              )
            )
          );


        // goal

        ccyModel.AddGoal("maxexpreturn", GoalKind.Maximize,
          Model.Sum(Model.ForEach(ccys, s => expreturn[s]*allocations[s])));

      }

      var soluation = context.Solve();
//.........这里部分代码省略.........
开发者ID:heimanhon,项目名称:researchwork,代码行数:101,代码来源:RvolSolver.cs

示例3: Solve

        public string Solve()
        {
            /***************************
            /*Construction of the model*
            /***************************/
            SolverContext context = SolverContext.GetContext();
            //For repeating the solution with other minimum returns
            context.ClearModel();

            //Create an empty model from context
            Model portfolio = context.CreateModel();

            //Create a string set with stock names
            Set setStocks = new Set(Domain.Any, "Stocks");

            /****Decisions*****/

            //Create decisions bound to the set. There will be as many decisions as there are values in the set
            Decision allocations = new Decision(Domain.RealNonnegative, "Allocations", setStocks);
            allocations.SetBinding(StocksHistory, "Allocation", "Stock");
            portfolio.AddDecision(allocations);

            /***Parameters***/

            //Create parameters bound to Covariant matrix
            Parameter pCovariants = new Parameter(Domain.Real, "Covariants", setStocks, setStocks);
            pCovariants.SetBinding(Covariants, "Variance", "StockI", "StockJ");

            //Create parameters bound to mean performance of the stocks over 12 month period
            Parameter pMeans = new Parameter(Domain.Real, "Means", setStocks);
            pMeans.SetBinding(StocksHistory, "Mean", "Stock");

            portfolio.AddParameters(pCovariants, pMeans);

            /***Constraints***/

            //Portion of a stock should be between 0 and 1
            portfolio.AddConstraint("portion", Model.ForEach(setStocks, stock => 0 <= allocations[stock] <= 1));

            //Sum of all allocations should be equal to unity
            portfolio.AddConstraint("SumPortions", Model.Sum(Model.ForEach(setStocks, stock => allocations[stock])) == 1);

            //Expected minimum return
            portfolio.AddConstraint("ROI", Model.Sum(Model.ForEach(setStocks, stock => Model.Product(allocations[stock], pMeans[stock]))) >= MinROI);

            /***Goals***/

            portfolio.AddGoal("Variance", GoalKind.Minimize, Model.Sum
                                                             (
                                                                Model.ForEach
                                                                (
                                                                    setStocks, stockI =>
                                                                    Model.ForEach
                                                                    (
                                                                        setStocks, stockJ =>
                                                                       Model.Product(pCovariants[stockI, stockJ], allocations[stockI], allocations[stockJ])
                                                                    )
                                                                )
                                                            )
                             );
            /************************************************
               /*Add SolveEvent to watch the solving progress  *
               /************************************************/

            EventHandler<SolvingEventArgs> handler = new EventHandler<SolvingEventArgs>(SolvingEventHandler);

            // ensure the handler is registered only once when hit the Solve button again.
            context.Solving -= handler;
            context.Solving += handler;

            /*******************
            /*Solve the model  *
            /*******************/

            //Use IPM algorithm
            Solution solution = context.Solve(new InteriorPointMethodDirective());

            //Save the decisions back to the array
            if (solution.Quality == SolverQuality.Optimal)
                context.PropagateDecisions();

            using (TextWriter tw = new StreamWriter("portfolio.qps"))
            {
                context.SaveModel(FileFormat.MPS, tw);
            }

            Report report = solution.GetReport();
            return report.ToString();
        }
开发者ID:antonydrew,项目名称:GPU-SuperComputing-HPC-,代码行数:89,代码来源:Portfolio.cs


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