Beispiel #1
0
    private static CogwheelEqsysFact addEqsysFact()
    {
        int eqsysFactId = GameState.Facts.Count;

        int[]             cogIds = GameState.Facts.FindAll(fact => fact.GetType().Equals(typeof(CogwheelFact))).Select(fact => fact.Id).ToArray();
        CogwheelEqsysFact eqsys  = new CogwheelEqsysFact(eqsysFactId, cogIds);

        GameState.Facts.Insert(eqsysFactId, eqsys);
        return(eqsys);
    }
Beispiel #2
0
 public override bool Equals(System.Object obj)
 {
     //Check for null and compare run-time types.
     if ((obj == null) || !this.GetType().Equals(obj.GetType()))
     {
         return(false);
     }
     else
     {
         CogwheelEqsysFact p = (CogwheelEqsysFact)obj;
         return(this.CogwheelIds.Equals(p.CogwheelIds));
     }
 }
Beispiel #3
0
    public static Dictionary <Fact, float> knowledgeBasedSimulation(Dictionary <Fact, float> knownAvMap)
    {
        List <SimplifiedFact> sFactList = GameState.LastKBSimulationResult.Item2;

        //Only send server-request if global fact-list has changed
        if (!GameState.LastKBSimulationResult.Item1.SequenceEqual(GameState.Facts))
        {
            if (GameState.ServerRunning)
            {
                CogwheelEqsysFact     eqsysFact = addEqsysFact();
                List <SimplifiedFact> response  = listSimplifiedFacts();

                if (response != null)
                {
                    GameState.LastKBSimulationResult = new Tuple <List <Fact>, List <SimplifiedFact> >(new List <Fact>(GameState.Facts), response);
                    sFactList = response;
                }
                else
                {
                    Debug.Log("KnowledgeBasedSimulation: /fact/list reponse is null. Using LastKBSimuationResult.");
                    return(null);
                }
            }
            else
            {
                Debug.LogWarning("KnowledgeBasedSimulation: Cannot send server-request, because FrameIT-Server is not running.");
                return(null);
            }
        }

        if (sFactList != null)
        {
            List <SEqsysFact> sEqsysFacts = sFactList.FindAll(sFact => sFact.GetType().Equals(typeof(SEqsysFact)))
                                            .Select(sFact => (SEqsysFact)sFact)
                                            .ToList();
            SEqsysFact eqsysFactForSimulation = null;

            if (sEqsysFacts.Count == 0)
            {
                Debug.Log("KnowledgeBasedSimulation: sFactList contains no SEqsysFact.");
                return(null);
            }
            else if (sEqsysFacts.Count > 1)
            {
                Debug.Log("KnowledgeBasedSimulation: sFactList contains more than one SEqsysFact. Using newest one for Simulation.");
                eqsysFactForSimulation = sEqsysFacts.ElementAt(sEqsysFacts.Count - 1);
            }
            else
            {
                eqsysFactForSimulation = sEqsysFacts.ElementAt(0);
            }

            if (eqsysFactForSimulation != null)
            {
                //Prepare Data (parse equations) for gls-solver
                Tuple <List <List <double> >, List <double>, List <MMTTerm> > glsTuple = eqsysFactForSimulation.parseEquationSystem();

                if (glsTuple == null)
                {
                    Debug.Log("KnowledgeBasedSimulation: Sth. went wrong while parsing the EquationSystem.");
                    return(null);
                }
                else
                {
                    List <List <double> > AData     = glsTuple.Item1;
                    List <double>         bData     = glsTuple.Item2;
                    List <MMTTerm>        variables = glsTuple.Item3;

                    int numberOfVariables = bData.Count;
                    addKnownEqsysValues(AData, bData, variables, knownAvMap);

                    Matrix <double> A = Matrix <double> .Build.DenseOfRows(AData);

                    Vector <double> b = Vector <double> .Build.DenseOfEnumerable(bData);

                    string[] equations = equationsToString(AData, bData);

                    /****DETERMINE HOW MANY SOLUTIONS THE LINEAR-EQUATION-SYSTEM HAS AND REACT CORRESPONDINGLY****/
                    int rankA = A.Rank();

                    if (AData.Count == bData.Count)
                    {
                        for (int i = 0; i < AData.Count; i++)
                        {
                            AData[i].Add(bData[i]);
                        }

                        Matrix <double> AExtended = Matrix <double> .Build.DenseOfRows(AData);

                        int    rankAExtended = AExtended.Rank();
                        string solutions     = "";

                        //TODO: SHOW EQUATION-SYSTEM / RANKS ETC. / MAYBE WITH USING COMMUNICATIONEVENTS

                        if (rankA != rankAExtended)
                        {
                            solutions = "0";
                            Debug.Log(String.Format("The linear EquationSystem has NO solution. Reason: rank of A and rank of AExtended are not equal. RankA = {0}, RankAExtended = {1}", rankA, rankAExtended));
                            CommunicationEvents.showEquationSystemEvent.Invoke(equations, rankA, rankAExtended, solutions);
                            return(null);
                        }
                        else if (rankA < numberOfVariables)
                        {
                            solutions = "∞";
                            Debug.Log(String.Format("The linear EquationSystem has an infinite number of solutions. Reason: rank of A = rank of AExtended = {0} < varNum = {1}", rankA, numberOfVariables));
                            CommunicationEvents.showEquationSystemEvent.Invoke(equations, rankA, rankAExtended, solutions);
                            return(null);
                        }
                        else if (rankA == numberOfVariables)
                        {
                            solutions = "1";
                            Debug.Log(String.Format("The linear EquationSystem has exactly one solution. Reason: rank of A = rank of AExtended = {0} = varNum = {1}", rankA, numberOfVariables));

                            CommunicationEvents.showEquationSystemEvent.Invoke(equations, rankA, rankAExtended, solutions);

                            //Solve GLS of the form 'A * x = b':
                            Vector <double> glsSolution = A.Solve(b);

                            //Map the glsSolution to the variables and to the corresponding cogwheels
                            return(getNewlyDiscoveredAvsMap(knownAvMap, variables, glsSolution));
                        }
                        else
                        {
                            solutions = "?";
                            Debug.Log(String.Format("Rank of A = rank of AExtended = {0} > varNum = {1}", rankA, numberOfVariables));
                            CommunicationEvents.showEquationSystemEvent.Invoke(equations, rankA, rankAExtended, solutions);
                            return(null);
                        }
                    }
                    else
                    {
                        Debug.Log("Row-Count of Matrix A and Vector b were not equal!");
                        return(null);
                    }
                }
            }
            else
            {
                return(null);
            }
        }
        else
        {
            Debug.Log("KnowledgeBasedSimulation: Cannot simulate, sFactList is null.");
            return(null);
        }
    }