Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
0
        public List <StateActive> RemoveState(IALPState other)
        {
            List <StateActive> list = this.Where(i => !i.S.Within(other)).ToList();

            foreach (StateActive sa in list)
            {
                Remove(sa);
            }
            return(list);
        }
Ejemplo n.º 4
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("子问题无解");
            }
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        public bool Within(IALPState other)
        {
            State temp = other as State;

            if (temp == null)
            {
                return(false);
            }
            foreach (IALPResource r in this.Keys)
            {
                if (!(temp.Keys.Contains(r) && temp[r] >= this[r]))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 7
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();
 }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 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;
                }
            }
        }