예제 #1
0
        public override void StepFoward()
        {
            alpha -= step;
            //Delete Agg constraint(s)
            foreach (var a in Aggconstraints)
            {
                RMPModel.Remove(a.Value);
            }
            List <IALPDecision> list = Aggconstraints.Keys.ToList();

            Aggconstraints.Clear();

            //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);

            foreach (IALPDecision de in list)
            {
                AddConstraint1(de);
            }
            Decision d = _ds[0] as Decision;

            for (int t = alpha + 1; t <= alpha + step; t++)
            {
                AddConstraint2(t, d);
            }
        }
예제 #2
0
        public override void DoCalculate()//Calculate the Bid-Price of current time
        {
            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++)
            {
                //print("------{0} Starting iteration:{1}", System.DateTime.Now.ToLongTimeString(), iter);

                if (RMPModel.Solve())
                {
                    #region 判断是否终止
                    if (RMPModel.GetObjValue() - tempObj < Threshold)
                    {
                        tol--;
                    }
                    else
                    {
                        tol = ObeyTime;
                    }
                    tempObj = RMPModel.GetObjValue();
                    #endregion
                }
                print("------{0} Starting iteration:{1},Current Obj Value:{2}", System.DateTime.Now.ToLongTimeString(), iter, tempObj);
                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

                IsOptimal = SolveSubProblem();

                if (IsOptimal)
                {
                    print("--------{0} Attemping Completed!iter:{1}", System.DateTime.Now.ToLongTimeString(), iter);
                    UpdateValues();
                    UpdateBidPrice();
                    break;
                }
            }
            SendEvent(new IterationCompletedEventArgs()
            {
                Alpha         = alpha,
                BidPrice      = this.BidPrice,
                ObjValue      = tempObj,
                TurnningPoint = findturnningpoint()
            });
        }
예제 #3
0
        //protected override bool CG(int t, out IALPDecision deci_a)
        //{
        //    deci_a = null;
        //    CP cp = new CP();
        //    INumExpr cost = cp.NumExpr();

        //    IIntVar[] u = cp.IntVarArray(Data.ProSpace.Count, 0, 1);
        //    IIntVar[] y = cp.IntVarArray(Data.pathList.Count, 0, 1);
        //    IIntVar[] z = cp.IntVarArray(Data.RS.Count, 0, 1);

        //    #region 生成目标函数
        //    foreach (MarketSegment ms in Data.MarketInfo)
        //    {
        //        INumExpr a = cp.NumExpr();
        //        INumExpr b = cp.NumExpr();
        //        foreach (Route h in ms.ConsiderationDic.Keys)
        //        {
        //            #region  计算wh
        //            double w = 0;
        //            w += h.TicketPrice;
        //            foreach (Resource r in Data.RS)
        //            {
        //                if (h.Find(i => i.Contains(r)) != null)
        //                {
        //                    for (int k = t + 1; k < Data.TimeHorizon; k++)
        //                    {
        //                        w -= V[k][Data.RS.IndexOf(r)];
        //                    }
        //                }
        //            }
        //            #endregion

        //            a = cp.Sum(a, cp.Prod(w * ms.ConsiderationDic[h], y[Data.pathList.IndexOf(h)]));
        //            b = cp.Sum(b, cp.Prod(ms.ConsiderationDic[h], y[Data.pathList.IndexOf(h)]));
        //        }
        //        cost = cp.Sum(cost, cp.Prod(cp.Quot(a, cp.Sum(b, ms.Retreat)), Data.MarketInfo.Ro(t) * ms.Lamada(t)));
        //    }
        //    foreach (Resource r in Data.RS)
        //    {
        //        cost = cp.Sum(cost, cp.Prod(-V[t][Data.RS.IndexOf(r)], z[Data.RS.IndexOf(r)]));
        //    }
        //    cost = cp.Sum(cost, -Sita[t]);
        //    #endregion

        //    cp.Add(cp.Maximize(cost));

        //    #region y-u约束
        //    foreach (Route r in Data.pathList)
        //    {
        //        INumExpr con1 = cp.NumExpr();
        //        foreach (Product p in r)
        //        {
        //            con1 = cp.Sum(con1, u[Data.ProSpace.IndexOf(p)]);
        //            INumExpr con2 = cp.NumExpr();
        //            con2 = cp.Sum(cp.Prod(-1, u[Data.ProSpace.IndexOf(p)]), y[Data.pathList.IndexOf(r)]);
        //            cp.AddLe(con2, 0);
        //        }
        //        con1 = cp.Sum(con1, cp.Prod(-1, y[Data.pathList.IndexOf(r)]));
        //        con1 = cp.Sum(con1, 1 - r.Count);
        //        cp.AddLe(con1, 0);
        //    }
        //    #endregion

        //    #region z-u约束
        //    foreach (Resource r in Data.RS)
        //    {
        //        INumExpr con3 = cp.NumExpr();
        //        foreach (Product p in Data.ProSpace)
        //        {
        //            if (p.Contains(r))
        //            {
        //                con3 = cp.Sum(con3, u[Data.ProSpace.IndexOf(p)]);
        //                INumExpr con4 = cp.NumExpr();
        //                con4 = cp.Sum(z[Data.RS.IndexOf(r)], cp.Prod(-1, u[Data.ProSpace.IndexOf(p)]));
        //                cp.AddGe(con4, 0);
        //            }
        //        }
        //        con3 = cp.Sum(con3, cp.Prod(-1, z[Data.RS.IndexOf(r)]));
        //        cp.AddGe(con3, 0);
        //    }
        //    #endregion

        //    cp.SetOut(null);

        //    cp.Solve();
        //    if (cp.ObjValue < Tolerance)
        //    {
        //        cp.End();
        //        return true;
        //    }
        //    else
        //    {
        //        double[] temp = new double[Data.ProSpace.Count];
        //        cp.GetValues(u, temp);
        //        //从u生成decision
        //        Decision d = new Decision();
        //        for (int i = 0; i < Data.ProSpace.Count; i++)
        //        {
        //            if (temp[i] == 1)
        //            {
        //                d.OpenProductSet.Add(Data.ProSpace[i]);
        //            }
        //        }
        //        deci_a = _ds.FirstOrDefault(k => (k as Decision).Equals(d)) as IALPDecision;
        //        if (deci_a == null) { _ds.Add(d); deci_a = d; }
        //        cp.End();
        //        return false;
        //    }
        //}
        protected override void SA()
        {
            for (int t = CurrentTime; t < Data.TimeHorizon; t++)
            {
                int    i = 0;
                var    b = RMPModel.GetSlacks(constraints[t].Values.ToArray());
                string s = "";
                foreach (var c in constraints[t])
                {
                    if (RMPModel.GetSlack(c.Value) == 0)
                    {
                        s += _ds.IndexOf(c.Key) + ",";
                    }
                }
                foreach (var a in b)
                {
                    if (a == 0)
                    {
                        i++;
                    }
                }
                #region w小于0的路径
                string ss = "";
                foreach (Route h in Data.RouteList)
                {
                    double temp = h.TicketPrice;
                    foreach (Resource r in Data.RS)
                    {
                        if (h.Exists(k => k.Contains(r)))
                        {
                            temp -= V[t][Data.RS.IndexOf(r)];
                        }
                    }
                    if (temp > 0)
                    {
                        ss += Data.RouteList.IndexOf(h) + ",";
                    }
                }
                #endregion
                print("Time:{0},Number of Bounded Constraints:{1}/{2} | {3} | {4}", t, i, b.Length, s, ss);
            }
        }
예제 #4
0
        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;
                }
            }
        }