##// END OF EJS Templates
Adds weights to operations (2)...
Alexandre Leroux -
r1216:0083fced54a2
parent child
Show More
@@ -43,7 +43,7 struct FuzzingOperationFactory {
43 43 static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type);
44 44 };
45 45
46 using OperationsTypes = std::set<FuzzingOperationType>;
47 Q_DECLARE_METATYPE(OperationsTypes)
46 using WeightedOperationsTypes = std::map<FuzzingOperationType, double>;
47 Q_DECLARE_METATYPE(WeightedOperationsTypes)
48 48
49 49 #endif // SCIQLOP_FUZZINGOPERATIONS_H
@@ -24,11 +24,13 namespace {
24 24 // /////// //
25 25
26 26 using VariableId = int;
27 using Weight = double;
28 using Weights = std::vector<Weight>;
27 29
28 30 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
29 31 using VariablesOperations = std::vector<VariableOperation>;
30 32
31 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
33 using WeightedOperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, Weight>;
32 34 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
33 35
34 36 // ///////// //
@@ -39,7 +41,7 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
39 41 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
40 42 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
41 43 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
42 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
44 = QVariant::fromValue(WeightedOperationsTypes{{FuzzingOperationType::CREATE, 1.}});
43 45
44 46 // /////// //
45 47 // Methods //
@@ -47,32 +49,40 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
47 49
48 50 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
49 51 /// pairs that are valid (i.e. operation that can be executed on variable)
50 VariablesOperations availableOperations(const VariablesPool &variablesPool,
51 const OperationsPool &operationsPool)
52 std::pair<VariablesOperations, Weights>
53 availableOperations(const VariablesPool &variablesPool,
54 const WeightedOperationsPool &operationsPool)
52 55 {
53 56 VariablesOperations result{};
57 Weights weights{};
54 58
55 59 for (const auto &variablesPoolEntry : variablesPool) {
56 60 auto variableId = variablesPoolEntry.first;
57 61 auto variable = variablesPoolEntry.second;
58 62
59 for (const auto &operation : operationsPool) {
63 for (const auto &operationsPoolEntry : operationsPool) {
64 auto operation = operationsPoolEntry.first;
65 auto weight = operationsPoolEntry.second;
66
60 67 // A pair is valid if the current operation can be executed on the current variable
61 68 if (operation->canExecute(variable)) {
62 69 result.push_back({variableId, operation});
70 weights.push_back(weight);
63 71 }
64 72 }
65 73 }
66 74
67 return result;
75 return {result, weights};
68 76 }
69 77
70 OperationsPool createOperationsPool(const OperationsTypes &types)
78 WeightedOperationsPool createOperationsPool(const WeightedOperationsTypes &types)
71 79 {
72 OperationsPool result{};
80 WeightedOperationsPool result{};
73 81
74 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
75 [](const auto &type) { return FuzzingOperationFactory::create(type); });
82 std::transform(
83 types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) {
84 return std::make_pair(FuzzingOperationFactory::create(type.first), type.second);
85 });
76 86
77 87 return result;
78 88 }
@@ -101,13 +111,16 public:
101 111 auto canExecute = true;
102 112 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
103 113 // Retrieves all operations that can be executed in the current context
104 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
114 VariablesOperations variableOperations{};
115 Weights weights{};
116 std::tie(variableOperations, weights)
117 = availableOperations(m_VariablesPool, operationsPool());
105 118
106 119 canExecute = !variableOperations.empty();
107 120 if (canExecute) {
108 121 // Of the operations available, chooses a random operation and executes it
109 122 auto variableOperation
110 = RandomGenerator::instance().randomChoice(variableOperations);
123 = RandomGenerator::instance().randomChoice(variableOperations, weights);
111 124
112 125 auto variableId = variableOperation.first;
113 126 auto variable = m_VariablesPool.at(variableId);
@@ -143,11 +156,11 private:
143 156 return result;
144 157 }
145 158
146 OperationsPool operationsPool() const
159 WeightedOperationsPool operationsPool() const
147 160 {
148 161 static auto result = createOperationsPool(
149 162 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
150 .value<OperationsTypes>());
163 .value<WeightedOperationsTypes>());
151 164 return result;
152 165 }
153 166
General Comments 0
You need to be logged in to leave comments. Login now