33#include < cstdint>
44#include < vector>
55
6- #include " core.hpp"
6+ #include " pyoptinterface/core.hpp"
7+ #include " pyoptinterface/nlexpr.hpp"
78
89enum class HessianSparsityType
910{
@@ -157,4 +158,120 @@ struct QuadraticEvaluator
157158 Hashmap<std::tuple<int , int >, int > &hessian_index_map,
158159 HessianSparsityType hessian_type);
159160 void eval_lagrangian_hessian (const double *restrict lambda, double *restrict hessian) const ;
161+ };
162+
163+ struct NonlinearEvaluator
164+ {
165+ // How many graph instances are there
166+ size_t n_graph_instances = 0 ;
167+ // record the inputs of graph instances
168+ struct GraphInput
169+ {
170+ std::vector<int > variables;
171+ std::vector<double > constants;
172+ };
173+ std::vector<GraphInput> graph_inputs;
174+ // record graph instances with constraint output and objective output
175+ struct GraphHash
176+ {
177+ // hash of this graph instance
178+ uint64_t hash;
179+ // index of this graph instance
180+ int index;
181+ };
182+ struct GraphHashes
183+ {
184+ std::vector<GraphHash> hashes;
185+ size_t n_hashes_since_last_aggregation;
186+ } constraint_graph_hashes, objective_graph_hashes;
187+
188+ // length = n_graph_instances
189+ // record which group this graph instance belongs to
190+ struct GraphGroupMembership
191+ {
192+ // which group it belongs to
193+ int group;
194+ // the rank in that group
195+ int rank;
196+ };
197+ std::vector<GraphGroupMembership> constraint_group_memberships, objective_group_memberships;
198+ // record which index of constraint this graph starts
199+ std::vector<int > constraint_indices_offsets;
200+
201+ // graph groups
202+ struct ConstraintGraphGroup
203+ {
204+ std::vector<int > instance_indices;
205+ AutodiffSymbolicStructure autodiff_structure;
206+ ConstraintAutodiffEvaluator autodiff_evaluator;
207+
208+ // where to store the hessian matrix
209+ // length = instance_indices.size() * hessian_nnz
210+ std::vector<int > hessian_indices;
211+ };
212+ std::vector<ConstraintGraphGroup> constraint_groups;
213+ Hashmap<uint64_t , int > hash_to_constraint_group;
214+
215+ struct ObjectiveGraphGroup
216+ {
217+ std::vector<int > instance_indices;
218+ AutodiffSymbolicStructure autodiff_structure;
219+ ObjectiveAutodiffEvaluator autodiff_evaluator;
220+ // where to store the gradient vector
221+ // length = instance_indices.size() * jacobian_nnz
222+ std::vector<int > gradient_indices;
223+ // where to store the hessian matrix
224+ // length = instance_indices.size() * hessian_nnz
225+ std::vector<int > hessian_indices;
226+ };
227+ std::vector<ObjectiveGraphGroup> objective_groups;
228+ Hashmap<uint64_t , int > hash_to_objective_group;
229+
230+ int add_graph_instance ();
231+ void finalize_graph_instance (size_t graph_index, const ExpressionGraph &graph);
232+ int aggregate_constraint_groups ();
233+ int get_constraint_group_representative (int group_index) const ;
234+ int aggregate_objective_groups ();
235+ int get_objective_group_representative (int group_index) const ;
236+
237+ void assign_constraint_group_autodiff_structure (int group_index,
238+ const AutodiffSymbolicStructure &structure);
239+ void assign_constraint_group_autodiff_evaluator (int group_index,
240+ const ConstraintAutodiffEvaluator &evaluator);
241+ void assign_objective_group_autodiff_structure (int group_index,
242+ const AutodiffSymbolicStructure &structure);
243+ void assign_objective_group_autodiff_evaluator (int group_index,
244+ const ObjectiveAutodiffEvaluator &evaluator);
245+
246+ void calculate_constraint_graph_instances_offset ();
247+
248+ // functions to evaluate the nonlinear constraints and objectives
249+
250+ // f
251+ void eval_constraints (const double *restrict x, double *restrict f) const ;
252+ double eval_objective (const double *restrict x) const ;
253+
254+ // first order derivative
255+ void analyze_constraints_jacobian_structure (size_t row_base, size_t &global_jacobian_nnz,
256+ std::vector<int > &global_jacobian_rows,
257+ std::vector<int > &global_jacobian_cols);
258+ void analyze_objective_gradient_structure (std::vector<int > &global_gradient_cols,
259+ Hashmap<int , int > &sparse_gradient_map);
260+
261+ void eval_constraints_jacobian (const double *restrict x, double *restrict jacobian) const ;
262+ void eval_objective_gradient (const double *restrict x, double *restrict grad_f) const ;
263+
264+ // second order derivative
265+ void analyze_constraints_hessian_structure (
266+ size_t &global_hessian_nnz, std::vector<int > &global_hessian_rows,
267+ std::vector<int > &global_hessian_cols,
268+ Hashmap<std::tuple<int , int >, int > &hessian_index_map, HessianSparsityType hessian_type);
269+ void analyze_objective_hessian_structure (size_t &global_hessian_nnz,
270+ std::vector<int > &global_hessian_rows,
271+ std::vector<int > &global_hessian_cols,
272+ Hashmap<std::tuple<int , int >, int > &hessian_index_map,
273+ HessianSparsityType hessian_type);
274+
275+ void eval_lagrangian_hessian (const double *restrict x, const double *restrict lambda,
276+ const double sigma, double *restrict hessian) const ;
160277};
0 commit comments