MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOp_ROp_max_near_feas_step.c
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "RTOp_obj_value_vtbl.h"
46 #include "RTOp_obj_free_free.h"
47 #include "RTOp_get_reduct_op.hpp"
48 
49 #include <stdlib.h>
50 
51 /* */
52 /* Implementation functions */
53 /* */
54 
55 /* Functions for the reduction target object */
56 
58  const struct RTOp_obj_type_vtbl_t* vtbl
59  ,const void* obj_data
60  ,int* num_values
61  ,int* num_indexes
62  ,int* num_chars
63  )
64 {
65  *num_values = 2;
66  *num_indexes = 0;
67  *num_chars = 0;
68  return 0;
69 }
70 
71 static int targ_obj_create(
72  const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
73  , RTOp_ReductTarget* targ_obj )
74 {
75  const int mem_size = sizeof(struct RTOp_ROp_max_near_feas_step_reduct_obj_t);
76  *targ_obj = malloc( mem_size );
77  return vtbl->obj_reinit(vtbl,obj_data,*targ_obj);
78 }
79 
80 static int targ_obj_reinit(
81  const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
82  , RTOp_ReductTarget targ_obj )
83 {
85  *targ = (struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)targ_obj;
86  targ->alpha_pos = +1e+50; /* big enough? */
87  targ->alpha_neg = -1e+50; /* big enough? */
88  return 0;
89 }
90 
91 static int targ_extract_state(
92  const struct RTOp_obj_type_vtbl_t* vtbl
93  ,const void * obj_data
94  ,void * reduct_obj
95  ,int num_values
96  ,RTOp_value_type value_data[]
97  ,int num_indexes
98  ,RTOp_index_type index_data[]
99  ,int num_chars
100  ,RTOp_char_type char_data[]
101  )
102 {
103  struct RTOp_ROp_max_near_feas_step_reduct_obj_t *targ = NULL;
104 #ifdef RTOp_DEBUG
105  assert( reduct_obj );
106  assert( num_values == 2 );
107  assert( num_indexes == 0 );
108  assert( num_chars == 0 );
109  assert( value_data );
110 #endif
111  targ = (struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)reduct_obj;
112  value_data[0] = targ->alpha_pos;
113  value_data[1] = targ->alpha_neg;
114  return 0;
115 }
116 
117 static int targ_load_state(
118  const struct RTOp_obj_type_vtbl_t* vtbl
119  ,const void* obj_data
120  ,int num_values
121  ,const RTOp_value_type value_data[]
122  ,int num_indexes
123  ,const RTOp_index_type index_data[]
124  ,int num_chars
125  ,const RTOp_char_type char_data[]
126  ,void ** reduct_obj
127  )
128 {
129  struct RTOp_ROp_max_near_feas_step_reduct_obj_t *targ = NULL;
130 #ifdef RTOp_DEBUG
131  assert( *reduct_obj );
132  assert( num_values == 2 );
133  assert( num_indexes == 0 );
134  assert( num_chars == 0 );
135  assert( value_data );
136 #endif
137  targ = (struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)*reduct_obj;
138  targ->alpha_pos = value_data[0];
139  targ->alpha_neg = value_data[1];
140  return 0;
141 }
142 
143 static const struct RTOp_obj_type_vtbl_t targ_obj_vtbl =
144 {
151 };
152 
153 /* Other functions */
154 
156  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
157  , const int num_vecs, const struct RTOp_SubVector vecs[]
158  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
159  , RTOp_ReductTarget targ_obj )
160 {
161  RTOp_value_type beta;
163  *targ = NULL;
164  RTOp_index_type sub_dim;
165  const RTOp_value_type *xl_val;
166  ptrdiff_t xl_val_s;
167  const RTOp_value_type *x_val;
168  ptrdiff_t x_val_s;
169  const RTOp_value_type *d_val;
170  ptrdiff_t d_val_s;
171  const RTOp_value_type *xu_val;
172  ptrdiff_t xu_val_s;
173  register RTOp_index_type k;
174  RTOp_value_type alpha;
175 
176  /* */
177  /* Validate the input */
178  /* */
179  if( num_vecs != 4 )
181  assert(vecs);
182  if( num_targ_vecs != 0 )
184  if( vecs[0].global_offset != vecs[1].global_offset
185  || vecs[0].sub_dim != vecs[1].sub_dim
186  || vecs[0].global_offset != vecs[2].global_offset
187  || vecs[0].sub_dim != vecs[2].sub_dim
188  || vecs[0].global_offset != vecs[3].global_offset
189  || vecs[0].sub_dim != vecs[3].sub_dim
190  )
192 
193  /* */
194  /* Get pointers to data */
195  /* */
196 
197  /* beta */
198  beta = *(RTOp_value_type*)obj_data;
199  /* targ */
200  targ = (struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)targ_obj;
201  /* sub_dim */
202  sub_dim = vecs[0].sub_dim;
203  /* xl */
204  xl_val = vecs[0].values;
205  xl_val_s = vecs[0].values_stride;
206  /* x */
207  x_val = vecs[1].values;
208  x_val_s = vecs[1].values_stride;
209  /* d */
210  d_val = vecs[2].values;
211  d_val_s = vecs[2].values_stride;
212  /* xu */
213  xu_val = vecs[3].values;
214  xu_val_s = vecs[3].values_stride;
215 
216  /* */
217  /* If x has already been found to be infeasible just return */
218  /* */
219  if(targ->alpha_pos < 0.0)
220  return 0; /* success! */
221 
222  /* */
223  /* Perform the reduction operation. */
224  /* */
225  /* max alpha_pos, min alpha_neg s.t. xl - beta <= x + alpha*d <= xu + beta */
226  /* */
227  for( k = 0; k < sub_dim; ++k, xl_val += xl_val_s, x_val += x_val_s, d_val += d_val_s, xu_val += xu_val_s ) {
228  if( *x_val < *xl_val - beta || *x_val > *xu_val + beta ) {
229  targ->alpha_pos = -1.0; /* x is infeasible as is */
230  return 0; /* success! */
231  }
232  if( *d_val != 0.0 ) {
233  /* check lower bound */
234  alpha = (*xl_val - beta - *x_val) / *d_val;
235  if( ( alpha > 0.0 && alpha < targ->alpha_pos )
236  || ( alpha == 0.0 && *d_val < 0.0 ) )
237  targ->alpha_pos = alpha;
238  if( ( alpha < 0.0 && -alpha < -targ->alpha_neg )
239  || ( alpha == 0.0 && *d_val > 0.0 ) )
240  targ->alpha_neg = alpha;
241  /* check upper bound */
242  alpha = (*xu_val + beta - *x_val) / *d_val;
243  if( (alpha > 0.0 && alpha < targ->alpha_pos )
244  || ( alpha == 0.0 && *d_val > 0.0 ) )
245  targ->alpha_pos = alpha;
246  if( ( alpha < 0.0 && -alpha < -targ->alpha_neg )
247  || ( alpha == 0.0 && *d_val < 0.0 ) )
248  targ->alpha_neg = alpha;
249  }
250  }
251 
252  return 0; /* success? */
253 }
254 
256  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
257  , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
258 {
260  *i_targ = (const struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)in_reduct_obj;
262  *io_targ = (struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)inout_reduct_obj;
263  if( i_targ->alpha_pos < io_targ->alpha_pos )
264  io_targ->alpha_pos = i_targ->alpha_pos;
265  if( -i_targ->alpha_neg < -io_targ->alpha_neg )
266  io_targ->alpha_neg = i_targ->alpha_neg;
267  return 0;
268 }
269 
274 
275 /* Virtual function table */
277 {
278  &RTOp_obj_value_vtbl /* use simple scalar value type for object instance data */
279  ,&targ_obj_vtbl
280  ,"ROp_max_near_feas_step"
281  ,NULL
285 };
286 
287 /* Class specific functions */
288 
290 {
292  op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data);
293  *((RTOp_value_type*)op->obj_data) = beta;
294  return 0; /* success? */
295 }
296 
298 {
299  op->vtbl->obj_data_vtbl->obj_free(NULL,NULL,&op->obj_data);
300  op->vtbl = NULL;
301  return 0; /* success? */
302 }
303 
305 {
306  *((RTOp_value_type*)op->obj_data) = beta;
307  return 0; /* success? */
308 }
309 
312 {
313  return *(struct RTOp_ROp_max_near_feas_step_reduct_obj_t*)targ_obj;
314 }
const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_near_feas_step_vtbl
ptrdiff_t values_stride
Definition: RTOp.h:305
double RTOp_value_type
Definition: RTOp.h:69
void * RTOp_ReductTarget
Definition: RTOp.h:191
int(* obj_create)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:941
const struct RTOp_obj_type_vtbl_t * obj_data_vtbl
Definition: RTOp.h:819
struct RTOp_ROp_max_near_feas_step_reduct_obj_t RTOp_ROp_max_near_feas_step_val(RTOp_ReductTarget targ_obj)
#define RTOp_ERR_INCOMPATIBLE_VECS
Definition: RTOp.h:270
char RTOp_char_type
Definition: RTOp.h:70
const RTOp_value_type * values
Definition: RTOp.h:303
static int targ_load_state(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, int num_values, const RTOp_value_type value_data[], int num_indexes, const RTOp_index_type index_data[], int num_chars, const RTOp_char_type char_data[], void **reduct_obj)
int RTOp_ROp_max_near_feas_step_set_beta(RTOp_value_type beta, struct RTOp_RTOp *op)
static int get_reduct_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_reduct_op_func_ptr_t *reduct_op_func_ptr)
#define RTOp_ERR_INVALID_NUM_VECS
Definition: RTOp.h:266
int(* obj_free)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:1019
static int targ_obj_reinit(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget targ_obj)
static int reduce_reduct_objs(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj)
static int get_targ_type_num_entries(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, int *num_values, int *num_indexes, int *num_chars)
INSERT_GET_REDUCT_OP_FUNCS(2, 0, 0, RTOp_ROp_max_near_feas_step_reduct_obj_t, reduce_reduct_objs, targ_load_state, targ_extract_state, external_reduct_op, get_reduct_op) const
static const struct RTOp_obj_type_vtbl_t targ_obj_vtbl
static int targ_obj_create(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget *targ_obj)
static int RTOp_ROp_max_near_feas_step_apply_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
static int targ_extract_state(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, void *reduct_obj, int num_values, RTOp_value_type value_data[], int num_indexes, RTOp_index_type index_data[], int num_chars, RTOp_char_type char_data[])
int(* obj_reinit)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void *obj)
Definition: RTOp.h:979
int RTOp_ROp_max_near_feas_step_construct(RTOp_value_type beta, struct RTOp_RTOp *op)
void * obj_data
Definition: RTOp.h:800
const struct RTOp_RTOp_vtbl_t * vtbl
Definition: RTOp.h:802
int RTOp_obj_free_free(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
int RTOp_ROp_max_near_feas_step_destroy(struct RTOp_RTOp *op)
RTOp_index_type sub_dim
Definition: RTOp.h:301
#define RTOp_ERR_INVALID_NUM_TARG_VECS
Definition: RTOp.h:268
const struct RTOp_obj_type_vtbl_t RTOp_obj_value_vtbl
static void CALL_API external_reduct_op(void *in_targ_array, void *inout_targ_array, int *len, RTOp_Datatype *datatype)
Teuchos_Ordinal RTOp_index_type
Definition: RTOp.h:68