Exemplo n.º 1
0
        private byte[][] RandomPlacementOverInitialState(byte[][] initialMatrix)
        {
            byte[][] sample = initialMatrix.Rotate();

            for (int j = 0; j < this.Columns; j++)
            {
                int    numberOfOnes = this.desiredContainerInstanceCounts[j];
                byte[] array        = RandomBinary.GenerateRandomBinaryArray(sample[j], numberOfOnes);
                sample[j] = array;
            }

            return(sample.Rotate());
        }
Exemplo n.º 2
0
        private byte[][] GenerateSample()
        {
            byte[][] sample = new byte[this.Columns][];

            for (int j = 0; j < this.Columns; j++)
            {
                int    numberOfOnes = this.desiredContainerInstanceCounts[j];
                byte[] array        = RandomBinary.GenerateRandomBinaryArray(this.Rows, numberOfOnes);
                sample[j] = array;
            }

            return(sample.Rotate());
        }
Exemplo n.º 3
0
        private List <Configuration> buildConfigs(VariabilityModel vm, List <SamplingStrategies> binaryStrats, List <ExperimentalDesign> numericStrats, List <HybridStrategy> hybridStrats)
        {
            List <Configuration> result = new List <Configuration>();

            List <List <BinaryOption> > binaryConfigs = new List <List <BinaryOption> >();
            List <Dictionary <NumericOption, Double> > numericConfigs = new List <Dictionary <NumericOption, double> >();

            foreach (SamplingStrategies strat in binaryStrats)
            {
                switch (strat)
                {
                //Binary sampling heuristics
                case SamplingStrategies.ALLBINARY:
                    if (optionsToConsider.ContainsKey(SamplingStrategies.ALLBINARY))
                    {
                        List <List <BinaryOption> > variants =
                            vg.GenerateAllVariantsFast(vm.reduce(optionsToConsider[SamplingStrategies.ALLBINARY]));
                        binaryConfigs.AddRange(changeModel(vm, variants));
                    }
                    else
                    {
                        binaryConfigs.AddRange(vg.GenerateAllVariantsFast(vm));
                    }
                    break;

                case SamplingStrategies.SAT:
                    int numberSamples = 2;
                    foreach (Dictionary <string, string> parameters in binaryParams.satParameters)
                    {
                        if (parameters.ContainsKey("henard"))
                        {
                            try
                            {
                                bool b = Boolean.Parse(parameters["henard"]);
                                ((Z3VariantGenerator)vg).henard = b;
                            }
                            catch (FormatException e)
                            {
                                Console.Error.WriteLine(e);
                            }
                        }
                        if (parameters.ContainsKey("numConfigs"))
                        {
                            try
                            {
                                numberSamples = Int32.Parse(parameters["numConfigs"]);
                            }
                            catch (FormatException)
                            {
                                TWise tw = new TWise();
                                numberSamples = tw.generateT_WiseVariants_new(GlobalState.varModel, Int32.Parse(parameters["numConfigs"].Remove(0, 4))).Count;
                            }
                        }

                        if (parameters.ContainsKey("seed") && vg is Z3VariantGenerator)
                        {
                            uint seed = 0;
                            seed = UInt32.Parse(parameters["seed"]);
                            ((Z3VariantGenerator)vg).setSeed(seed);
                        }
                        if (optionsToConsider.ContainsKey(SamplingStrategies.SAT))
                        {
                            List <List <BinaryOption> > variants =
                                vg.GenerateUpToNFast(vm.reduce(optionsToConsider[SamplingStrategies.SAT]), numberSamples);
                            binaryConfigs.AddRange(changeModel(vm, variants));
                        }
                        else
                        {
                            binaryConfigs.AddRange(vg.GenerateUpToNFast(vm, numberSamples));
                        }
                        numberSamples = 2;
                    }
                    break;

                case SamplingStrategies.BINARY_RANDOM:
                    RandomBinary rb;
                    if (optionsToConsider.ContainsKey(SamplingStrategies.BINARY_RANDOM))
                    {
                        rb = new RandomBinary(vm.reduce(optionsToConsider[SamplingStrategies.BINARY_RANDOM]));
                    }
                    else
                    {
                        rb = new RandomBinary(vm);
                    }
                    foreach (Dictionary <string, string> expDesignParamSet in binaryParams.randomBinaryParameters)
                    {
                        binaryConfigs.AddRange(changeModel(vm, rb.getRandomConfigs(expDesignParamSet)));
                    }

                    break;

                case SamplingStrategies.OPTIONWISE:
                {
                    FeatureWise fw = new FeatureWise();
                    if (optionsToConsider.ContainsKey(SamplingStrategies.OPTIONWISE))
                    {
                        List <List <BinaryOption> > variants = fw.generateFeatureWiseConfigurations(GlobalState.varModel
                                                                                                    .reduce(optionsToConsider[SamplingStrategies.OPTIONWISE]));
                        binaryConfigs.AddRange(changeModel(vm, variants));
                    }
                    else
                    {
                        binaryConfigs.AddRange(fw.generateFeatureWiseConfigurations(GlobalState.varModel));
                    }
                }
                break;

                case SamplingStrategies.DISTANCE_BASED:
                    foreach (Dictionary <string, string> parameters in binaryParams.distanceMaxParameters)
                    {
                        DistanceBased distSampling = new DistanceBased(vm);
                        binaryConfigs.AddRange(distSampling.getSample(parameters));
                    }
                    break;

                //case SamplingStrategies.MINMAX:
                //    {
                //        MinMax mm = new MinMax();
                //        binaryConfigs.AddRange(mm.generateMinMaxConfigurations(GlobalState.varModel));

                //    }
                //    break;

                case SamplingStrategies.PAIRWISE:
                {
                    PairWise pw = new PairWise();
                    if (optionsToConsider.ContainsKey(SamplingStrategies.PAIRWISE))
                    {
                        List <List <BinaryOption> > variants = pw.generatePairWiseVariants(GlobalState.varModel
                                                                                           .reduce(optionsToConsider[SamplingStrategies.PAIRWISE]));
                        binaryConfigs.AddRange(changeModel(vm, variants));
                    }
                    else
                    {
                        binaryConfigs.AddRange(pw.generatePairWiseVariants(GlobalState.varModel));
                    }
                }
                break;

                case SamplingStrategies.NEGATIVE_OPTIONWISE:
                {
                    NegFeatureWise neg = new NegFeatureWise();        //2nd option: neg.generateNegativeFWAllCombinations(GlobalState.varModel));
                    if (optionsToConsider.ContainsKey(SamplingStrategies.NEGATIVE_OPTIONWISE))
                    {
                        List <List <BinaryOption> > variants = neg.generateNegativeFW(GlobalState.varModel
                                                                                      .reduce(optionsToConsider[SamplingStrategies.NEGATIVE_OPTIONWISE]));
                        binaryConfigs.AddRange(changeModel(vm, variants));
                    }
                    else
                    {
                        binaryConfigs.AddRange(neg.generateNegativeFW(GlobalState.varModel));
                    }
                }
                break;

                case SamplingStrategies.T_WISE:
                    foreach (Dictionary <string, string> ParamSet in binaryParams.tWiseParameters)
                    {
                        TWise tw = new TWise();
                        int   t  = 3;

                        foreach (KeyValuePair <String, String> param in ParamSet)
                        {
                            if (param.Key.Equals(TWise.PARAMETER_T_NAME))
                            {
                                t = Convert.ToInt16(param.Value);
                            }

                            if (optionsToConsider.ContainsKey(SamplingStrategies.T_WISE))
                            {
                                List <List <BinaryOption> > variants = tw.generateT_WiseVariants_new(
                                    vm.reduce(optionsToConsider[SamplingStrategies.T_WISE]), t);
                                binaryConfigs.AddRange(changeModel(vm, variants));
                            }
                            else
                            {
                                binaryConfigs.AddRange(tw.generateT_WiseVariants_new(vm, t));
                            }
                        }
                    }
                    break;
                }
            }

            //Experimental designs for numeric options
            if (numericStrats.Count != 0)
            {
                handleDesigns(numericStrats, numericConfigs, vm);
            }


            foreach (List <BinaryOption> binConfig in binaryConfigs)
            {
                if (numericConfigs.Count == 0)
                {
                    Configuration c = new Configuration(binConfig);
                    result.Add(c);
                }
                foreach (Dictionary <NumericOption, double> numConf in numericConfigs)
                {
                    Configuration c = new Configuration(binConfig, numConf);
                    result.Add(c);
                }
            }

            // Filter configurations based on the NonBooleanConstraints
            List <Configuration> filtered = new List <Configuration>();

            foreach (Configuration conf in result)
            {
                bool isValid = true;
                foreach (NonBooleanConstraint nbc in vm.NonBooleanConstraints)
                {
                    if (!nbc.configIsValid(conf))
                    {
                        isValid = false;
                    }
                }

                if (isValid)
                {
                    filtered.Add(conf);
                }
            }
            result = filtered;


            // Hybrid designs
            if (hybridStrats.Count != 0)
            {
                existingConfigurations = new List <Configuration>(result);
                List <Configuration> configurations = ExecuteHybridStrategy(hybridStrats, vm);

                if (numericStrats.Count == 0 && binaryStrats.Count == 0)
                {
                    result = configurations;
                }
                else
                {
                    // Prepare the previous sample sets
                    if (result.Count == 0 && binaryConfigs.Count == 0)
                    {
                        foreach (Dictionary <NumericOption, double> numConf in numericConfigs)
                        {
                            Configuration c = new Configuration(new Dictionary <BinaryOption, BinaryOption.BinaryValue>(), numConf);
                            result.Add(c);
                        }
                    }


                    if (hybridStrats.Count > 1)
                    {
                        // TODO handling of more than one hybrid strategy
                        throw new NotImplementedException("Handling more than one hybrid strategy has not been fully implemented yet!");
                    }
                    HybridStrategy hybridStrategy = hybridStrategies.First();
                    if (hybridStrategy.GetSamplingParameters(DistributionSensitive.ONLY_BINARY).Equals("true") && binaryStrats.Count > 0 ||
                        hybridStrategy.GetSamplingParameters(DistributionSensitive.ONLY_NUMERIC).Equals("true") && numericStrats.Count > 0)
                    {
                        result.AddRange(configurations);
                    }
                    else
                    {
                        // Build the cartesian product
                        List <Configuration> newResult = new List <Configuration>();
                        foreach (Configuration config in result)
                        {
                            foreach (Configuration hybridConfiguration in configurations)
                            {
                                Dictionary <BinaryOption, BinaryOption.BinaryValue> binOpts = new Dictionary <BinaryOption, BinaryOption.BinaryValue>(config.BinaryOptions);
                                Dictionary <NumericOption, double> numOpts = new Dictionary <NumericOption, double>(config.NumericOptions);

                                Dictionary <BinaryOption, BinaryOption.BinaryValue> hybridBinOpts = hybridConfiguration.BinaryOptions;
                                foreach (BinaryOption binOpt in hybridConfiguration.BinaryOptions.Keys)
                                {
                                    binOpts.Add(binOpt, hybridBinOpts[binOpt]);
                                }

                                Dictionary <NumericOption, double> hybridNumOpts = hybridConfiguration.NumericOptions;
                                foreach (NumericOption numOpt in hybridConfiguration.NumericOptions.Keys)
                                {
                                    numOpts.Add(numOpt, hybridNumOpts[numOpt]);
                                }

                                newResult.Add(new Configuration(binOpts, numOpts));
                            }
                        }
                        result = newResult;
                    }
                }
            }

            if (vm.MixedConstraints.Count == 0)
            {
                if (binaryStrats.Count == 1 && binaryStrats.Last().Equals(SamplingStrategies.ALLBINARY) && numericStrats.Count == 1 && numericStrats.Last() is FullFactorialDesign)
                {
                    return(replaceReference(result.ToList()));
                }
                return(replaceReference(result.Distinct().ToList()));
            }
            List <Configuration> unfilteredList        = result.Distinct().ToList();
            List <Configuration> filteredConfiguration = new List <Configuration>();

            foreach (Configuration toTest in unfilteredList)
            {
                bool isValid = true;
                foreach (MixedConstraint constr in vm.MixedConstraints)
                {
                    if (!constr.configIsValid(toTest))
                    {
                        isValid = false;
                    }
                }

                if (isValid)
                {
                    filteredConfiguration.Add(toTest);
                }
            }
            return(replaceReference(filteredConfiguration));
        }
        public static List <Configuration> buildConfigs(VariabilityModel vm, List <SamplingStrategies> binaryStrategies,
                                                        List <ExperimentalDesign> experimentalDesigns)
        {
            List <Configuration> result = new List <Configuration>();
            VariantGenerator     vg     = new VariantGenerator();

            List <List <BinaryOption> > binaryConfigs = new List <List <BinaryOption> >();
            List <Dictionary <NumericOption, Double> > numericConfigs = new List <Dictionary <NumericOption, double> >();

            foreach (SamplingStrategies strat in binaryStrategies)
            {
                switch (strat)
                {
                //Binary sampling heuristics
                case SamplingStrategies.ALLBINARY:
                    binaryConfigs.AddRange(vg.generateAllVariantsFast(vm));
                    break;

                case SamplingStrategies.BINARY_RANDOM:
                    RandomBinary rb = new RandomBinary(vm);
                    foreach (Dictionary <string, string> expDesignParamSet in binaryParams.randomBinaryParameters)
                    {
                        binaryConfigs.AddRange(rb.getRandomConfigs(expDesignParamSet));
                    }

                    break;

                case SamplingStrategies.OPTIONWISE:
                {
                    FeatureWise fw = new FeatureWise();
                    binaryConfigs.AddRange(fw.generateFeatureWiseConfigurations(GlobalState.varModel));
                }
                break;

                //case SamplingStrategies.MINMAX:
                //    {
                //        MinMax mm = new MinMax();
                //        binaryConfigs.AddRange(mm.generateMinMaxConfigurations(GlobalState.varModel));

                //    }
                //    break;

                case SamplingStrategies.PAIRWISE:
                {
                    PairWise pw = new PairWise();
                    binaryConfigs.AddRange(pw.generatePairWiseVariants(GlobalState.varModel));
                }
                break;

                case SamplingStrategies.NEGATIVE_OPTIONWISE:
                {
                    NegFeatureWise neg = new NegFeatureWise();        //2nd option: neg.generateNegativeFWAllCombinations(GlobalState.varModel));
                    binaryConfigs.AddRange(neg.generateNegativeFW(GlobalState.varModel));
                }
                break;

                case SamplingStrategies.T_WISE:
                    foreach (Dictionary <string, string> ParamSet in binaryParams.tWiseParameters)
                    {
                        TWise tw = new TWise();
                        int   t  = 3;

                        foreach (KeyValuePair <String, String> param in ParamSet)
                        {
                            if (param.Key.Equals(TWise.PARAMETER_T_NAME))
                            {
                                t = Convert.ToInt16(param.Value);
                            }
                            binaryConfigs.AddRange(tw.generateT_WiseVariants_new(vm, t));
                        }
                    }
                    break;
                }
            }

            //Experimental designs for numeric options
            if (experimentalDesigns.Count != 0)
            {
                handleDesigns(experimentalDesigns, numericConfigs, vm);
            }


            foreach (List <BinaryOption> binConfig in binaryConfigs)
            {
                if (numericConfigs.Count == 0)
                {
                    Configuration c = new Configuration(binConfig);
                    result.Add(c);
                }
                foreach (Dictionary <NumericOption, double> numConf in numericConfigs)
                {
                    Configuration c = new Configuration(binConfig, numConf);
                    result.Add(c);
                }
            }
            if (vm.MixedConstraints.Count == 0)
            {
                return(result.Distinct().ToList());
            }
            else
            {
                List <Configuration> unfilteredList        = result.Distinct().ToList();
                List <Configuration> filteredConfiguration = new List <Configuration>();
                foreach (Configuration toTest in unfilteredList)
                {
                    bool isValid = true;
                    foreach (MixedConstraint constr in vm.MixedConstraints)
                    {
                        if (!constr.requirementsFulfilled(toTest))
                        {
                            isValid = false;
                        }
                    }

                    if (isValid)
                    {
                        filteredConfiguration.Add(toTest);
                    }
                }
                return(filteredConfiguration);
            }
        }