本文整理汇总了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);
}
示例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();
//.........这里部分代码省略.........
示例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();
}