-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem.h
More file actions
90 lines (77 loc) · 2.78 KB
/
problem.h
File metadata and controls
90 lines (77 loc) · 2.78 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
/*
* Copyright 2026 Daniel Cederberg and William Zhang
*
* This file is part of the DNLP-differentiation-engine project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PROBLEM_H
#define PROBLEM_H
#include "expr.h"
#include "utils/CSR_Matrix.h"
#include "utils/Timer.h"
#include <stdbool.h>
typedef struct
{
double time_init_derivatives;
double time_eval_jacobian;
double time_eval_gradient;
double time_eval_hessian;
double time_forward_obj;
double time_forward_constraints;
int nnz_affine;
int nnz_nonlinear; /* jacobian of nonlinear constraints */
int nnz_hessian;
} Diff_engine_stats;
typedef struct problem
{
expr *objective;
expr **constraints;
int n_constraints;
int n_vars;
int total_constraint_size;
/* Allocated by new_problem */
double *constraint_values;
double *gradient_values;
/* Allocated by problem_init_derivatives */
CSR_Matrix *jacobian;
CSR_Matrix *lagrange_hessian;
int *hess_idx_map; /* Maps all wsum_hess nnz to lagrange_hessian (obj +
constraints) */
/* for the affine shortcut we keep track of the first time the jacobian and
* hessian are called */
bool jacobian_called;
/* Parameter tracking for fast parameter updates */
expr **param_nodes; /* weak references to parameter nodes in tree */
int n_param_nodes;
int total_parameter_size;
/* Statistics for performance measurement */
Diff_engine_stats stats;
bool verbose;
} problem;
/* Retains objective and constraints (shared ownership with caller) */
problem *new_problem(expr *objective, expr **constraints, int n_constraints,
bool verbose);
void problem_init_jacobian(problem *prob);
void problem_init_hessian(problem *prob);
void problem_init_derivatives(problem *prob);
void free_problem(problem *prob);
double problem_objective_forward(problem *prob, const double *u);
void problem_constraint_forward(problem *prob, const double *u);
void problem_gradient(problem *prob);
void problem_jacobian(problem *prob);
void problem_hessian(problem *prob, double obj_w, const double *w);
/* Parameter support */
void problem_register_params(problem *prob, expr **param_nodes, int n_param_nodes);
void problem_update_params(problem *prob, const double *theta);
#endif