//Initialise various methods and arrays
    void Start()
    {
        fitnessMeasure = LearningModeScript.fitnessMeasure;
        Population     = LearningModeScript.Population;
        Probability    = LearningModeScript.Probability;
        nextChild.onClick.AddListener(NextChild);
        Rigidbody  = GetComponent <Rigidbody>();
        controller = new CarController();
        saveLoad   = new GeneticWeights();
        Rigidbody.GetComponent <CarPhysics>().driver = Driver.AI;
        Fitness  = new double[Population];
        Outputs  = new List <double>();
        Position = transform.position;

        //Create cars of size population
        Cars = new GeneticNetwork[Population];

        //Initiate list of weights of size pupulation
        for (int i = 0; i < Population; i++)
        {
            Cars[i] = new GeneticNetwork();
        }
    }
Exemple #2
0
        /// <summary>
        /// Gets called when an un-explored branch is encountered during program execution
        /// </summary>
        /// <param name="executionNode"></param>
        /// <param name="explorableType"></param>
        public void HandleTargetBranch(CodeLocation location, Term condition, TermManager termManager, TypeEx explorableType)
        {
            var accessedFields = new SafeList <Field>();

            if (PexMeConstants.IGNORE_UNCOV_BRANCH_IN_SYSTEM_LIB)
            {
                if (IsUncoveredLocationInSystemLib(location))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "uncoveredlocation",
                                             "Ignoring the uncovered location " + location.ToString() + ", since it is in system library");
                    return;
                }
            }

            Term unnegatedCondition;
            bool bNegated = false;

            if (termManager.TryGetInnerLogicallyNegatedValue(condition, out unnegatedCondition))
            {
                bNegated = true;
            }
            else
            {
                unnegatedCondition = condition;
            }

            var            culpritFields = new SafeList <Field>();
            Term           left, right;
            BinaryOperator binOp;

            SafeStringBuilder sbTerm = new SafeStringBuilder();

            this.ConvertTermToText(new SafeStringWriter(sbTerm), condition, termManager);

            //Handling only binary conditions. TODO: Needs to check what are the other conditions
            //The related code is in TermSolver function
            if (!termManager.TryGetBinary(unnegatedCondition, out binOp, out left, out right))
            {
                this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                         "Handling only binary operations in terms");
                return;
            }

            if (PexMeConstants.USE_TERM_SOLVER)
            {
                //TODO: Temporarily ignoring the scenario where both the sides are symbolic values
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                             "Handling only binary operations where atleast one side of the condition is concrete. Current expression has both sides symbolic");
                    return;
                }

                SafeDictionary <Field, FieldValueHolder> expectedFieldValues;
                SafeDictionary <Field, FieldValueHolder> actualFieldValues;
                SafeList <Field>  allFieldsInCondition;
                SafeList <TypeEx> allFieldTypes;
                TermSolver.SolveTerm(this.explorationComponent, condition, binOp,
                                     out actualFieldValues, out expectedFieldValues, out allFieldsInCondition, out allFieldTypes);

                //Compute an intersection to identify culprit fields
                List <Field> actualKeys   = actualFieldValues.Keys.ToList();
                List <Field> expectedKeys = expectedFieldValues.Keys.ToList();

                AddToCulpritField(allFieldsInCondition, culpritFields);
                if (culpritFields.Count == 0)
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                             "Failed to retrieve culprit fields from the uncovered branch");
                }
                else
                {
                    foreach (Field field in culpritFields)
                    {
                        FieldModificationType fieldfmt;
                        int fitnessval;
                        FitnessMeasure.ComputeFitnessValue(field, actualFieldValues[field], expectedFieldValues[field], this.host, out fieldfmt, out fitnessval);

                        if (fieldfmt == FieldModificationType.UNKNOWN)
                        {
                            continue;
                        }
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, allFieldsInCondition, fieldfmt,
                                                                   condition, sbTerm.ToString(), fitnessval, explorableType, allFieldTypes);
                    }
                }
            }
            else
            {
                FieldModificationType fmt;
                if (!termManager.IsValue(left) && !termManager.IsValue(right))
                {
                    SafeDictionary <Field, FieldValueHolder> leftFieldValues;
                    SafeList <TypeEx> leftFieldTypes;
                    var leftAccessedFields = GetInvolvedFields(this.host, termManager, left, out leftFieldValues, out leftFieldTypes);
                    if (leftAccessedFields.Count > 0)
                    {
                        AddToCulpritField(leftAccessedFields, culpritFields);
                    }

                    SafeDictionary <Field, FieldValueHolder> rightFieldValues;
                    SafeList <TypeEx> rightFieldTypes;
                    var rightAccessedFields = GetInvolvedFields(this.host, termManager, right, out rightFieldValues, out rightFieldTypes);
                    if (rightAccessedFields.Count > 0)
                    {
                        AddToCulpritField(rightAccessedFields, culpritFields);
                    }

                    int fitnessval;
                    this.handleNoConstantsInTerm(termManager, left, right, binOp, bNegated,
                                                 culpritFields, unnegatedCondition, out fmt, out fitnessval);

                    //TODO: fitnessval can be different from left and right handside terms. Needs to deal with this later
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, leftAccessedFields,
                                                               fmt, condition, sbTerm.ToString(), fitnessval, explorableType, leftFieldTypes);
                    this.pmd.AddFieldsOfUncoveredCodeLocations(location, rightAccessedFields,
                                                               fmt, condition, sbTerm.ToString(), fitnessval, explorableType, rightFieldTypes);
                }
                else
                {
                    Term non_constant_term = null;
                    if (termManager.IsValue(left))
                    {
                        non_constant_term = right;
                    }
                    else if (termManager.IsValue(right))
                    {
                        non_constant_term = left;
                    }
                    else
                    {
                        SafeDebug.AssumeNotNull(null, "Control should not come here!!!");
                    }


                    //Get accessed fields in the uncovered branching condition
                    SafeDictionary <Field, FieldValueHolder> fieldValues;
                    SafeList <TypeEx> fieldTypes;
                    accessedFields = GetInvolvedFields(this.host, termManager, non_constant_term, out fieldValues, out fieldTypes);
                    if (accessedFields.Count != 0)
                    {
                        AddToCulpritField(accessedFields, culpritFields);
                    }

                    if (culpritFields.Count == 0)
                    {
                        this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, PexMeLogCategories.Term,
                                                 "Failed to retrieve culprit fields from the uncovered branch");
                    }
                    else
                    {
                        int fitnessval;
                        this.handleAConstantInTerm(termManager, left, right, binOp, bNegated, fieldValues, culpritFields[0], out fmt, out fitnessval);
                        this.pmd.AddFieldsOfUncoveredCodeLocations(location, accessedFields,
                                                                   fmt, condition, sbTerm.ToString(), fitnessval, explorableType, fieldTypes);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// OBSOLETE: Handles a scenario where there is a term in the condition
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="binOp"></param>
        /// <param name="fmt"></param>
        private void handleAConstantInTerm(TermManager termManager, Term left, Term right,
                                           BinaryOperator binOp, bool bNegated, SafeDictionary <Field, FieldValueHolder> fieldValues, Field culpritField,
                                           out FieldModificationType fmt, out int fitnessval)
        {
            fitnessval = Int32.MaxValue;
            fmt        = FieldModificationType.UNKNOWN;
            Term non_constant_term = null;
            Term constant_term     = null;

            bool bleftRightMaintainted = true;

            if (termManager.IsValue(left))
            {
                non_constant_term     = right;
                constant_term         = left;
                bleftRightMaintainted = true;
            }
            else if (termManager.IsValue(right))
            {
                non_constant_term     = left;
                constant_term         = right;
                bleftRightMaintainted = false;
            }

            int value;

            if (termManager.TryGetI4Constant(constant_term, out value))
            {
                fmt = FieldModificationType.INCREMENT;

                FieldValueHolder fvh;
                if (fieldValues.TryGetValue(culpritField, out fvh))
                {
                    int non_constant_field_value = fvh.intValue;    //TODO: Assuming that the fieldType is Int32
                    if (bleftRightMaintainted)
                    {
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, value, non_constant_field_value, bNegated);
                    }
                    else
                    {
                        fitnessval = FitnessMeasure.ComputeFitnessValue(this.host, binOp, non_constant_field_value, value, bNegated);
                    }
                }
                else
                {
                    this.host.Log.LogWarning(WikiTopics.MissingWikiTopic, "fitness measure",
                                             "Failed to retrieve value for field " + culpritField.ToString());
                }
            }
            else if (termManager.IsDefaultValue(constant_term))
            {
                if (binOp == BinaryOperator.Ceq)
                {
                    if (culpritField.Type.ToString() == "System.Boolean")
                    {
                        fmt = bNegated ? FieldModificationType.TRUE_SET : FieldModificationType.FALSE_SET;
                    }
                    else
                    {
                        fmt = bNegated ? FieldModificationType.NON_NULL_SET : FieldModificationType.NULL_SET;
                    }
                }
            }

            Term           objectValue;
            ObjectProperty objectProperty;

            if (termManager.TryGetObjectProperty(constant_term, out objectValue, out objectProperty))
            {
                //TODO??? How to handle this scenario?
            }
        }
Exemple #4
0
 //When dropdown value is changed, also change the fitnessMeasure static var
 void DropdownValueChanged(Dropdown change)
 {
     fitnessMeasure = (FitnessMeasure)change.value;
 }