Esempio n. 1
0
    private static Dictionary <Fact, float> getNewlyDiscoveredAvsMap(Dictionary <Fact, float> knownAvMap, List <MMTTerm> variables, Vector <double> glsSolution)
    {
        Dictionary <Fact, float> discoveredAvsMap = new Dictionary <Fact, float>();

        //Find KeyValuePairs<Fact,float> ...
        if (glsSolution.Count.Equals(variables.Count))
        {
            foreach (Fact fact in GameState.Facts)
            {
                if (fact.GetType().Equals(typeof(CogwheelFact)))
                {
                    MMTTerm knownAvVariable = variables.Find(variable => variable.isSimplifiedCogwheelAvTerm() &&
                                                             ((OMF)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(0)).value).f.Equals(((CogwheelFact)fact).Radius) &&
                                                             ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(0)).f.Equals(((CogwheelFact)fact).Point.x) &&
                                                             ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(1)).f.Equals(((CogwheelFact)fact).Point.y) &&
                                                             ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(2)).f.Equals(((CogwheelFact)fact).Point.z));
                    //If an element was found
                    if (knownAvVariable != null)
                    {
                        double newlyDiscoveredAv = glsSolution.ElementAt(variables.IndexOf(knownAvVariable));
                        //Now we know that the Fact = fact should rotate with angularVelocity = newlyDiscoveredAv
                        discoveredAvsMap.Add(fact, (float)newlyDiscoveredAv);
                    }
                }
            }
        }
        else
        {
            Debug.Log("KnowledgeBasedSimulation.getNewlyDiscoveredAvsMap: Variables and GLS-Solution don't have the same number of elements.");
            return(null);
        }

        //Build relative complement A \ B => discoveredAvsMap \ knownAvMap
        return(discoveredAvsMap.Except(knownAvMap).ToDictionary(x => x.Key, x => x.Value));
    }
Esempio n. 2
0
    private MMTDeclaration generate90DegreeAngleDeclaration(float val, string p1URI, string p2URI, string p3URI)
    {
        MMTTerm argument = new OMA(
            new OMS(MMTURIs.Eq),
            new List <MMTTerm> {
            new OMS(MMTURIs.RealLit),
            new OMA(
                new OMS(MMTURIs.Angle),
                new List <MMTTerm> {
                new OMS(p1URI),
                new OMS(p2URI),
                new OMS(p3URI)
            }
                ),
            new OMF(val)     // 90f
        }
            );

        MMTTerm tp = new OMA(new OMS(MMTURIs.Ded), new List <MMTTerm> {
            argument
        });
        MMTTerm df = null;

        return(new MMTSymbolDeclaration(this.Label, tp, df));
    }
Esempio n. 3
0
    private static void addKnownEqsysValues(List <List <double> > AData, List <double> bData, List <MMTTerm> variables, Dictionary <Fact, float> knownAvMap)
    {
        //Add a row in AData and a value in bData for the known angularVelocity(s) of the rotating cogwheel(s)
        foreach (KeyValuePair <Fact, float> knownAv in knownAvMap)
        {
            if (knownAv.Key.GetType().Equals(typeof(CogwheelFact)))
            {
                //Find Variable, which represents the unknown angularVelocity of a cogwheel, and which is equal
                //to a cogwheel whose angularVelocity is already known (Because it is the rotatingCogwheel driven by the generator).
                MMTTerm knownAvVariable = variables.Find(variable => variable.isSimplifiedCogwheelAvTerm() &&
                                                         ((OMF)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(0)).value).f.Equals(((CogwheelFact)knownAv.Key).Radius) &&
                                                         ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(0)).f.Equals(((CogwheelFact)knownAv.Key).Point.x) &&
                                                         ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(1)).f.Equals(((CogwheelFact)knownAv.Key).Point.y) &&
                                                         ((OMF)((OMA)((RECARG)((OMA)((OMA)variable).arguments.ElementAt(0)).arguments.ElementAt(3)).value).arguments.ElementAt(2)).f.Equals(((CogwheelFact)knownAv.Key).Point.z));
                int numberOfElementsPerRow = AData.ElementAt(0).Count;
                //Create new row with zero-entries
                List <double> newADataRow = new List <double>(new double[numberOfElementsPerRow]);

                if (knownAvVariable != null)
                {
                    //Set 1 in AData for the column of the corresponding variable
                    newADataRow[variables.IndexOf(knownAvVariable)] = 1;
                    //Add new column to AData
                    AData.Add(newADataRow);
                    //Add known angularVelocity to bData
                    bData.Add(knownAv.Value);
                }
            }
        }
    }
Esempio n. 4
0
 /**
  * Constructor used for sending new declarations to mmt
  */
 public MMTValueDeclaration(string label, MMTTerm lhs, MMTTerm valueTp, MMTTerm value)
 {
     this.label   = label;
     this.lhs     = lhs;
     this.valueTp = valueTp;
     this.value   = value;
 }
Esempio n. 5
0
    private void init(string pid, string rid)
    {
        this.Pid = pid;
        this.Rid = rid;

        PointFact pf   = _Facts[pid] as PointFact;
        RayFact   rf   = _Facts[rid] as RayFact;
        string    pURI = pf.Id;
        string    rURI = rf.Id;

        List <MMTTerm> innerArguments = new List <MMTTerm>
        {
            new OMS(rURI),
            new OMS(pURI)
        };

        List <MMTTerm> outerArguments = new List <MMTTerm>
        {
            new OMA(new OMS(MMTURIs.OnLine), innerArguments)
        };

        //OMS constructor generates full URI
        MMTTerm tp = new OMA(new OMS(MMTURIs.Ded), outerArguments);
        MMTTerm df = null;

        MMTSymbolDeclaration mmtDecl = new MMTSymbolDeclaration(this.Label, tp, df);

        AddFactResponse.sendAdd(mmtDecl, out this._URI);
    }
Esempio n. 6
0
 /**A MMTTerm is a Value, if it has a positive float value (type OMF)
  * OR if it has a negative float value (type OMA(OMS(Minus), OMF))
  **/
 private bool mmtTermEqualsValue(MMTTerm term)
 {
     return((term.GetType().Equals(typeof(OMF))) ||
            (term.GetType().Equals(typeof(OMA)) &&
             ((OMA)term).applicant.GetType().Equals(typeof(OMS)) &&
             ((OMS)((OMA)term).applicant).uri.Equals(MMTURIs.Minus) &&
             ((OMA)term).arguments.Count == 1 &&
             ((OMA)term).arguments.ElementAt(0).GetType().Equals(typeof(OMF))));
 }
Esempio n. 7
0
        public override bool Equals(object obj)
        {
            if (!(obj is MMTTerm))
            {
                return(false);
            }

            MMTTerm term = (MMTTerm)obj;

            return(kind.Equals(term.kind));
        }
Esempio n. 8
0
 private void getPlusTerms(List <MMTTerm> plusTerms, MMTTerm newTerm)
 {
     if (newTerm.GetType().Equals(typeof(OMA)) &&
         ((OMA)newTerm).applicant.GetType().Equals(typeof(OMS)) &&
         ((OMS)((OMA)newTerm).applicant).uri.Equals(MMTURIs.Addition))
     {
         //plus_real_lit is a function with two input parameters
         getPlusTerms(plusTerms, ((OMA)newTerm).arguments.ElementAt(0));
         getPlusTerms(plusTerms, ((OMA)newTerm).arguments.ElementAt(1));
     }
     else
     {
         plusTerms.Add(newTerm);
     }
 }
Esempio n. 9
0
    /**
     * Used to process the right side of an equation
     * Returns false, if sth. went wrong with the equation
     */
    private bool addToVectorB(List <double> bData, MMTTerm newTerm)
    {
        if (newTerm.GetType().Equals(typeof(OMF)))
        {
            bData.Add((double)((OMF)newTerm).f);
            return(true);
        }
        else if (newTerm.GetType().Equals(typeof(OMA)) &&
                 ((OMA)newTerm).applicant.GetType().Equals(typeof(OMS)) &&
                 ((OMS)((OMA)newTerm).applicant).uri.Equals(MMTURIs.Minus) &&
                 ((OMA)newTerm).arguments.Count == 1 &&
                 ((OMA)newTerm).arguments.ElementAt(0).GetType().Equals(typeof(OMF)))
        {
            bData.Add((double)(-1.0 * ((OMF)((OMA)newTerm).arguments.ElementAt(0)).f));
            return(true);
        }

        Debug.Log("SimplifiedFact.addToVectorB: newTerm wasn't either an OMF or an OMA(OMS(Minus),OMF)");
        return(false);
    }
Esempio n. 10
0
 public OMA(MMTTerm applicant, List <MMTTerm> arguments)
 {
     this.applicant = applicant;
     this.arguments = arguments;
 }
Esempio n. 11
0
 /**
  * Constructor used for sending new declarations to mmt
  */
 public MMTSymbolDeclaration(string label, MMTTerm tp, MMTTerm df)
 {
     this.label = label;
     this.tp    = tp;
     this.df    = df;
 }
Esempio n. 12
0
 public RECARG(string name, MMTTerm value)
 {
     this.name  = name;
     this.value = value;
 }
Esempio n. 13
0
    public Tuple <List <List <double> >, List <double>, List <MMTTerm> > parseEquationSystem()
    {
        if (this.equations == null || this.equations.Count == 0)
        {
            Debug.Log("SEqsysFact.ParseEquationSystem: equations null or empty.");
            return(null);
        }
        else
        {
            bool equationsHaveTwoElements = equations.Aggregate(true, (total, next) => total &= (next.Count == 2));
            bool equationsAreNormalized   = equations.Aggregate(true, (total, next) => total &= (mmtTermEqualsValue(next.ElementAt(0)) || mmtTermEqualsValue(next.ElementAt(1))));

            if (!equationsHaveTwoElements || !equationsAreNormalized)
            {
                Debug.Log("SEqsysFact.ParseEquationSystem: Some equations either don't have 2 elements OR are not normalized.");
                return(null);
            }
            else
            {
                //We want to create a GLS of the form 'A * x = b'
                List <List <double> > AData = new List <List <double> >();
                List <double>         bData = new List <double>();
                //variables-list is used to bring elements of AData into the right order, and
                //to know how many elements each row in AData must have
                List <MMTTerm> variables = new List <MMTTerm>();

                bool processingEquationsSuccessfully = true;

                //Foreach equation, we want to add a row in A and an element in b
                foreach (List <MMTTerm> equation in this.equations)
                {
                    if (!processingEquationsSuccessfully)
                    {
                        break;
                    }

                    MMTTerm leftSide  = equation.ElementAt(0);
                    MMTTerm rightSide = equation.ElementAt(1);

                    if (mmtTermEqualsValue(rightSide))
                    {
                        processingEquationsSuccessfully &= this.addToMatrixA(AData, variables, leftSide);
                        processingEquationsSuccessfully &= this.addToVectorB(bData, rightSide);
                    }
                    else
                    {
                        processingEquationsSuccessfully &= this.addToMatrixA(AData, variables, rightSide);
                        processingEquationsSuccessfully &= this.addToVectorB(bData, leftSide);
                    }
                }

                if (processingEquationsSuccessfully)
                {
                    //Complement missing 0's in AData
                    int ADataColumns = variables.Count();
                    foreach (List <double> row in AData)
                    {
                        for (int i = row.Count; i < ADataColumns; i++)
                        {
                            row.Add(0);
                        }
                    }

                    //TODO: Filter duplicate rows in AData/bData
                    return(new Tuple <List <List <double> >, List <double>, List <MMTTerm> >(AData, bData, variables));
                }
                else
                {
                    return(null);
                }
            }
        }
    }
Esempio n. 14
0
    /**
     * Used to process the left side of an equation
     * Returns false, if sth. went wrong with the equation
     */
    private bool addToMatrixA(List <List <double> > AData, List <MMTTerm> variables, MMTTerm newTerm)
    {
        List <double>             newADataRow;
        Dictionary <MMTTerm, OMF> multPairs = new Dictionary <MMTTerm, OMF>();

        bool           noErrors  = true;
        List <MMTTerm> plusTerms = new List <MMTTerm>();

        getPlusTerms(plusTerms, newTerm);

        foreach (MMTTerm plusTerm in plusTerms)
        {
            //case: Times_real_lit of arguments
            //If the plusTerm is an OMA(OMS(times_real_lit), arguments), we know that one or both of the arguments
            //has to be a Variable, otherwise times_real_lit would have been simplified on the MMT-side
            if (plusTerm.GetType().Equals(typeof(OMA)) &&
                ((OMA)plusTerm).applicant.GetType().Equals(typeof(OMS)) &&
                ((OMS)((OMA)plusTerm).applicant).uri.Equals(MMTURIs.Multiplication))
            {
                if (!(((OMA)plusTerm).arguments.ElementAt(0).GetType().Equals(typeof(OMF)) ^ ((OMA)plusTerm).arguments.ElementAt(1).GetType().Equals(typeof(OMF))))
                {
                    noErrors = false;
                    Debug.Log("SimplifiedFact.addToMatrixA: Corrupt simplified times_real_lit term.");
                    break;
                }
                else
                {
                    OMF     multiplier;
                    MMTTerm multiplicand;
                    //Element at Index = 0 is OMF
                    if (((OMA)plusTerm).arguments.ElementAt(0).GetType().Equals(typeof(OMF)))
                    {
                        multiplier   = (OMF)((OMA)plusTerm).arguments.ElementAt(0);
                        multiplicand = ((OMA)plusTerm).arguments.ElementAt(1);
                    }
                    //Element at Index = 1 is OMF
                    else
                    {
                        multiplier   = (OMF)((OMA)plusTerm).arguments.ElementAt(1);
                        multiplicand = ((OMA)plusTerm).arguments.ElementAt(0);
                    }

                    if (!(variables.Contains(multiplicand)))
                    {
                        variables.Add(multiplicand);
                    }

                    multPairs.Add(multiplicand, multiplier);
                }
            }
        }

        newADataRow = new List <double>(new double[variables.Count]);
        foreach (KeyValuePair <MMTTerm, OMF> multPair in multPairs)
        {
            newADataRow[variables.IndexOf(multPair.Key)] = multPair.Value.f;
        }

        AData.Add(newADataRow);
        return(noErrors);
    }