示例#1
0
        private DFA <Tuple <T, T>, U> andor(DFA <T, U> other, bool and)
        {
            if (!Alphabet.SetEquals(other.Alphabet))
            {
                return(null);
            }
            var curr = new HashSet <Tuple <T, T> >(Transitions.Count * other.Transitions.Count());

            curr.Add(Tuple.Create(StartState, other.StartState));
            var result = new DFA <Tuple <T, T>, U>();

            result.addStartState(curr.First());
            while (result.Transitions.Count() < Transitions.Count * other.Transitions.Count())
            {
                var next = new HashSet <Tuple <T, T> >(Transitions.Count * other.Transitions.Count());
                foreach (var currState in curr)
                {
                    if (and ? (EndStates.Contains(currState.Item1) && other.EndStates.Contains(currState.Item2))
                        : (EndStates.Contains(currState.Item1) || other.EndStates.Contains(currState.Item2)))
                    {
                        result.addEndState(currState);
                    }
                    foreach (var terminal in Alphabet)
                    {
                        var nextState = Tuple.Create(Transitions[currState.Item1][terminal], other.Transitions[currState.Item2][terminal]);
                        next.Add(nextState);
                        result.addTransition(terminal, currState, nextState);
                    }
                }
                curr = next;
            }
            return(result);
        }
示例#2
0
        public Tuple <int, string> ProcessWord(string word)
        {
            int State        = StartState;
            int HeadPosition = 0;

            char[] Tape = word.ToCharArray();

            while (!EndStates.Contains(State))
            {
                char       currentSymbol = GetSymbol(Tape, HeadPosition);
                Transition t             = TransitionFunction(State, currentSymbol);

                Debug.WriteLine($"({State}, {currentSymbol}) -> ({t.NextState}, {t.Write ?? currentSymbol}, {t.MovementDirection}); {new String(Tape).Insert(HeadPosition, $"(q{State})")}");

                State = t.NextState;
                if (t.Write.HasValue)
                {
                    Tape[HeadPosition] = t.Write.Value;
                }
                if (t.MovementDirection == Direction.Left)
                {
                    HeadPosition--;
                }
                else
                {
                    HeadPosition++;
                }
            }
            return(new Tuple <int, string>(State, new string(Tape)));
        }
示例#3
0
    public void ContinueToNextDay()
    {
        EndStates endState = gameOverCheck.CheckEndOfDay();

        switch (endState)
        {
        case EndStates.NoFood:
            playerInfo.currState = EndStates.NoFood;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoHouse:
            playerInfo.currState = EndStates.NoHouse;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoTax:
            playerInfo.currState = EndStates.NoTax;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoMoney:
            playerInfo.currState = EndStates.NoMoney;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.TooManyStrikes:
            playerInfo.currState = EndStates.TooManyStrikes;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.KidDies:
            playerInfo.currState = EndStates.KidDies;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.DataBreach:
            playerInfo.currState = EndStates.DataBreach;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.Conspiring:
            playerInfo.currState = EndStates.Conspiring;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.FalseInfo:
            playerInfo.currState = EndStates.FalseInfo;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.None:
            //TODO: Change this
            playerInfo.ResetPlayer();
            StartCoroutine(transitionManager.TransitionScene("Day2"));
            break;
        }
    }
示例#4
0
 /// <summary>
 /// Resets a player to the start of a new game.
 /// </summary>
 public void NewPlayer()
 {
     paidFood         = false;
     paidTax          = false;
     paidHouse        = false;
     paidMedicine     = false;
     hasDatabreach    = false;
     noSellHuey       = false;
     dataBreachCount  = 0;
     currProfitForDay = 0;
     incorrectCount   = 0;
     currMoney        = 10;
     currState        = EndStates.None;
 }
示例#5
0
        public DFA <T, U> negative()
        {
            DFA <T, U> result = new DFA <T, U>();

            result.addStartState(StartState);
            result.Transitions = Transitions;
            result.States      = States;
            foreach (var state in States)
            {
                if (!EndStates.Contains(state))
                {
                    result.addEndState(state);
                }
            }
            return(result);
        }
示例#6
0
        public bool accept(U[] input)
        {
            if (!isValid())
            {
                throw new AutomatonInvalidException();
            }
            var currState = StartState;

            foreach (U u in input)
            {
                if (!Alphabet.Contains(u))
                {
                    throw new System.ArgumentException($"{u} is not part of alphabet");
                }
                currState = Transitions[currState][u];
            }

            return(EndStates.Contains(currState));
        }
示例#7
0
        public StateMachine(System.Xml.Linq.XDocument document)
        {
            BeginState = document.Root.Attribute("beginState").Value;
            string endState = document.Root.Attribute("endState").Value;

            EndStates.Add(endState);
            States = new List <State>();
            //States = (from x in document.Descendants("State")

            //           select new State() { Name = x.Attribute("name").Value, Display = x.Attribute("display").Value }).ToList();
            foreach (var x in document.Descendants("State"))
            {
                var state = new State()
                {
                    Name = x.Attribute("name").Value, Display = x.Attribute("display").Value
                };
                var isnav = x.Attribute("isNavigate");
                if (isnav != null && Boolean.TryParse(isnav.Value, out bool isNav))
                {
                    state.IsNavigate = isNav;
                    var order = x.Attribute("order");
                    if (order != null && Int32.TryParse(order.Value, out int i))
                    {
                        state.SortOrder = i;
                    }
                }
                States.Add(state);
            }

            Transitions = (from x in document.Descendants("Transition")
                           select new Transition()
            {
                Name = x.Attribute("name").Value,
                OriginState = x.Attribute("origin").Value,
                DestinationState = x.Attribute("destination").Value,
                SortOrder = Convert.ToInt32(x.Attribute("order").Value)
            }).ToList();
        }
示例#8
0
        public bool accept(U[] input)
        {
            if (!isValid())
            {
                throw new AutomatonInvalidException();
            }

            var currStates = StartStates;

            foreach (U u in input)
            {
                if (!Alphabet.Contains(u))
                {
                    throw new System.ArgumentException($"{u} is not part of alphabet");
                }
                HashSet <T> newStates = new HashSet <T>();
                foreach (T state in currStates)
                {
                    if (!Transitions.ContainsKey(state))
                    {
                        continue;
                    }
                    if (Transitions[state].ContainsKey(Epsilon))
                    {
                        newStates.UnionWith(Transitions[state][Epsilon]);
                    }
                    if (Transitions[state].ContainsKey(u))
                    {
                        newStates.UnionWith(Transitions[state][u]);
                    }
                }
                currStates = newStates;
            }

            return(EndStates.Intersect(currStates).Count() > 0);
        }
示例#9
0
 private void UpdateText(EndStates state)
 {
     TitleField.text = Titles[(int)state];
     DescField.text  = Descriptions[(int)state];
 }
示例#10
0
 public bool addEndState(T state)
 {
     return(EndStates.Add(state));
 }
示例#11
0
        public DFA <IEnumerable <T>, U> toDFA()
        {
            DFA <IEnumerable <T>, U> result = new DFA <IEnumerable <T>, U>();
            HashSet <StateSet <T> >  curr   = new HashSet <StateSet <T> >();

            // Add start state
            StateSet <T> start = new StateSet <T>();

            foreach (T startState in StartStates)
            {
                start.UnionWith(epsilonClosure(startState));
            }
            curr.Add(start);
            result.addStartState(start);

            // Add trap state
            StateSet <T> trap = new StateSet <T>();

            foreach (U terminal in Alphabet)
            {
                result.addTransition(terminal, trap, trap);
            }

            // Add transitions
            bool done = false;

            while (!done)
            {
                bool addedNew = false;
                HashSet <StateSet <T> > next = new HashSet <StateSet <T> >();
                // For each current state in set of current states.
                foreach (StateSet <T> currState in curr)
                {
                    // For each terminal in the alphabet.
                    foreach (U terminal in Alphabet)
                    {
                        // nextState will become the actual dfa to state.
                        StateSet <T> nextState = new StateSet <T>();
                        // For each ndfa state of which the dfa state is made.
                        foreach (T subState in currState)
                        {
                            // Get the epsilon closure of the sub state.
                            HashSet <T> preClosure = epsilonClosure(subState);
                            // For each state in the epsilon closure.
                            foreach (T preClosureState in preClosure)
                            {
                                // Check if exists.
                                if (!Transitions.ContainsKey(preClosureState) || !Transitions[preClosureState].ContainsKey(terminal))
                                {
                                    continue;
                                }
                                // Get all the to states for this terminal.
                                HashSet <T> follow = Transitions[preClosureState][terminal];
                                // Accumulate the epsilon closure of each followed state.
                                foreach (T followedState in follow)
                                {
                                    HashSet <T> postClosure = epsilonClosure(followedState);
                                    nextState.UnionWith(postClosure);
                                }
                            }
                        }

                        if (nextState.Count() > 0)
                        {
                            next.Add(nextState);
                            // Add transition.
                            if (result.addTransition(terminal, currState, nextState))
                            {
                                addedNew = true;
                            }
                            // Add end state
                            if (EndStates.Intersect(nextState).Count() > 0)
                            {
                                result.addEndState(nextState);
                            }
                        }
                        else
                        {
                            /*
                             * . . . . . . . . . . . . . . . .   ____________
                             * . . . . . . . . . . . . . . . . / It’s a trap! \
                             * . . . . . . . . . . . . . . . . \ _____________/
                             * __...------------._
                             * ,-'                   `-.
                             * ,-'                         `.
                             * ,'                            ,-`.
                             * ;                              `-' `.
                             * ;                                 .-. \
                             * ;                           .-.    `-'  \
                             * ;                            `-'          \
                             * ;                                          `.
                             * ;                                           :
                             * ;                                            |
                             * ;                                             ;
                             * ;                            ___              ;
                             * ;                        ,-;-','.`.__          |
                             * _..;                      ,-' ;`,'.`,'.--`.        |
                             * ///;           ,-'   `. ,-'   ;` ;`,','_.--=:      /
                             |'':          ,'        :     ;` ;,;,,-'_.-._`.   ,'
                             * '  :         ;_.-.      `.    :' ;;;'.ee.    \|  /
                             \.'    _..-'/8o. `.     :    :! ' ':8888)   || /
                             ||`-''    \\88o\ :     :    :! :  :`""'    ;;/
                             ||         \"88o\;     `.    \ `. `.      ;,'
                             ||/)   ___    `."'/(--.._ `.    `.`.  `-..-' ;--.
                             \(.="""""==.. `'-'     `.|      `-`-..__.-' `. `.
                             |          `"==.__      )                    )  ;
                             |   ||           `"=== '                   .'  .'
                             | /\,,||||  | |           \                .'   .'
                             | |||'|' |'|'           \|             .'   _.' \
                             | |\' |  |           || ||           .'    .'    \
                             | ' | \ ' |'  .   ``-- `| ||         .'    .'       \
                             | '  |  ' |  .    ``-.._ |  ;    .'    .'          `.
                             | _.--,;`.       .  --  ...._,'   .'    .'              `.__
                             | ,'  ,';   `.     .   --..__..--'.'    .'                __/_\
                             | ,'   ; ;     |    .   --..__.._.'     .'                ,'     `.
                             | /    ; :     ;     .    -.. _.'     _.'                 /         `
                             | /     :  `-._ |    .    _.--'     _.'                   |
                             | /       `.    `--....--''       _.'                      |
                             | `._              _..-'                         |
                             | `-..____...-''                              |
                             |
                             |
                             */
                            result.addTransition(terminal, currState, trap);
                        }
                    }
                }
                curr = next;
                done = !addedNew;
            }

            return(result);
        }
示例#12
0
 void Start()
 {
     instance = this;
 }
示例#13
0
        public void RunTraining()
        {
            QMethod.Validate(this);

            /*
             * For each episode: Select random initial state
             * Do while not reach goal state
             *  Select one among all possible actions for the current state
             *  Using this possible action, consider to go to the next state
             *  Get maximum Q value of this next state based on all possible actions
             *  Set the next state as the current state
             */

            // For each episode
            var  rand = new Random();
            long maxloopEventCount = 0;

            // Train episodes
            for (long i = 0; i < Episodes; i++)
            {
                long maxloop = 0;
                // Select random initial state
                int     stateIndex = rand.Next(States.Count);
                QState  state      = States[stateIndex];
                QAction action     = null;
                do
                {
                    if (++maxloop > MaxExploreStepsWithinOneEpisode)
                    {
                        if (ShowWarning)
                        {
                            string msg = string.Format(
                                "{0} !! MAXLOOP state: {1} action: {2}, {3} endstate is to difficult to reach?",
                                ++maxloopEventCount, state, action, "maybe your path setup is wrong or the ");
                            QMethod.Log(msg);
                        }

                        break;
                    }

                    // no actions, skip this state
                    if (state.Actions.Count == 0)
                    {
                        break;
                    }

                    // Selection strategy is random based on probability
                    int index = rand.Next(state.Actions.Count);
                    action = state.Actions[index];

                    // Using this possible action, consider to go to the next state
                    // Pick random Action outcome
                    QActionResult nextStateResult = action.PickActionByProbability();
                    string        nextStateName   = nextStateResult.StateName;

                    double q    = nextStateResult.QEstimated;
                    double r    = nextStateResult.Reward;
                    double maxQ = MaxQ(nextStateName);

                    // Q(s,a)= Q(s,a) + alpha * (R(s,a) + gamma * Max(next state, all actions) - Q(s,a))
                    double value = q + Alpha * (r + Gamma * maxQ - q); // q-learning
                    nextStateResult.QValue = value;                    // update

                    // is end state go to next episode
                    if (EndStates.Contains(nextStateResult.StateName))
                    {
                        break;
                    }

                    // Set the next state as the current state
                    state = StateLookup[nextStateResult.StateName];
                } while (true);
            }
        }
示例#14
0
    public void ContinueToNextDay()
    {
        EndStates endState = gameOverCheck.CheckEndOfDay();

        if (playerInfo.dataBreachCount >= 2)
        {
            endState = EndStates.DataBreach;
        }
        switch (endState)
        {
        case EndStates.NoFood:
            playerInfo.currState = EndStates.NoFood;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoHouse:
            playerInfo.currState = EndStates.NoHouse;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoTax:
            playerInfo.currState = EndStates.NoTax;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.NoMoney:
            playerInfo.currState = EndStates.NoMoney;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.TooManyStrikes:
            playerInfo.currState = EndStates.TooManyStrikes;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.KidDies:
            playerInfo.currState = EndStates.KidDies;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.DataBreach:
            playerInfo.currState = EndStates.DataBreach;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.Conspiring:
            playerInfo.currState = EndStates.Conspiring;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.FalseInfo:
            playerInfo.currState = EndStates.FalseInfo;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.Dabbnapping:
            playerInfo.currState = EndStates.Dabbnapping;
            StartCoroutine(transitionManager.TransitionScene("BadEnd"));
            break;

        case EndStates.None:
            //TODO: Change this
            playerInfo.ResetPlayer();
            switch (gameButton.CurrDay)
            {
            case 1:
                StartCoroutine(transitionManager.TransitionScene("Day2"));
                break;

            case 2:
                StartCoroutine(transitionManager.TransitionScene("Day3"));
                break;

            case 3:
                StartCoroutine(transitionManager.TransitionScene("Day4"));
                break;

            case 4:
                StartCoroutine(transitionManager.TransitionScene("Day5"));
                break;

            case 5:
                StartCoroutine(transitionManager.TransitionScene("Day6"));
                break;

            case 6:
                StartCoroutine(transitionManager.TransitionScene("Day7"));
                break;

            case 7:
                StartCoroutine(transitionManager.TransitionScene("End"));
                break;

            default:
                StartCoroutine(transitionManager.TransitionScene("MainMenu"));
                break;
            }

            break;
        }
    }