Debian Codegraph @

SYMPHONY/Applications/MISOCO/src/CutGen/user_cg.c

raw
1 /*===========================================================================*/
2 /* */
3 /* This file is part of the SYMPHONY Branch, Cut, and Price Library. */
4 /* */
5 /* SYMPHONY was jointly developed by Ted Ralphs ([email protected]) and */
6 /* Laci Ladanyi ([email protected]). */
7 /* */
8 /* (c) Copyright 2000-2007 Ted Ralphs. All Rights Reserved. */
9 /* */
10 /* This software is licensed under the Eclipse Public License. Please see */
11 /* accompanying file for terms. */
12 /* */
13 /*===========================================================================*/
14
15 /* SYMPHONY include files */
16 #include "sym_constants.h"
17 #include "sym_macros.h"
18 #include "sym_cg_u.h"
19
20 /* User include files */
21 #include "user.h"
22
23 /* C STL */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <math.h>
28
29 #define CONIC_APPROX_CUT 1001
30
31
32 #define TOL 1e-5
33
34 int separate_lorentz_cone(int size, double * sol, double * coef);
35 int separate_rotated_cone(int size, double * sol, double * coef);
36 void simple_separation(int size, double * p, double * coef);
37 double lorentz_cone_feasibility(int size, double const * point);
38 double rotated_cone_feasibility(int size, double const * point);
39
40 /*===========================================================================*/
41
42 /*===========================================================================*\
43 * This file contains user-written functions used by the cut generator
44 * process.
45 \*===========================================================================*/
46
47 /*===========================================================================*\
48 * Here is where the user must receive all of the data sent from
49 * user_send_cg_data() and set up data structures. Note that this function is
50 * only called if one of COMPILE_IN_CG, COMPILE_IN_LP, or COMPILE_IN_TM is
51 * FALSE. For sequential computation, nothing is needed here.
52 \*===========================================================================*/
53
54 int user_receive_cg_data(void **user, int dg_id)
55 {
56 return(USER_DEFAULT);
57 }
58
59 /*===========================================================================*/
60
61 /*===========================================================================*\
62 * If the user wants to fill in a customized routine for sending and receiving
63 * the LP solution, it can be done here. For most cases, the default routines
64 * are fine.
65 \*===========================================================================*/
66
67 int user_receive_lp_solution_cg(void *user)
68 {
69 return(USER_DEFAULT);
70 }
71
72 /*===========================================================================*/
73
74 /*===========================================================================*\
75 * Find cuts violated by a particular LP solution. This can be a fairly
76 * involved function but the bottom line is that an LP solution comes in
77 * and cuts go out. Remember, use the function cg_send_cut() to send cuts out
78 * when they are found.
79 \*===========================================================================*/
80
81 int user_find_cuts(void *user, int varnum, int iter_num, int level,
82 int index, double objval, int *indices, double *values,
83 double ub, double etol, int *num_cuts, int *alloc_cuts,
84 cut_data ***cuts) {
85 user_problem *prob = (user_problem *) user;
86 int i;
87 int j;
88 int num_cones = prob->num_cones;
89 int * cone_type = prob->cone_type;
90 int * cone_size = prob->cone_size;
91 int ** cone_members = prob->cone_members;
92 int n = prob->colnum;
93 int m = prob->rownum;
94 /* cut_data * new_cut = NULL; */
95 double * coef;
96 int res;
97 // cg_send_cut(new_cut, num_cuts, alloc_cuts, cuts);
98 // fill sol using values and indices
99 double * par_sol;
100 for (i=0; i<num_cones; ++i) {
101 /* new_cut = (cut_data *) calloc(1, sizeof(cut_data)); */
102 /* new_cut->size = cone_size[i]; */
103 /* new_cut->rhs = 0.0; */
104 /* new_cut->range = ; */
105 /* new_cut->type = CONIC_APPROX_CUT; */
106 /* new_cut->sense = 'G'; */
107 /* new_cut->deletable = ; */
108 /* new_cut->branch = ; */
109 /* new_cut->name = -2; */
110 // fill par_sol
111 coef = (double *) calloc(cone_size[i], sizeof(double));
112 par_sol = (double *) calloc(cone_size[i], sizeof(double));
113 for (j=0; j<cone_size[i]; j++) {
114 par_sol[j] = prob->curr_solution[cone_members[i][j]];
115 }
116 if (cone_type[i]==0) {
117 res = separate_lorentz_cone(cone_size[i], par_sol, coef);
118 }
119 else if (cone_type[i]==1) {
120 res = separate_rotated_cone(cone_size[i], par_sol, coef);
121 }
122 else {
123 fprintf(stderr, "Unknown cone type!\n");
124 free(par_sol);
125 return (USER_ERROR);
126 }
127 if (res==0) {
128 // add cut
129 /* cg_send_cut(new_cut, num_cuts, alloc_cuts, cuts); */
130 // fprintf(stdout, "Cone %d is infeasible. Generating cut.\n", i);
131 cg_add_explicit_cut(cone_size[i], cone_members[i], coef, 0.0, 0, 'G',
132 TRUE, num_cuts, alloc_cuts, cuts);
133 }
134 // free par_sol
135 free(par_sol);
136 /* free(new_cut); */
137 free(coef);
138 }
139 return(USER_SUCCESS);
140 }
141
142 // returns 0 if point is not epsilon feasible, nonzero otherwise
143 // fills cut->coef
144 int separate_lorentz_cone(int size, double * sol, double * coef) {
145 double feas = lorentz_cone_feasibility(size, sol);
146 if (feas>-TOL)
147 return 1;
148 double * p = (double *) calloc(size, sizeof(double));
149 for (int i=0; i<size; ++i) {
150 p[i] = sol[i];
151 }
152 // 2. compute point on cone and coefficient from the point
153 simple_separation(size, p, coef);
154 // closest_point_separation(p, coef);
155 // check if we actually cut the point
156 double term1 = 0.0;
157 int i;
158 for (i=0; i<size; ++i) {
159 term1 += coef[i]*sol[i];
160 }
161 if (term1> TOL) {
162 fprintf(stderr, "Generated plane does not cut point.\n");
163 assert(0);
164 }
165 // point is not feasible, add cut to cuts and return false
166 // rhs is allways 0.0
167 free(p);
168 return 0;
169 }
170
171 void simple_separation(int size, double * p, double * coef) {
172 // update p[0]
173 int i;
174 double term1 = 0.0;
175 for (i=1; i<size; ++i) {
176 term1 += p[i]*p[i];
177 }
178 p[0] = sqrt(term1);
179 // coef array, [2p1, -2p2, -2p3, ... -2pn]
180 coef[0] = 2.0*p[0];
181 for (i=1; i<size; ++i) {
182 coef[i] = -2.0*p[i];
183 }
184 }
185
186 // fills cut->coef
187 int separate_rotated_cone(int size, double * sol, double * coef) {
188 /* double feas = feasibility(point); */
189 /* if (feas>-options()->get_dbl_option(TOL)) */
190 /* return 1; */
191 /* // coef array, [2x1, -2x2, -2x3, ... -2xn] */
192 /* double * coef = new double[size_]; */
193 /* double * p = new double[size_]; */
194 /* for (int i=0; i<size_; ++i) { */
195 /* p[i] = point[members_[i]]; */
196 /* } */
197 /* // 2. compute point on cone and coefficient from the point */
198 /* simple_separation(p, coef); */
199 /* // closest_point_separation(p, coef); */
200 /* // check if we actually cut the point */
201 /* double term1 = std::inner_product(coef, coef+size_, p, 0.0); */
202 /* if (term1< -options()->get_dbl_option(TOL)) { */
203 /* std::cerr << "Generated plane does not cut point." << std::endl; */
204 /* throw std::exception(); */
205 /* } */
206 /* // point is not feasible, add cut to cuts_ and return false */
207 /* // index is cone_members */
208 /* // rhs is allways 0.0 */
209 /* cut = new CoinPackedVector(size_, members_, coef); */
210 /* delete[] coef; */
211 /* delete[] p; */
212 return 0;
213 }
214
215 // return the feasibility of point
216 // for Lorentz cones; x1-|x_2:n| or 2x1x2-|x_3:n|^2
217 double lorentz_cone_feasibility(int size, double const * point) {
218 int i;
219 double const * p = point;
220 double feas;
221 double term1 = p[0];
222 double term2 = 0.0;
223 for (i=1; i<size; ++i) {
224 term2+=p[i]*p[i];
225 }
226 term2 = sqrt(term2);
227 feas = term1-term2;
228 return feas;
229 }
230
231 double rotated_cone_feasibility(int size, double const * point) {
232 int i;
233 double const * p = point;
234 double feas;
235 double term1 = 2.0*p[0]*p[1];
236 double term2 = 0.0;
237 for (i=2; i<size; ++i) {
238 term2+=p[i]*p[i];
239 }
240 feas = term1-term2;
241 return feas;
242 }
243 /*===========================================================================*/
244
245 /*===========================================================================*\
246 * Free the user data structure. If the default setup is used with sequential
247 * computation, nothing needs to be filled in here.
248 \*===========================================================================*/
249
250 int user_free_cg(void **user)
251 {
252 return(USER_DEFAULT);
253 }
254
255 /*===========================================================================*/
256
257 /*===========================================================================*\
258 * This is an undocumented (for now) debugging feature which can allow the user
259 * to identify the cut which cuts off a particular known feasible solution.
260 \*===========================================================================*/
261
262 #ifdef CHECK_CUT_VALIDITY
263 int user_check_validity_of_cut(void *user, cut_data *new_cut)
264 {
265 return(USER_DEFAULT);
266 }
267 #endif