本文整理汇总了C#中Parameter.SetBinding方法的典型用法代码示例。如果您正苦于以下问题:C# Parameter.SetBinding方法的具体用法?C# Parameter.SetBinding怎么用?C# Parameter.SetBinding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parameter
的用法示例。
在下文中一共展示了Parameter.SetBinding方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
SolverContext context = SolverContext.GetContext();
Model model = context.CreateModel();
// ------------
// Parameters
Set city = new Set(Domain.IntegerNonnegative, "city");
Parameter dist = new Parameter(Domain.Real, "dist", city, city);
var arcs = from p1 in data
from p2 in data
select new Arc { City1 = p1.Name, City2 = p2.Name, Distance = p1.Distance(p2) };
dist.SetBinding(arcs, "Distance", "City1", "City2");
model.AddParameters(dist);
// ------------
// Decisions
Decision assign = new Decision(Domain.IntegerRange(0, 1), "assign", city, city);
Decision rank = new Decision(Domain.RealNonnegative, "rank", city);
model.AddDecisions(assign, rank);
// ------------
// Goal: minimize the length of the tour.
Goal goal = model.AddGoal("TourLength", GoalKind.Minimize,
Model.Sum(Model.ForEach(city, i => Model.ForEachWhere(city, j => dist[i, j] * assign[i, j], j => i != j))));
// ------------
// Enter and leave each city only once.
int N = data.Length;
model.AddConstraint("assign1",
Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j],
j => i != j)) == 1));
model.AddConstraint("assign2",
Model.ForEach(city, j => Model.Sum(Model.ForEachWhere(city, i => assign[i, j], i => i != j)) == 1));
// Forbid subtours (Miller, Tucker, Zemlin - 1960...)
model.AddConstraint("no_subtours",
Model.ForEach(city,
i => Model.ForEachWhere(city,
j => rank[i] + 1 <= rank[j] + N * (1 - assign[i, j]),
j => Model.And(i != j, i >= 1, j >= 1)
)
)
);
Solution solution = context.Solve();
// Retrieve solution information.
Console.WriteLine("Cost = {0}", goal.ToDouble());
Console.WriteLine("Tour:");
var tour = from p in assign.GetValues() where (double)p[0] > 0.9 select p[2];
foreach (var i in tour.ToArray())
{
Console.Write(i + " -> ");
}
Console.WriteLine();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
示例2: Run
public static Tuple<ICollection<SubCalendarEvent>, double> Run(ICollection<SubCalendarEvent> ListOfElements, int BeginningAndEnd=0)
{
#if EnableHive
if (ListOfElements.Count < 3)
{
return new Tuple<ICollection<SubCalendarEvent>, double>(ListOfElements, 0);
}
CitiesData citiesData = new CitiesData(ListOfElements.ToList());
int totalNumberBees = 100;
int numberInactive = 20;
int numberActive = 50;
int numberScout = 30;
int maxNumberVisits = 50;
int maxNumberCycles = 20;
Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, citiesData, BeginningAndEnd);
bool doProgressBar = false;
hive.Solve(doProgressBar);
return hive.getBestPath();
#endif
#if LinearTSP
Coordinate[] data = new Coordinate[ListOfElements.Count];
Dictionary<int, SubCalendarEvent> DictOFData = new Dictionary<int,SubCalendarEvent>();
int NameIndex = 0;
foreach (SubCalendarEvent eachSubCalendarEvent in ListOfElements)
{
data[NameIndex] = new Coordinate(NameIndex, eachSubCalendarEvent);
DictOFData.Add(NameIndex++, eachSubCalendarEvent);
}
SolverContext context = SolverContext.GetContext();
Model model = context.CreateModel();
// ------------
// Parameters
Set city = new Set(Domain.IntegerNonnegative, "city");
Parameter dist = new Parameter(Domain.Real, "dist", city, city);
var arcs = from p1 in data
from p2 in data
select new Arc { City1 = p1.Name, City2 = p2.Name, Distance = p1.Distance(p2, data.Length) };
dist.SetBinding(arcs, "Distance", "City1", "City2");
model.AddParameters(dist);
// ------------
// Decisions
Decision assign = new Decision(Domain.IntegerRange(0, 1), "assign", city, city);
Decision rank = new Decision(Domain.RealNonnegative, "rank", city);
model.AddDecisions(assign, rank);
// ------------
// Goal: minimize the length of the tour.
Goal goal = model.AddGoal("TourLength", GoalKind.Minimize,
Model.Sum(Model.ForEach(city, i => Model.ForEachWhere(city, j => dist[i, j] * assign[i, j], j => i != j))));
// ------------
// Enter and leave each city only once.
int N = data.Length;
model.AddConstraint("assign_1",
Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j],
j => i != j)) == 1));
model.AddConstraint("assign_2",
Model.ForEach(city, j => Model.Sum(Model.ForEachWhere(city, i => assign[i, j], i => i != j)) == 1));
// Forbid subtours (Miller, Tucker, Zemlin - 1960...)
model.AddConstraint("no_subtours",
Model.ForEach(city,
i => Model.ForEachWhere(city,
j => rank[i] + 1 <= rank[j] + N * (1 - assign[i, j]),
j => Model.And(i != j, i >= 1, j >= 1)
)
)
);
Solution solution = context.Solve();
double Cost = goal.ToDouble();
List<SubCalendarEvent> OptimizedSubCalEvents = new List<SubCalendarEvent>();
var tour = from p in assign.GetValues() where (double)p[0] > 0.9 select p;
foreach (var i in tour.ToArray())
{
int MyIndex =Convert.ToInt32(i[2]);
OptimizedSubCalEvents.Add(DictOFData[MyIndex]);
//Console.WriteLine(i[1] + " -> " + );
}
context.ClearModel();
return new Tuple<ICollection<SubCalendarEvent>, double>(OptimizedSubCalEvents, Cost);
#endif
//.........这里部分代码省略.........
示例3: 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);
}
示例4: 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();
//.........这里部分代码省略.........
示例5: 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();
}