//Affine Funciton Approximation public static Cplex Build_CD1_Model(IALPFTMDP aff) { Cplex model = new Cplex(); IObjective cost = model.AddMaximize(); IRange[][] constraint1 = new IRange[aff.TimeHorizon][]; IRange[] constraint2 = new IRange[aff.TimeHorizon]; #region //////////////生成约束////////////// for (int i = 0; i < aff.TimeHorizon; i++) { constraint1[i] = new IRange[aff.RS.Count]; foreach (IALPResource re in aff.RS) { if (i == 0) { constraint1[i][aff.RS.IndexOf(re)] = model.AddRange((aff.InitialState as IALPState)[re], (aff.InitialState as IALPState)[re]); } else { constraint1[i][aff.RS.IndexOf(re)] = model.AddRange(0, 0); } } constraint2[i] = model.AddRange(1, 1); } #endregion #region //////////////生成变量////////////// for (int t = 0; t < aff.TimeHorizon; t++) { foreach (IALPState s in aff.SS) { foreach (IMDPDecision a in aff.GenDecisionSpace(s)) { Column col = model.Column(cost, aff.Rt(t, a)); foreach (IALPResource re in aff.RS) { col = col.And(model.Column(constraint1[t][aff.RS.IndexOf(re)], (s as IALPState)[re])); if (t < aff.TimeHorizon - 1) { col = col.And(model.Column(constraint1[t + 1][aff.RS.IndexOf(re)], (aff.Qti(t, re, a)) - (s as IALPState)[re])); } } col = col.And(model.Column(constraint2[t], 1)); model.NumVar(col, 0, double.MaxValue, NumVarType.Float); } } } #endregion return(model); }
//Piece-Wise Function Approximation public static Cplex Build_Dual_PW_Model(IALPFTMDP aff) { Cplex model = new Cplex(); IObjective cost = model.AddMaximize(); #region //////////////生成约束////////////// IRange[][][] constraint1 = new IRange[aff.TimeHorizon][][]; IRange[] constraint2 = new IRange[aff.TimeHorizon]; for (int i = 0; i < aff.TimeHorizon; i++) { constraint1[i] = new IRange[aff.RS.Count][]; foreach (IALPResource re in aff.RS) { constraint1[i][aff.RS.IndexOf(re)] = new IRange[(aff.InitialState as IALPState)[re]]; for (int k = 1; k < (aff.InitialState as IALPState)[re]; k++) { if (i == 0) { constraint1[i][aff.RS.IndexOf(re)][k - 1] = model.AddRange(1, 1); } else { constraint1[i][aff.RS.IndexOf(re)][k - 1] = model.AddRange(0, 0); } } } constraint2[i] = model.AddRange(1, 1); } #endregion #region //////////////生成变量////////////// //System.Threading.Tasks.Parallel.For(0, aff.TimeHorizon, (t) => for (int t = 0; t < aff.TimeHorizon; t++) { foreach (IALPState s in aff.SS) { foreach (IMDPDecision a in aff.GenDecisionSpace(s)) { //目标函数 Column col = model.Column(cost, aff.Reward(t, s, a)); // System.Console.WriteLine(aff.Reward(t, s, a)); //第一类约束 foreach (IALPResource re in aff.RS) { for (int k = 1; k < (aff.InitialState as IALPState)[re]; k++) { if (s[re] >= k) { col = col.And(model.Column(constraint1[t][aff.RS.IndexOf(re)][k - 1], 1)); if (t < aff.TimeHorizon - 1) { col = col.And(model.Column(constraint1[t + 1][aff.RS.IndexOf(re)][k - 1], -1)); if (s[re] == k) { col = col.And(model.Column(constraint1[t + 1][aff.RS.IndexOf(re)][k - 1], (aff.Qti(t, re, a)))); } } } } } //第二类约束 col = col.And(model.Column(constraint2[t], 1)); model.NumVar(col, 0, double.MaxValue, NumVarType.Float); } } } ///); #endregion model.SetOut(null); return(model); }