31 std::vector<c_int>* P_indices,
32 std::vector<c_int>* P_indptr) {
34 const int num_of_variables = 3 * n;
35 const int num_of_nonzeros = num_of_variables + (n - 1);
36 std::vector<std::vector<std::pair<c_int, c_float>>> columns(num_of_variables);
41 for (
int i = 0; i < n - 1; ++i) {
47 columns[n - 1].emplace_back(
53 for (
int i = 0; i < n - 1; ++i) {
54 columns[n + i].emplace_back(
59 columns[2 * n - 1].emplace_back(2 * n - 1,
66 columns[2 * n].emplace_back(2 * n,
71 for (
int i = 0; i < n - 1; ++i) {
72 columns[2 * n + i + 1].emplace_back(
78 for (
int i = 1; i < n - 1; ++i) {
79 columns[2 * n + i].emplace_back(
84 columns[3 * n - 1].emplace_back(
90 CHECK_EQ(value_index, num_of_nonzeros);
93 for (
int i = 0; i < num_of_variables; ++i) {
94 P_indptr->push_back(ind_p);
95 for (
const auto& row_data_pair : columns[i]) {
96 P_data->push_back(row_data_pair.second * 2.0);
97 P_indices->push_back(row_data_pair.first);
101 P_indptr->push_back(ind_p);
105 std::vector<c_float>* A_data, std::vector<c_int>* A_indices,
106 std::vector<c_int>* A_indptr, std::vector<c_float>* lower_bounds,
107 std::vector<c_float>* upper_bounds) {
112 const int num_of_extra_constraints =
113 static_cast<int>(extra_constraints_.size());
114 const int num_of_vertex_constraints =
115 static_cast<int>(vertex_constraints_.size());
116 const int num_of_variables = 3 * n;
117 const int num_of_constraints = num_of_variables + 3 * (n - 1) + 3 +
118 num_of_extra_constraints +
119 num_of_vertex_constraints;
120 lower_bounds->resize(num_of_constraints);
121 upper_bounds->resize(num_of_constraints);
123 std::vector<std::vector<std::pair<c_int, c_float>>> variables(
126 int constraint_index = 0;
128 for (
int i = 0; i < num_of_variables; ++i) {
130 variables[i].emplace_back(constraint_index, 1.0);
131 lower_bounds->at(constraint_index) =
133 upper_bounds->at(constraint_index) =
135 }
else if (i < 2 * n) {
136 variables[i].emplace_back(constraint_index, 1.0);
138 lower_bounds->at(constraint_index) =
140 upper_bounds->at(constraint_index) =
143 variables[i].emplace_back(constraint_index, 1.0);
144 lower_bounds->at(constraint_index) =
146 upper_bounds->at(constraint_index) =
151 CHECK_EQ(constraint_index, num_of_variables);
152 for (
int i = 0; i < num_of_extra_constraints; ++i) {
153 auto& left_index = extra_constraints_[i].left_index;
154 auto& right_index = extra_constraints_[i].right_index;
155 auto& left_weight = extra_constraints_[i].left_weight;
156 auto& right_weight = extra_constraints_[i].right_weight;
158 variables[left_index].emplace_back(constraint_index,
160 variables[right_index].emplace_back(constraint_index,
162 lower_bounds->at(constraint_index) = extra_constraints_[i].lower_bound;
163 upper_bounds->at(constraint_index) = extra_constraints_[i].upper_bound;
166 for (
int i = 0; i < num_of_vertex_constraints; ++i) {
167 auto& left_index = vertex_constraints_[i].left_index;
168 auto& right_index = vertex_constraints_[i].right_index;
169 auto& left_weight = vertex_constraints_[i].left_weight;
170 auto& right_weight = vertex_constraints_[i].right_weight;
172 variables[left_index].emplace_back(constraint_index,
174 variables[right_index].emplace_back(constraint_index,
176 variables[n + left_index].emplace_back(
179 variables[n + right_index].emplace_back(
182 lower_bounds->at(constraint_index) = vertex_constraints_[i].lower_bound;
183 upper_bounds->at(constraint_index) = vertex_constraints_[i].upper_bound;
187 for (
int i = 0; i + 1 < n; ++i) {
188 variables[2 * n + i].emplace_back(constraint_index, -1.0);
189 variables[2 * n + i + 1].emplace_back(constraint_index, 1.0);
190 lower_bounds->at(constraint_index) =
192 upper_bounds->at(constraint_index) =
197 for (
int i = 0; i + 1 < n; ++i) {
198 variables[n + i].emplace_back(constraint_index, -1.0 *
scale_factor_[2]);
199 variables[n + i + 1].emplace_back(constraint_index, 1.0 *
scale_factor_[2]);
200 variables[2 * n + i].emplace_back(constraint_index,
202 variables[2 * n + i + 1].emplace_back(constraint_index,
204 lower_bounds->at(constraint_index) = 0.0;
205 upper_bounds->at(constraint_index) = 0.0;
211 for (
int i = 0; i + 1 < n; ++i) {
212 variables[i].emplace_back(constraint_index,
214 variables[i + 1].emplace_back(constraint_index,
216 variables[n + i].emplace_back(
218 variables[2 * n + i].emplace_back(
221 variables[2 * n + i + 1].emplace_back(
225 lower_bounds->at(constraint_index) = 0.0;
226 upper_bounds->at(constraint_index) = 0.0;
230 variables[0].emplace_back(constraint_index, 1.0);
235 variables[n].emplace_back(constraint_index, 1.0);
240 variables[2 * n].emplace_back(constraint_index, 1.0);
245 CHECK_EQ(constraint_index, num_of_constraints);
247 for (
int i = 0; i < num_of_variables; ++i) {
248 A_indptr->push_back(ind_p);
249 for (
const auto& variable_nz : variables[i]) {
251 A_data->push_back(variable_nz.second);
254 A_indices->push_back(variable_nz.first);
260 A_indptr->push_back(ind_p);