Example #1
0
        protected virtual void CreateFeasibleSolution()
        {
            print("--------{0}开始生成可行解--------", System.DateTime.Now.ToLongTimeString());
            IMDPDecision d = (Data.DS as IALPDecisionSpace).CloseAllDecision();

            for (int t = CurrentTime; t < Data.TimeHorizon; t++)
            {
                int       iteration = t;
                IALPState tempstate = (Data.InitialState as IALPState);
                Task      ta        = factory.StartNew(() =>
                {
                    StateActive temp = new StateActive()
                    {
                        T = iteration, S = tempstate, D = d
                    };
                    if (curSAS.FirstOrDefault(i => i.Equals(temp)) == null)
                    {
                        tempSA.Push(temp);
                        lock (RMPModel)
                        {
                            temp.Var = AddCol(temp.T, temp.S, temp.D);
                        }
                    }
                }, cts.Token);
                tasks.Add(ta);
            }
            ;
            Task.WaitAll(tasks.ToArray());
            tasks.Clear();
            curSAS.UnionWith(tempSA);
            tempSA.Clear();
            print("--------{0}生成可行解完毕--------", System.DateTime.Now.ToLongTimeString());
        }
Example #2
0
        private bool CG(int t, out IALPState temp_s, out IMDPDecision deci_a)
        {
            temp_s = null;
            deci_a = null;
            double reduced_cost = 0;

            foreach (IALPState s in Data.SS)
            {
                foreach (IMDPDecision a in Data.GenDecisionSpace(s))
                {
                    double temp = Data.Rt(t, a) - Data.RS.Sum(i => DualValue1[t][Data.RS.IndexOf(i)] * s[i]) - DualValue2[t];

                    if (t < Data.TimeHorizon - 1)
                    {
                        temp -= Data.RS.Sum(i => DualValue1[t + 1][Data.RS.IndexOf(i)] * (Data.Qti(t, i, a) - s[i]));
                    }

                    if (temp >= Tolerance && reduced_cost < temp)
                    {
                        reduced_cost = temp;
                        temp_s       = s;
                        deci_a       = a;
                    }
                }
            }
            System.Console.WriteLine("CG({0})求解结束\t目标值为:{1}", t, reduced_cost);
            if (reduced_cost > 0)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #3
0
        private bool RCG(int t, out IALPState temp_s, out IMDPDecision deci_a)
        {
            temp_s = null;
            deci_a = null;

            //System.Console.WriteLine("--------开始生成问题:{0}-----", System.DateTime.Now.Millisecond);
            #region 创建目标函数
            SubModel.Remove(SubModel.GetObjective());
            SubModel.AddMaximize(subObject[1][t]);
            #endregion

            foreach (IALPResource re in Data.RS)
            {
                constraint_sub2[Data.RS.IndexOf(re)].SetBounds(0, (Data.InitialState as IALPState)[re]);
            }

            //System.Console.WriteLine("--------生成问题结束:{0}-----", System.DateTime.Now.Millisecond);
            if (SubModel.Solve())
            {
                //System.Console.WriteLine("--------求解问题结束:{0}-----", System.DateTime.Now.Millisecond);
                //System.Console.WriteLine("RCG({0})求解结束\t目标值为:{1}", t, SubModel.ObjValue<0.001?0: SubModel.ObjValue);
                if (SubModel.ObjValue <= Tolerance)
                {
                    return(true);
                }
                else
                {
                    //print("{0}问题违反程度{1}", t, (SubModel.ObjValue - Tolerance).ToString());
                    #region  用h,r生成一个State
                    for (int _h = 0; _h < h.Count(); _h++)
                    {
                        double temp_h = SubModel.GetValue(h[_h]);
                        if (temp_h == 1)
                        {
                            deci_a = Data.DS[_h];
                            break;
                        }
                    }

                    Dictionary <IALPResource, int> dic = new Dictionary <IALPResource, int>();
                    for (int _r = 0; _r < r.Count(); _r++)
                    {
                        dic.Add(Data.RS[_r], (int)SubModel.GetValue(r[_r]));
                    }
                    temp_s = Data.CreateOrFind(dic);
                    #endregion
                    return(false);
                }
            }
            else
            {
                throw new System.Exception("子问题无解");
            }
        }
Example #4
0
        protected virtual INumVar AddCol(int t, IALPState s, IMDPDecision a)//Add a column into RMP model
        {
            Column col = RMPModel.Column(cost, Data.Rt(t, a));

            foreach (IALPResource re in Data.RS)
            {
                col = col.And(RMPModel.Column(constraint1[t][Data.RS.IndexOf(re)], (s as IALPState)[re]));
                if (t < Data.TimeHorizon - 1)
                {
                    col = col.And(RMPModel.Column(constraint1[t + 1][Data.RS.IndexOf(re)], (Data.Qti(t, re, a)) - (s as IALPState)[re]));
                }
            }
            col = col.And(RMPModel.Column(constraint2[t], 1));
            return(RMPModel.NumVar(col, 0, double.MaxValue, NumVarType.Float));
        }
Example #5
0
 private void SolveSubProblem()
 {
     for (int t = CurrentTime; t < Data.TimeHorizon; t++)
     {
         IALPState    temp_s   = null;
         IMDPDecision deci_a   = null;
         bool         IsSubOpt = RCG(t, out temp_s, out deci_a);
         if (!IsSubOpt)
         {
             int          iteration = t;
             IALPState    tempstate = temp_s;
             IMDPDecision tempdeci  = deci_a;
             Task         ta        = factory.StartNew(() =>
             {
                 StateActive temp = new StateActive()
                 {
                     T = iteration, S = tempstate, D = tempdeci
                 };
                 if (curSAS.FirstOrDefault(i => i.Equals(temp)) == null)
                 {
                     tempSA.Push(temp);
                     lock (RMPModel)
                     {
                         temp.Var = AddCol(temp.T, temp.S, temp.D);
                     }
                 }
             }, cts.Token);
             tasks.Add(ta);
             //StateActive tempSA = new StateActive() { T = t, S = temp_s, D = deci_a };
             //if (curSAS.Add(tempSA))
             //{
             //    tempSA.Var = AddCol(t, temp_s, deci_a);
             //}
         }
         IsOptimal = IsSubOpt && IsOptimal;
         //System.Console.WriteLine("#{0}第{1}个子问题已经解决!", System.DateTime.Now.ToLongTimeString(), t);
     }
     Task.WaitAll(tasks.ToArray());
     tasks.Clear();
     curSAS.UnionWith(tempSA);
     tempSA.Clear();
 }
Example #6
0
 public double Prob(int time, IMDPState s1, IMDPState s2, IMDPDecision a)
 {
     if (s1.Equals(s2))
     {
         return(1 - suppRoute((a as Decision).OpenProductSet).Sum(p => Ro(time) * P(time, p, a as Decision)));
     }
     else
     {
         //Find a route via which s1 transits to s2.
         Route r = suppRoute((a as Decision).OpenProductSet).
                   FirstOrDefault(i => (GenStateSpace(s1, a) as StateSpace).MinusOneUnit(s1, i).Equals(s2));
         if (r != null)
         {
             return(Ro(time) * P(time, r, a as Decision));
         }
         else
         {
             return(0);
         }
     }
 }
Example #7
0
        private bool AddCol(int t, IALPState s, IMDPDecision a)//Add a column into RMP model
        {
            Column col = RMPModel.Column(cost, Data.Rt(t, a));

            foreach (IALPResource re in Data.RS)
            {
                col = col.And(RMPModel.Column(constraint1[t][Data.RS.IndexOf(re)], (s as IALPState)[re]));
                if (t < Data.TimeHorizon - 1)
                {
                    col = col.And(RMPModel.Column(constraint1[t + 1][Data.RS.IndexOf(re)], (Data.Qti(t, re, a)) - (s as IALPState)[re]));
                }
            }
            col = col.And(RMPModel.Column(constraint2[t], 1));
            INumVar var = RMPModel.NumVar(col, 0, double.MaxValue, NumVarType.Float);

            sas.Add(new StateActive()
            {
                S = s, D = a, Var = var
            });
            return(true);
        }
Example #8
0
        public IMDPStateSpace GenStateSpace(IMDPState s, IMDPDecision a)
        {
            //目前状态减去开放产品集中的产品
            IMDPStateSpace subss = new StateSpace();

            if ((s as State).CanSupportDecision(a as Decision))
            {
                foreach (Product p in (a as Decision).OpenProductSet)
                {
                    IMDPState _s = (_ss as StateSpace).MinusOneUnit(s, p);
                    if (!subss.Contains(_s))
                    {
                        subss.Add(_s);
                    }
                }
                //再加入本身
                subss.Add(s);
            }

            return(subss);
        }
Example #9
0
        public void DoCalculate()//Calculate the Bid-Price of current time
        {
            print("--------{0}开始求解--------", System.DateTime.Now.ToLongTimeString());
            IsOptimal = false;
            CreateFeasibleSolution();


            //检验是否有违背State的变量
            foreach (StateActive sa in sas.RemoveState(Data.InitialState as IALPState))
            {
                RMPModel.Delete(sa.Var);
            }

            double tempObj = 0;
            int    tol     = ObeyTime;

            for (int iter = 1; ; iter++)
            {
                print("--------{1}开始进行第{0}次循环--------", iter, System.DateTime.Now.ToLongTimeString());
                if (RMPModel.Solve())
                {
                    if (RMPModel.GetObjValue() - tempObj < threshold)
                    {
                        tol--;
                    }
                    else
                    {
                        tol = ObeyTime;
                    }
                    tempObj = RMPModel.GetObjValue();
                }
                IsOptimal = true;
                print("--------{1}目标值:{0}--------", RMPModel.ObjValue, System.DateTime.Now.ToLongTimeString());

                print("--------{0}更新参数开始--------", System.DateTime.Now.ToLongTimeString());
                UpdateDualValues();

                print("--------{0}更新参数结束--------", System.DateTime.Now.ToLongTimeString());
                if (tol < 0)
                {
                    print("--------{0}算法达到终止条件而退出--------", System.DateTime.Now.ToLongTimeString());
                    break;
                }

                for (int t = CurrentTime; t < Data.TimeHorizon; t++)
                {
                    int  iteration = t;
                    Task ta        = factory.StartNew(() =>
                    {
                        //print("Problem {0} on thread {1}   ", iteration, Thread.CurrentThread.ManagedThreadId);
                        bool temp;
                        IALPState temp_s    = null;
                        IMDPDecision deci_a = null;
                        lock (SubModel)
                        {
                            GenSubModelObj(iteration);
                            temp = RCG(iteration, out temp_s, out deci_a);
                        }
                        lock (RMPModel)
                        {
                            lock (sas)
                            {
                                if (!temp)
                                {
                                    AddCol(iteration, temp_s, deci_a);
                                }
                            }
                        }
                        IsOptimal = temp && IsOptimal;
                        //print("#{0}第{1}个子问题已经解决!", System.DateTime.Now.ToLongTimeString(), iteration);
                    }, cts.Token);
                    tasks.Add(ta);
                }
                Task.WaitAll(tasks.ToArray());
                tasks.Clear();



                if (IsOptimal)
                {
                    print("#已经达到最优!");
                    UpdateDualValues();
                    break;
                }
            }
        }
Example #10
0
 public double Qti(int t, IALPResource re, IMDPDecision a)
 {
     return(Ro(t) * suppRoute((a as Decision).OpenProductSet).
            Sum(i => P(t, i, a as Decision) * aik(re, i)));
 }
Example #11
0
 public double Rt(int t, IMDPDecision a)
 {
     return(Ro(t) * suppRoute((a as Decision).OpenProductSet).
            Sum(i => P(t, i, a as Decision) * f(i)));
 }
Example #12
0
 public double Reward(int t, IMDPState s, IMDPDecision a)
 {
     return(suppRoute((a as Decision).OpenProductSet).
            Where(i => (s as State).CanSupportRoute(i)).
            Sum(i => Ro(t) * P(t, i, a as Decision) * f(i)));
 }