protected virtual bool CG1(out IALPDecision deci_a) { deci_a = null; double temp = 0; foreach (IALPDecision d in Data.DS) { double temp1 = 0; temp1 += AggSita - Sita[alpha + 1]; foreach (IALPResource re in Data.RS) { if (d.UseResource(re)) { temp1 += AggV[Data.RS.IndexOf(re)] * (1 + alpha) * Data.Qti(alpha, re, d); } } temp1 = (1 + alpha) * Data.Rt(alpha, d) - temp1; if (temp1 > temp) { deci_a = d; temp = temp1; } } if (temp <= Tolerance || Aggconstraints.ContainsKey(deci_a)) { return(true); } else { return(false); } }
protected void AddConstraint(int t, IALPDecision a)//Add a column into RMP model { if (constraints[t].ContainsKey(a)) { return; } INumExpr exp1 = RMPModel.NumExpr(); if (t < Data.TimeHorizon - 1) { exp1 = RMPModel.Sum(Var2[t], RMPModel.Prod(-1, Var2[t + 1])); foreach (IALPResource re in Data.RS) { if (a.UseResource(re)) { exp1 = RMPModel.Sum(exp1, Var1[t][Data.RS.IndexOf(re)], RMPModel.Prod(Data.Qti(t, re, a) - 1, Var1[t + 1][Data.RS.IndexOf(re)])); } } } else { exp1 = RMPModel.Sum(exp1, Var2[t]); foreach (IALPResource re in Data.RS) { if (a.UseResource(re)) { exp1 = RMPModel.Sum(exp1, Var1[t][Data.RS.IndexOf(re)]); } } } constraints[t].Add(a, RMPModel.AddGe(exp1, Data.Rt(t, a))); }
protected void AddCol(int t, IALPDecision a)//Add a column into RMP model { if (var[t].ContainsKey(a)) { return; } //目标函数 Column col = RMPModel.Column(cost, Data.Rt(t, a)); //第一类约束 foreach (IALPResource re in Data.RS) { for (int k = t + 1; k < Data.TimeHorizon; k++) { col = col.And(RMPModel.Column(constraint1[k][Data.RS.IndexOf(re)], Data.Qti(t, re, a))); } if (a.UseResource(re)) { col = col.And(RMPModel.Column(constraint1[t][Data.RS.IndexOf(re)], 1)); } } //第二类约束 col = col.And(RMPModel.Column(constraint2[t], 1)); INumVar v = RMPModel.NumVar(col, 0, 1, NumVarType.Float); var[t].Add(a, v); }
public virtual void StepFoward() { alpha -= step; //TODO:Delete Agg constraint(s) foreach (var a in Aggconstraints) { RMPModel.Remove(a.Value); } List <IALPDecision> list = Aggconstraints.Keys.ToList(); Aggconstraints.Clear(); //TODO:Add DisAgg variables and constraint(s) for (int i = alpha + 1; i <= alpha + step; i++) { constraints.Add(i, new Dictionary <IALPDecision, IRange>()); Var1.Add(i, RMPModel.NumVarArray(Data.RS.Count, 0, double.MaxValue)); Var2.Add(i, RMPModel.NumVar(0, double.MaxValue)); V.Add(i, new double[Data.RS.Count]); Sita.Add(i, 0); } for (int t = alpha + 1; t <= alpha + step; t++) { if (t < Data.TimeHorizon - 1) { foreach (IALPResource re in Data.RS) { INumExpr exp2 = RMPModel.Sum(Var1[t][Data.RS.IndexOf(re)], RMPModel.Prod(-1, Var1[t + 1][Data.RS.IndexOf(re)])); RMPModel.AddGe(exp2, 0); } INumExpr exp3 = RMPModel.Sum(Var2[t], RMPModel.Prod(-1, Var2[t + 1])); RMPModel.AddGe(exp3, 0); } } foreach (IALPResource re in Data.RS) { INumExpr exp6 = RMPModel.NumExpr(); exp6 = RMPModel.Sum(AggVar1[Data.RS.IndexOf(re)], RMPModel.Prod(-1, Var1[alpha + 1][Data.RS.IndexOf(re)])); RMPModel.AddGe(exp6, 0); } INumExpr exp4 = RMPModel.NumExpr(); exp4 = RMPModel.Sum(exp4, AggVar2, RMPModel.Prod(-1, Var2[alpha + 1])); RMPModel.AddGe(exp4, 0); IALPDecision d = (Data.DS as IALPDecisionSpace).CloseAllDecision() as IALPDecision; AddConstraint1(d); foreach (IALPDecision de in list) { AddConstraint1(de); } for (int t = alpha + 1; t <= alpha + step; t++) { AddConstraint2(t, d); } }
protected virtual void CreateFeasibleSolution() { IALPDecision d = (Data.DS as IALPDecisionSpace).CloseAllDecision() as IALPDecision; for (int t = CurrentTime; t < Data.TimeHorizon; t++) { AddCol(t, d); } }
protected virtual void CreateFeasibleSolution() { IALPDecision d = (Data.DS as IALPDecisionSpace).CloseAllDecision() as IALPDecision; AddConstraint1(d); for (int t = alpha + 1; t < Data.TimeHorizon; t++) { AddConstraint2(t, d); } }
public virtual void DoCalculate()//Calculate the Bid-Price of current time { bool IsOptimal = true; double tempObj = 0; int tol = ObeyTime; for (int iter = 1; ; iter++) { IsOptimal = true; if (RMPModel.Solve()) { #region 判断是否终止 if (RMPModel.GetObjValue() - tempObj < threshold) { tol--; } else { tol = ObeyTime; } tempObj = RMPModel.GetObjValue(); #endregion } UpdateValues(); #region 判断是否终止 if (tol < 0) { print("--------{0}算法达到终止条件而退出--------", System.DateTime.Now.ToLongTimeString()); UpdateBidPrice(); break; } #endregion for (int t = CurrentTime; t < Data.TimeHorizon; t++) { IALPDecision deci_a = null; bool IsSubOpt = CG(t, out deci_a); if (!IsSubOpt) { AddConstraint(t, deci_a); } IsOptimal = IsSubOpt && IsOptimal; } if (IsOptimal) { print("--------已经达到最优!", System.DateTime.Now.ToLongTimeString()); UpdateValues(); UpdateBidPrice(); break; } } }
private bool SolveSubProblem() { bool IsOpt = true; //IALPDecision deci_a1 = null; //bool IsSubOpt1 = CG1(out deci_a1); //if (!IsSubOpt1) //{ // AddConstraint1(deci_a1); //} //IsOpt = IsSubOpt1 && IsOpt; //for (int t = alpha + 1; t < Data.TimeHorizon; t++) //{ // IALPDecision deci_a = null; // bool IsSubOpt = CG2(t, out deci_a); // if (!IsSubOpt) // { // AddConstraint2(t, deci_a); // } // IsOpt = IsSubOpt && IsOpt; //} Task aggta = factory.StartNew(() => { IALPDecision deci_a1 = null; bool IsSubOpt1 = CG1(out deci_a1); if (!IsSubOpt1) { AddConstraint1(deci_a1); } IsOpt = IsSubOpt1 && IsOpt; }, cts.Token); tasks.Add(aggta); for (int iteration = alpha + 1; iteration < Data.TimeHorizon; iteration++) { int t = iteration; Task ta = factory.StartNew(() => { IALPDecision deci_a = null; bool IsSubOpt = CG2(t, out deci_a); if (!IsSubOpt) { AddConstraint2(t, deci_a); } IsOpt = IsSubOpt && IsOpt; }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); return(IsOpt); }
protected virtual bool CG(int t, out IALPDecision deci_a) { deci_a = null; double temp = 0; foreach (IALPDecision d in Data.DS) { double temp1 = 0; if (t < Data.TimeHorizon - 1) { temp1 += Sita[t] - Sita[t + 1]; foreach (IALPResource re in Data.RS) { if (d.UseResource(re)) { temp1 += V[t][Data.RS.IndexOf(re)] + V[t + 1][Data.RS.IndexOf(re)] * (Data.Qti(t, re, d) - 1); } } } else { temp1 += Sita[t]; foreach (IALPResource re in Data.RS) { if (d.UseResource(re)) { temp1 += V[t][Data.RS.IndexOf(re)]; } } } temp1 = Data.Rt(t, d) - temp1; if (temp1 > temp) { deci_a = d; temp = temp1; } } if (temp <= Tolerance || constraints[t].ContainsKey(deci_a)) { return(true); } else { return(false); } }
protected void AddConstraint1(IALPDecision a) { if (Aggconstraints.ContainsKey(a)) { return; } lock (RMPModel) { INumExpr exp1 = RMPModel.NumExpr(); exp1 = RMPModel.Sum(AggVar2, RMPModel.Prod(-1, Var2[alpha + 1])); foreach (IALPResource re in Data.RS) { if (a.UseResource(re)) { exp1 = RMPModel.Sum(exp1, RMPModel.Prod((alpha + 1) * Data.Qti(alpha, re, a), AggVar1[Data.RS.IndexOf(re)])); } } Aggconstraints.Add(a, RMPModel.AddGe(exp1, (alpha + 1) * Data.Rt(alpha, a))); } }
protected virtual bool CG(int t, out IALPDecision deci_a) { deci_a = null; double temp = 0; foreach (IALPDecision d in Data.DS) { double temp1 = 0; temp1 += DualValue2[t]; foreach (IALPResource r in Data.RS) { if (d.UseResource(r)) { temp1 += DualValue1[t][Data.RS.IndexOf(r)]; } double temp2 = 0; for (int k = t + 1; k < Data.TimeHorizon; k++) { temp2 += DualValue1[k][Data.RS.IndexOf(r)]; } temp1 += temp2 * Data.Qti(t, r, d); } temp1 = Data.Rt(t, d) - temp1; if (temp1 > temp) { deci_a = d; temp = temp1; } } if (temp <= Tolerance || var[t].ContainsKey(deci_a)) { return(true); } else { return(false); } }
protected override bool CG1(out IALPDecision deci_a) { return(CG(alpha, out deci_a)); }
public abstract bool TestValidation(); //最优条件判断 protected abstract void Add_Agg_Constraint(int i, IALPDecision a);
private bool CG(int t, out IALPDecision deci_a) { deci_a = null; #region List <Product> list = (Data.ProSpace as List <Product>).Where(i => { double w = bidprice(t, i); if (i.Fare < w) { return(false); } else { return(true); } }).ToList(); List <Product> tempDecision = new List <Product>(); //加入第一个元素 Product tempProduct = null; double temp = computeValue(t, tempDecision, tempProduct); foreach (Product p in list) { if (temp < computeValue(t, tempDecision, p)) { temp = computeValue(t, tempDecision, p); tempProduct = p; } } if (tempProduct != null) { tempDecision.Add(tempProduct); list.Remove(tempProduct); } //贪婪方法加入其他元素 for (int length = 0; length < tempDecision.Count();) { length = tempDecision.Count(); foreach (Product p in list) { double temp1 = computeValue(t, tempDecision, p); if (temp < temp1) { temp = temp1; tempDecision.Add(p); } } } #endregion #region #region 得到 Route List //List<Route> list = Data.pathList.Where(i => //{ // double w = bidprice(t, i); // if (i.TicketPrice < w) // { // return false; // } // else // { // return true; // } //}).ToList(); //List<Route> tempRoutelist = new List<Route>(); //Route tempRoute = null; //double temp = computeValue(t, tempRoutelist, tempRoute); //foreach (Route r in list) //{ // if (temp < computeValue(t, tempRoutelist, r)) // { // temp = computeValue(t, tempRoutelist, r); // tempRoute = r; // } //} //if (tempRoute != null) //{ // tempRoutelist.Add(tempRoute); // list.Remove(tempRoute); //} ////贪婪方法加入其他元素 //for (int length = 0; length < tempRoutelist.Count();) //{ // length = tempRoutelist.Count(); // foreach (Route r in list) // { // double temp1 = computeValue(t, tempRoutelist, r); // if (temp < temp1) // { // temp = temp1; // tempRoutelist.Add(r); // } // } //} //#endregion //List<Product> tempDecision = new List<Product>(); //#region 找到一个u //foreach (Route r in tempRoutelist) //{ // foreach (Product p in r) // { // if (!tempDecision.Contains(p)) { tempDecision.Add(p); } // } //} #endregion #endregion //从u生成decision Decision d = new Decision(); d.OpenProductSet.UnionWith(tempDecision); lock (_ds) { deci_a = _ds.FirstOrDefault(k => (k as Decision).Equals(d)) as IALPDecision; if (deci_a == null) { _ds.Add(d); deci_a = d; } } if (computeValue(t, tempDecision, null) < Tolerance || (t > alpha ? constraints[t].ContainsKey(deci_a) : Aggconstraints.ContainsKey(deci_a))) { return(true); } else { return(false); } }
protected override bool CG(int t, out IALPDecision deci_a) { deci_a = null; //foreach (OD i in Data.MarketInfo.ODList) //{ // List<MarketSegment> li = Data.MarketInfo.getMSbyOD(i); // List<Route> Routeli = Data.pathList.Where(x => x.StartStation == i.OriSta && x.EndStation == i.DesSta).ToList(); //} #region 得到 Route List List <Route> list = Data.RouteList.Where(i => { double w = bidprice(t, i); if (i.TicketPrice < w) { return(false); } else { return(true); } }).ToList(); List <Route> tempRoutelist = new List <Route>(); double temp = 0; Route tempRoute = null; foreach (Route r in list) { if (temp < computeValue(t, tempRoutelist, r)) { temp = computeValue(t, tempRoutelist, r); tempRoute = r; } } if (tempRoute != null) { tempRoutelist.Add(tempRoute); } list.Remove(tempRoute); foreach (Route r in list) { if (temp < computeValue(t, tempRoutelist, r)) { tempRoutelist.Add(r); } } #endregion List <Product> templist = new List <Product>(); #region 找到一组u foreach (Route r in tempRoutelist) { foreach (Product p in r) { if (!templist.Contains(p)) { templist.Add(p); } } } #endregion /* #region 找到一个u * List<Product> list = Data.ProSpace.Where(i => * { * double w = bidprice(t, i); * if (i.Fare < w) * { * return false; * } * else * { * return true; * } * }).ToList(); * List<Product> templist = new List<Product>(); * double temp = 0; * Product tempProduct = null; * foreach (Product p in list) * { * if (temp < computeValue(t, templist, p)) * { * temp = computeValue(t, templist, p); * tempProduct = p; * } * } * if (tempProduct != null) * { * templist.Add(tempProduct); * } * list.Remove(tempProduct); * foreach (Product p in list) * { * if (temp < computeValue(t, templist, p)) * { * templist.Add(p); * } * } * #endregion */ //从u生成decision Decision d = new Decision(); d.OpenProductSet.UnionWith(templist); deci_a = constraints[t].Keys.FirstOrDefault(k => (k as Decision).Equals(d)) as IALPDecision; if (deci_a == null) { _ds.Add(d); deci_a = d; } if (computeValue(t, templist, null) < Tolerance || constraints[t].ContainsKey(deci_a)) { return(true); } else { return(false); } }
public override void DoCalculate()//Calculate the Bid-Price of current time { CreateFeasibleSolution(); bool IsOptimal = true; double tempObj = 0; int tol = ObeyTime; for (int iter = 1; ; iter++) { IsOptimal = true; if (RMPModel.Solve()) { #region 判断是否终止 if (RMPModel.GetObjValue() - tempObj < threshold) { tol--; } else { tol = ObeyTime; } tempObj = RMPModel.GetObjValue(); #endregion } UpdateValues(); #region 判断是否终止 if (tol < 0) { print("--------{0}算法达到终止条件而退出--------", System.DateTime.Now.ToLongTimeString()); UpdateBidPrice(); break; } #endregion //for (int t = CurrentTime; t < Data.TimeHorizon; t++) //{ // IALPDecision deci_a = null; // bool IsSubOpt = CG(t, out deci_a); // if (!IsSubOpt) // { // AddConstraint(t, deci_a); // } // IsOptimal = IsSubOpt && IsOptimal; //} for (int iteration = CurrentTime; iteration < Data.TimeHorizon; iteration++) { int t = iteration; Task ta = factory.StartNew(() => { IALPDecision deci_a = null; bool IsSubOpt = CG(t, out deci_a); if (!IsSubOpt) { lock (RMPModel) { AddConstraint(t, deci_a); } } IsOptimal = IsSubOpt && IsOptimal; //print("#{0}第{1}个子问题已经解决!", System.DateTime.Now.ToLongTimeString(), iteration); }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); if (IsOptimal) { print("--------已经达到最优!", System.DateTime.Now.ToLongTimeString()); UpdateValues(); UpdateBidPrice(); SA(); break; } } }
protected override bool CG2(int t, out IALPDecision deci_a) { return(CG(t, out deci_a)); }
protected abstract void Add_Dis_Constraint(int t, IALPDecision a);
protected override bool CG(int t, out IALPDecision deci_a) { deci_a = null; #region 贪婪算法求解组合优化问题 List <Product> list = (Data.ProSpace as List <Product>).Where(i => { double w = bidprice(t, i); if (i.Fare < w) { return(false); } else { return(true); } }).ToList(); //优先关闭机会成本低的产品 List <Product> tempDecision = new List <Product>(); //开放产品集合 //加入第一个元素 Product tempProduct = null; double temp = computeValue(t, tempDecision, tempProduct); foreach (Product p in list) { if (temp < computeValue(t, tempDecision, p)) { temp = computeValue(t, tempDecision, p); tempProduct = p; } } if (tempProduct != null) { tempDecision.Add(tempProduct); list.Remove(tempProduct); } for (; list.Count > 0;) { temp = computeValue(t, tempDecision, null); double temp2 = temp; Product tempPro = null; foreach (Product p in list) { double temp1 = computeValue(t, tempDecision, p); if (temp2 < temp1) { temp2 = temp1; tempPro = p; } } if (temp2 > temp) { tempDecision.Add(tempPro); list.Remove(tempPro); } else { break; } } #endregion //从u生成decision Decision d = new Decision(); d.OpenProductSet.UnionWith(tempDecision); lock (_ds) { deci_a = _ds.FirstOrDefault(k => (k as Decision).Equals(d)) as IALPDecision; if (deci_a == null) { _ds.Add(d); deci_a = d; } } if (computeValue(t, tempDecision, null) < Tolerance || constraints[t].ContainsKey(deci_a)) { return(true); } else { return(false); } }
public virtual void DoCalculate()//Calculate the Bid-Price of current time { CreateFeasibleSolution(); for (; alpha - step >= 0;) { print("------{0} Attemping with [Alpha = {1}]", System.DateTime.Now.ToLongTimeString(), alpha); bool IsOptimal = true; double tempObj = 0; int tol = ObeyTime; for (int iter = 1; ; iter++) { IsOptimal = true; if (RMPModel.Solve()) { #region 判断是否终止 if (RMPModel.GetObjValue() - tempObj < Threshold) { tol--; } else { tol = ObeyTime; } tempObj = RMPModel.GetObjValue(); #endregion } UpdateValues(); #region 判断是否终止 if (tol < 0) { print("--------{0} Attemping Completed!iter:{1}", System.DateTime.Now.ToLongTimeString(), iter); print("--------{0}算法达到终止条件而退出--------", System.DateTime.Now.ToLongTimeString()); UpdateBidPrice(); break; } #endregion IALPDecision deci_a1 = null; bool IsSubOpt1 = CG1(out deci_a1); if (!IsSubOpt1) { AddConstraint1(deci_a1); } IsOptimal = IsSubOpt1 && IsOptimal; for (int t = alpha + 1; t < Data.TimeHorizon; t++) { IALPDecision deci_a = null; bool IsSubOpt = CG2(t, out deci_a); if (!IsSubOpt) { AddConstraint2(t, deci_a); } IsOptimal = IsSubOpt && IsOptimal; } if (IsOptimal) { print("--------{0} Attemping Completed!iter:{1}", System.DateTime.Now.ToLongTimeString(), iter); UpdateValues(); UpdateBidPrice(); break; } } if (TestValidation()) { print("--------已经达到最优!", System.DateTime.Now.ToLongTimeString()); break; } else { StepFoward(); } } }