3

我有一个 Excel 工作簿,它使用 Solver 加载项来最大化其中包含平方根的一组方程(例如,它是非线性的)。我正在尝试使用 Microsoft Solver Foundation 在 C# 中重新实现这一点。我尝试了一些不同的指令,但无法找到一个求解器来重现我在 Excel 中得到的结果。

我尝试使用混合本地搜索,但结果完全错误,并且结果最大化并没有接近 excel。如果我使用内点法并删除平方根(从 excel 和 c# 中),我非常接近 excel 优化,但这对我没有用,因为我试图匹配包含正方形的 excel 模型根。

我认为混合本地搜索的问题在于我没有获得全局最大值。我没有找到任何其他支持 NLP 的内置指令。

我认为 Excel Solver 使用 GRG2 算法。有什么方法可以重现 MSF 中 Excel 求解器使用的算法?

作为参考,下面是 MSF 附带的 QP 示例,其中我在注释 '// #######' 之前所做的更改:

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);


            /***Goals***/

            portfolio.AddGoal("Variance", GoalKind.Maximize,
                // ####### Include a inner product of the means and weights  to form utility curve
                Model.Sum
                (
                    Model.ForEach
                        (
                            setStocks, x =>
                                        Model.Product(
                                            allocations[x],
                                            pMeans[x])
                        )
                )
                -
                // ####### Use square root of variance to get volatility instead.  This makes the problem non-linear
                Model.Sqrt(
                    Model.Sum
                    (
                        Model.ForEach
                        (
                            setStocks, stockI =>
                            Model.ForEach
                            (
                                setStocks, stockJ =>
                                Model.Product(pCovariants[stockI, stockJ], allocations[stockI], allocations[stockJ])
                            )
                        )
                    )
                )
             );
            // ####### remove event handler        

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

            // ####### Use an NLP algorithm directive
            Solution solution = context.Solve(new HybridLocalSearchDirective());

            // ####### remove conditions on propagate
                context.PropagateDecisions();

            // ####### Remove save.  Can't save an NLP Model

            Report report = solution.GetReport();
            return report.ToString();
        }
4

0 回答 0