85 bool eval(std::vector<Sym> &path_constraints,
86 std::unordered_map<int, float> params) {
88 for (
int i = 0; i < path_constraints.size(); i++) {
89 result = result && (path_constraints[i].eval(params,
eps) <= 0.0f);
110 bool solve(std::vector<Sym> &path_constraints,
111 std::unordered_map<int, float> ¶ms,
112 bool is_init_params_const =
true) {
113 if (path_constraints.size() == 0) {
121 std::unordered_map<int, bool> is_const;
122 std::unordered_set<int> unique_var_ids;
124 for (
int i = 0; i < path_constraints.size(); i++) {
125 path_constraints[i].gather_var_ids(unique_var_ids);
128 for (
int i : unique_var_ids) {
129 if (params.find(i) == params.end()) {
131 params.emplace(std::make_pair(i, idist(
gen)));
133 params.emplace(std::make_pair(i, rdist(
gen)));
135 is_const.emplace(std::make_pair(i,
false));
137 is_const.emplace(std::make_pair(i, is_init_params_const));
142 bool is_sat =
eval(path_constraints, params);
143 bool is_converge =
false;
145 while ((!is_sat) && (!is_converge) && (itr <
num_epochs)) {
147 for (
int i = 0; i < path_constraints.size(); i++) {
148 if (path_constraints[i].
eval(params,
eps) > 0.0f) {
149 grads = grads + path_constraints[i].grad(params,
eps);
153 for (
auto &g : grads.
val) {
154 if (!is_const.at(g.first)) {
155 if (g.second != 0.0f) {
159 params.at(g.first) -=
lr * g.second;
162 if (g.second > 0.0f) {
164 }
else if (g.second < 0.0f) {
167 params.at(g.first) -=
lr * sign;
171 is_sat =
eval(path_constraints, params);
Definition: compiler.h:11
void gen(Node *node, Prog &prg)
Generates virtual instructions for a given AST node.
Definition: compiler.h:35
Gradient Descent Optimizer for Symbolic Path Constraints.
Definition: gd.h:28
bool sign_grad
Definition: gd.h:34
bool init_param_uniform_int
Definition: gd.h:36
int num_used_itr
Number of used iterations during optimization.
Definition: gd.h:43
float lr
Learning rate for gradient descent.
Definition: gd.h:30
bool contain_randomized_vars
Definition: gd.h:40
float param_low
Lower bound for parameter initialization.
Definition: gd.h:32
int num_epochs
Maximum number of optimization epochs.
Definition: gd.h:29
float eps
The smallest positive value.
Definition: gd.h:31
bool eval(std::vector< Sym > &path_constraints, std::unordered_map< int, float > params)
Evaluate if path constraints are satisfied for given parameters.
Definition: gd.h:85
float param_high
Upper bound for parameter initialization.
Definition: gd.h:33
int seed
Random seed for initializing parameter values.
Definition: gd.h:42
bool solve(std::vector< Sym > &path_constraints, std::unordered_map< int, float > ¶ms, bool is_init_params_const=true)
Solve path constraints using gradient descent optimization.
Definition: gd.h:110
GDOptimizer(int num_epochs=100, float lr=1.0, float eps=1.0, float param_low=-10.0, float param_high=10.0, bool sign_grad=true, bool init_param_uniform_int=true, int seed=42)
Constructor for GDOptimizer.
Definition: gd.h:61
Struct representing the gradient of a symbolic expression.
Definition: type.h:153
std::unordered_map< int, float > val
Definition: type.h:155
Implementatations of basic alias, types, and classes.