示例#1
0
        private void doValueIteration(BinaryDecisionTree bdt)
        {
            List <double>     maxV        = bdt.ReturnValue(true);
            List <double>     minV        = bdt.ReturnValue(false);
            List <Set <int> > abstractMap = bdt.ReturnLeaves();
            double            tmp;
            int maxvcount = maxV.Count;


            double diff    = 1.0;
            double epsilon = 0.1;

            while (diff > epsilon)
            {
                diff = 0.0;
                for (int k = 0; k < maxvcount; k++)
                {
                    tmp     = doLocalValueIteration(k, abstractMap, maxV, true);
                    diff    = Math.Max(diff, Math.Abs(tmp - maxV[k]));
                    maxV[k] = tmp;
                    tmp     = doLocalValueIteration(k, abstractMap, minV, false);
                    diff    = Math.Max(diff, Math.Abs(tmp - minV[k]));
                    minV[k] = tmp;
                }
            }

            for (int k = 0; k < maxvcount; k++)
            {
                System.Console.WriteLine("maxv " + maxV[k] + " minv " + minV[k]);
            }
            // Need to update the maxValue as well as minValue in the tree
            bdt = bdt.UpdateMaxMin(maxV, minV, abstractMap);
        }
示例#2
0
        private CompoundTerm ChooseAction(Sequence <Action> actions, IState iState)
        {
            TransitionProperties tp;
            int               targetId    = -1;
            List <double>     MaxV        = bdt.ReturnValue(true);
            List <double>     MinV        = bdt.ReturnValue(false);
            List <Set <int> > abstractMap = bdt.ReturnLeaves();
            Sequence <Pair <int, Action> > cumulActSum = Sequence <Pair <int, Action> > .EmptySequence;
            double epsilon = 0.1;
            Dictionary <int, int> sumTarget = new Dictionary <int, int>();
            Action maxAct      = null;
            int    targetAbsId = -1;
            int    sum         = 0;

            UpdateRequirementMaps(actions, iState);
            Set <Action> newStateActs = new Set <Action>();
            Set <Action> oldActs      = new Set <Action>(actions.Head);

            foreach (Action a in actions)
            {
                int tState = this.modelProgram.GetTargetState(iState, a, null, out tp).GetHashCode();
                targetAbsId = findAbstractId(tState, abstractMap);
                if (targetAbsId == -1)
                {
                    newStateActs = newStateActs.Add(a);
                }
                else
                {
                    sum = sum + (int)(MaxV[targetAbsId] * Math.Pow(10.0, 9.0));
                    Pair <int, Action> np = new Pair <int, Action>(sum, a);
                    sumTarget.Add(sum, targetAbsId);
                    cumulActSum = cumulActSum.AddLast(np);
                }
            }
            if (!newStateActs.IsEmpty)
            {
                maxAct = newStateActs.Choose();
                System.Console.WriteLine("new action in new state " + maxAct.ToString());
                return(maxAct);
            }
            else
            {
                Random rndNumbers = new Random();
                int    rndNumber  = rndNumbers.Next(sum);
                System.Console.WriteLine(sum + " " + rndNumber);
                foreach (Pair <int, Action> np in cumulActSum)
                {
                    System.Console.WriteLine(np.First + " " + np.Second.ToString());
                    if (rndNumber <= np.First)
                    {
                        maxAct   = np.Second;
                        targetId = sumTarget[np.First];
                        break;
                    }
                    targetId = sumTarget[np.First];
                    maxAct   = np.Second;
                }
                System.Console.WriteLine("old action in old state " + maxAct.ToString());
            }
            // Adaptive Refinement
            if (MaxV[targetId] - MinV[targetId] > epsilon)
            {
                if (i < requirementProperties.Count)
                {
                    string s1 = requirementProperties[i++];
                    bdt = bdt.Refine(s1, requireEnabledStateMap[s1]);
                    bdt.PrintTree(0);
                }
            }
            return(maxAct);
        }
示例#3
0
 private void doValueIteration(BinaryDecisionTree bdt)
 {
     List<double> maxV = bdt.ReturnValue(true);
     List<double> minV = bdt.ReturnValue(false);
     List<Set<int>> abstractMap = bdt.ReturnLeaves();
     double tmp; 
     int maxvcount = maxV.Count;
     
             
     double diff = 1.0;
     double epsilon = 0.1;
     while (diff > epsilon)
     {
         diff = 0.0;
         for (int k = 0; k < maxvcount;k++)
         {
             tmp = doLocalValueIteration(k, abstractMap, maxV, true);
             diff = Math.Max(diff, Math.Abs(tmp - maxV[k]));
             maxV[k] = tmp;
             tmp = doLocalValueIteration(k, abstractMap, minV, false);
             diff = Math.Max(diff, Math.Abs(tmp - minV[k]));
             minV[k] = tmp;
         }
     }        
     
     for (int k = 0; k < maxvcount; k++)
     {
         System.Console.WriteLine("maxv "+ maxV[k]+ " minv "+ minV[k]);
     }
     // Need to update the maxValue as well as minValue in the tree
     bdt = bdt.UpdateMaxMin(maxV, minV, abstractMap);
 }