Esempio n. 1
0
        private void UpdateRequirementMaps(Sequence <CompoundTerm> actions, IState iState)
        {
            Set <string> yesStrings = new Set <string>();

            foreach (Action a in actions)
            {
                System.Console.WriteLine(iState.GetHashCode() + a.ToString());
                foreach (string s in this.modelProgram.GetEnablingConditionDescriptions(iState, a, false))
                {
                    yesStrings = yesStrings.Add(s);
                    if (!requirementProperties.Contains(s))
                    {
                        requirementProperties  = requirementProperties.AddLast(s);
                        requireEnabledStateMap = requireEnabledStateMap.Add(s, Set <int> .EmptySet);
                    }
                    requireEnabledStateMap = requireEnabledStateMap.Override(s, requireEnabledStateMap[s].Add(iState.GetHashCode()));
                }
            }
            bdt = bdt.addState(yesStrings, iState.GetHashCode());
            if (i == 0)
            {
                if (i < requirementProperties.Count)
                {
                    string s1 = requirementProperties[i++];
                    bdt = bdt.Refine(s1, requireEnabledStateMap[s1]);
                    bdt.PrintTree(0);
                }
            }
        }
Esempio n. 2
0
 private void UpdateRequirementMaps(Sequence<CompoundTerm> actions, IState iState)
 {
     Set<string> yesStrings = new Set<string>();
     foreach (Action a in actions)
     {
         System.Console.WriteLine(iState.GetHashCode() + a.ToString());
         foreach (string s in this.modelProgram.GetEnablingConditionDescriptions(iState, a, false))
         {
             yesStrings = yesStrings.Add(s);
             if (!requirementProperties.Contains(s))
             {
                 requirementProperties = requirementProperties.AddLast(s);
                 requireEnabledStateMap = requireEnabledStateMap.Add(s, Set<int>.EmptySet);
             }
             requireEnabledStateMap = requireEnabledStateMap.Override(s, requireEnabledStateMap[s].Add(iState.GetHashCode()));
         }
     }
     bdt = bdt.addState(yesStrings, iState.GetHashCode());
     if (i == 0)
     {
         if (i < requirementProperties.Count)
         {
             string s1 = requirementProperties[i++];
             bdt = bdt.Refine(s1, requireEnabledStateMap[s1]);
             bdt.PrintTree(0);
         }
     }            
 }
Esempio n. 3
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);
        }
Esempio n. 4
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;
 }