我有一个Excel工作簿,它使用Solver加载项来最大化其中有平方根的方程组(例如,它是非线性的)。我试图使用Microsoft Solver Foundation在C#中重新实现它。我尝试了一些不同的指令但是却找不到能够重现我在Excel中得到的结果的求解器。
我尝试使用混合本地搜索,但结果出错了,结果最大化并不接近excel。如果我使用内点法并删除平方根(来自excel和c#),我非常接近excel优化,但这对我没用,因为我试图匹配包含正方形的excel模型根。
我认为Hybrid本地搜索的问题在于我没有获得全局最大值。我没有找到任何支持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();
}