-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_param_broadcast.h
More file actions
157 lines (130 loc) · 5.42 KB
/
test_param_broadcast.h
File metadata and controls
157 lines (130 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef TEST_PARAM_BROADCAST_H
#define TEST_PARAM_BROADCAST_H
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "atoms/affine.h"
#include "expr.h"
#include "minunit.h"
#include "problem.h"
#include "subexpr.h"
#include "test_helpers.h"
const char *test_constant_broadcast_vector_mult(void)
{
int n = 6;
/* minimize sum(x) subject to broadcast(c) ∘ x, with c constant */
expr *x = new_variable(2, 3, 0, n);
expr *objective = new_sum(x, -1);
double c_vals[3] = {1.0, 2.0, 3.0};
expr *c = new_parameter(1, 3, PARAM_FIXED, n, c_vals);
expr *c_bcast = new_broadcast(c, 2, 3);
expr *constraint = new_vector_mult(c_bcast, x);
expr *constraints[1] = {constraint};
problem *prob = new_problem(objective, constraints, 1, false);
problem_init_derivatives(prob);
/* point for evaluating */
double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
problem_constraint_forward(prob, x_vals);
double constrs[6] = {1.0, 2.0, 6.0, 8.0, 15.0, 18.0};
problem_jacobian(prob);
double jac_x[6] = {1.0, 1.0, 2.0, 2.0, 3.0, 3.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
free_problem(prob);
return 0;
}
const char *test_constant_promote_vector_mult(void)
{
int n = 6;
/* minimize sum(x) subject to promote(c) ∘ x, with c constant */
expr *x = new_variable(2, 3, 0, n);
expr *objective = new_sum(x, -1);
double c_vals = 3.0;
expr *c = new_parameter(1, 1, PARAM_FIXED, n, &c_vals);
expr *c_bcast = new_promote(c, 2, 3);
expr *constraint = new_vector_mult(c_bcast, x);
expr *constraints[1] = {constraint};
problem *prob = new_problem(objective, constraints, 1, false);
problem_init_derivatives(prob);
/* point for evaluating */
double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
problem_constraint_forward(prob, x_vals);
double constrs[6] = {3.0, 6.0, 9.0, 12.0, 15.0, 18.0};
problem_jacobian(prob);
double jac_x[6] = {3.0, 3.0, 3.0, 3.0, 3.0, 3.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
free_problem(prob);
return 0;
}
const char *test_param_broadcast_vector_mult(void)
{
int n = 6;
/* minimize sum(x) subject to broadcast(p) ∘ x, with p parameter */
expr *x = new_variable(2, 3, 0, n);
expr *objective = new_sum(x, -1);
double c_vals[3] = {1.0, 2.0, 3.0};
expr *c = new_parameter(1, 3, 0, n, c_vals);
expr *c_bcast = new_broadcast(c, 2, 3);
expr *constraint = new_vector_mult(c_bcast, x);
expr *constraints[1] = {constraint};
problem *prob = new_problem(objective, constraints, 1, false);
expr *param_nodes[1] = {c};
problem_register_params(prob, param_nodes, 1);
problem_init_derivatives(prob);
/* point for evaluating */
double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
problem_constraint_forward(prob, x_vals);
double constrs[6] = {1.0, 2.0, 6.0, 8.0, 15.0, 18.0};
problem_jacobian(prob);
double jac_x[6] = {1.0, 1.0, 2.0, 2.0, 3.0, 3.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
/* second iteration after updating parameter */
double theta[3] = {5.0, 4.0, 3.0};
problem_update_params(prob, theta);
problem_constraint_forward(prob, x_vals);
problem_jacobian(prob);
double updated_constrs[6] = {5.0, 10.0, 12.0, 16.0, 15.0, 18.0};
double updated_jac_x[6] = {5.0, 5.0, 4.0, 4.0, 3.0, 3.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, updated_constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, updated_jac_x, 6));
free_problem(prob);
return 0;
}
const char *test_param_promote_vector_mult(void)
{
int n = 6;
/* minimize sum(x) subject to promote(p) ∘ x, with p parameter */
expr *x = new_variable(2, 3, 0, n);
expr *objective = new_sum(x, -1);
double c_vals = 3.0;
expr *c = new_parameter(1, 1, 0, n, &c_vals);
expr *c_bcast = new_promote(c, 2, 3);
expr *constraint = new_vector_mult(c_bcast, x);
expr *constraints[1] = {constraint};
problem *prob = new_problem(objective, constraints, 1, false);
expr *param_nodes[1] = {c};
problem_register_params(prob, param_nodes, 1);
problem_init_derivatives(prob);
/* point for evaluating */
double x_vals[6] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
problem_constraint_forward(prob, x_vals);
double constrs[6] = {3.0, 6.0, 9.0, 12.0, 15.0, 18.0};
problem_jacobian(prob);
double jac_x[6] = {3.0, 3.0, 3.0, 3.0, 3.0, 3.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, jac_x, 6));
/* second iteration after updating parameter */
double theta = 5.0;
problem_update_params(prob, &theta);
problem_constraint_forward(prob, x_vals);
problem_jacobian(prob);
double updated_constrs[6] = {5.0, 10.0, 15.0, 20.0, 25.0, 30.0};
double updated_jac_x[6] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0};
mu_assert("vals fail", cmp_double_array(prob->constraint_values, updated_constrs, 6));
mu_assert("vals fail", cmp_double_array(prob->jacobian->x, updated_jac_x, 6));
free_problem(prob);
return 0;
}
#endif /* TEST_PARAM_BROADCAST_H */