MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOp_ROp_max_inequ_viol.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 
44 #include <math.h>
45 
46 #define max(a,b) ( (a) > (b) ? (a) : (b) )
47 #define min(a,b) ( (a) < (b) ? (a) : (b) )
48 
50 #include "RTOp_obj_null_vtbl.h"
51 #include "RTOp_obj_value_vtbl.h"
52 #include "RTOp_obj_free_free.h"
53 #include "RTOp_get_reduct_op.hpp"
54 
55 #include <stdlib.h>
56 
57 /* */
58 /* Implementation functions */
59 /* */
60 
61 /* Functions for the reduction target object */
62 
64  const struct RTOp_obj_type_vtbl_t* vtbl
65  ,const void* obj_data
66  ,int* num_values
67  ,int* num_indexes
68  ,int* num_chars
69  )
70 {
71  *num_values = 3;
72  *num_indexes = 2;
73  *num_chars = 0;
74  return 0;
75 }
76 
77 static int targ_obj_reinit(
78  const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
79  , RTOp_ReductTarget targ_obj )
80 {
82  *targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
83  targ->max_viol = 0.0; /* None violated yet! */
84  targ->v_i = 0.0; /* arbitrary */
85  targ->vLU_i = 0.0; /* arbitrary */
86  targ->max_viol_i = 0; /* None violated yet! */
87  targ->bnd_type = -2; /* Invalid type (just in case used by accident) */
88  return 0;
89 }
90 
91 static int targ_obj_create(
92  const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
93  ,RTOp_ReductTarget* targ_obj
94  )
95 {
96  const int mem_size = sizeof(struct RTOp_ROp_max_inequ_viol_reduct_obj_t);
97  *targ_obj = malloc( mem_size );
98  return targ_obj_reinit(vtbl,obj_data,*targ_obj);
99 }
100 
102  const struct RTOp_obj_type_vtbl_t* vtbl
103  ,const void * obj_data
104  ,void * reduct_obj
105  ,int num_values
106  ,RTOp_value_type value_data[]
107  ,int num_indexes
108  ,RTOp_index_type index_data[]
109  ,int num_chars
110  ,RTOp_char_type char_data[]
111  )
112 {
113  struct RTOp_ROp_max_inequ_viol_reduct_obj_t *targ = NULL;
114  assert( reduct_obj );
115  assert( num_values == 3 );
116  assert( num_indexes == 3 );
117  assert( num_chars == 0 );
118  targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)reduct_obj;
119  value_data[0] = targ->max_viol;
120  value_data[1] = targ->v_i;
121  value_data[2] = targ->vLU_i;
122  index_data[0] = targ->max_viol_i;
123  index_data[1] = targ->bnd_type;
124  return 0;
125 }
126 
127 static int targ_load_state(
128  const struct RTOp_obj_type_vtbl_t* vtbl
129  ,const void* obj_data
130  ,int num_values
131  ,const RTOp_value_type value_data[]
132  ,int num_indexes
133  ,const RTOp_index_type index_data[]
134  ,int num_chars
135  ,const RTOp_char_type char_data[]
136  ,void ** reduct_obj
137  )
138 {
139  struct RTOp_ROp_max_inequ_viol_reduct_obj_t *targ = NULL;
140  assert( *reduct_obj );
141  assert( num_values == 3 );
142  assert( num_indexes == 2 );
143  assert( num_chars == 0 );
144  targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)*reduct_obj;
145  targ->max_viol = value_data[0];
146  targ->v_i = value_data[1];
147  targ->vLU_i = value_data[2];
148  targ->max_viol_i = index_data[0];
149  targ->bnd_type = index_data[1];
150  return 0;
151 }
152 
153 static const struct RTOp_obj_type_vtbl_t targ_obj_vtbl =
154 {
161 };
162 
163 /* Other functions */
164 
166  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
167  ,const int num_vecs, const struct RTOp_SubVector vecs[]
168  ,const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
169  ,RTOp_ReductTarget targ_obj
170  )
171 {
172  /* */
173  /* Declare local variables */
174  /* */
175 
176  /* targ */
178  *targ = NULL;
179  /* global_off */
180  size_t global_offset;
181  /* sub_dim */
182  size_t sub_dim;
183  /* v */
184  const RTOp_value_type *v_val = NULL;
185  ptrdiff_t v_val_s;
186  /* vL */
187  const RTOp_value_type *vL_val = NULL;
188  ptrdiff_t vL_val_s;
189  /* vU */
190  const RTOp_value_type *vU_val = NULL;
191  ptrdiff_t vU_val_s;
192 
193  register size_t k;
194  RTOp_index_type i;
195  RTOp_value_type v_scale;
196  RTOp_value_type violL;
197  RTOp_value_type violU;
198 
199  /* */
200  /* Validate the input */
201  /* */
202  if( num_vecs != 3 )
204  if( num_targ_vecs != 0 )
206  if( vecs[0].global_offset != vecs[1].global_offset
207  || vecs[0].sub_dim != vecs[1].sub_dim
208  || vecs[0].global_offset != vecs[2].global_offset
209  || vecs[0].sub_dim != vecs[2].sub_dim )
211 
212  /* */
213  /* Get pointers to the data */
214  /* */
215 
216  /* targ */
217  targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
218  /* global_off */
219  global_offset = vecs[0].global_offset;
220  /* sub_dim */
221  sub_dim = vecs[0].sub_dim;
222  /* v */
223  v_val = vecs[0].values;
224  v_val_s = vecs[0].values_stride;
225  /* vL */
226  vL_val = vecs[1].values;
227  vL_val_s = vecs[1].values_stride;
228  /* vU */
229  vU_val = vecs[2].values;
230  vU_val_s = vecs[2].values_stride;
231 
232  /* */
233  /* Perform the reduction operation. */
234  /* */
235 
236  i = global_offset + 1;
237  for( k = 0; k < sub_dim; ++k, ++i, v_val += v_val_s, vL_val += vL_val_s, vU_val += vU_val_s ) {
238  v_scale = 1.0 / (1.0 + fabs(*v_val));
239  /* (vL - v)*v_scale */
240  violL = (*vL_val - *v_val) * v_scale;
241  /* (v - vU)*v_scale */
242  violU = (*v_val - *vU_val) * v_scale;
243  /* Perform the reduction */
244  if(
245  ( max(violL,violU) > targ->max_viol )
246  ||
247  ( max(violL,violU) == targ->max_viol && i < targ->max_viol_i )
248  )
249  {
250  targ->bnd_type = ( violL > 0.0
251  ? ( *vL_val == *vU_val
252  ? 0 /* EQUALITY */
253  : -1 /* LOWER */
254  )
255  : +1 /* UPPER */
256  );
257  targ->max_viol = ( targ->bnd_type <= 0 ? violL : violU );
258  targ->v_i = *v_val;
259  targ->vLU_i = ( targ->bnd_type <= 0 ? *vL_val : *vU_val );
260  targ->max_viol_i = i;
261  }
262  }
263 
264  return 0; /* success? */
265 }
266 
268  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
269  , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
270 {
272  *i_targ = (const struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)in_reduct_obj;
274  *io_targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)inout_reduct_obj;
275 
276  if(
277  ( i_targ->max_viol > io_targ->max_viol )
278  ||
279  ( i_targ->max_viol == io_targ->max_viol && i_targ->max_viol_i < io_targ->max_viol_i )
280  )
281  {
282  io_targ->max_viol = i_targ->max_viol;
283  io_targ->v_i = i_targ->v_i;
284  io_targ->vLU_i = i_targ->vLU_i;
285  io_targ->max_viol_i = i_targ->max_viol_i;
286  io_targ->bnd_type = i_targ->bnd_type;
287  }
288  return 0;
289 }
290 
295 
297 {
299  ,&targ_obj_vtbl
300  ,"ROp_max_inequ_viol"
301  ,NULL
305 };
306 
307 /* Class specific functions */
308 
310 {
312  op->obj_data = NULL;
313  return 0; /* success? */
314 }
315 
317 {
318  op->vtbl = NULL;
319  op->obj_data = NULL;
320  return 0; /* success? */
321 }
322 
325 {
326  return *(struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
327 }
#define max(a, b)
ptrdiff_t values_stride
Definition: RTOp.h:305
double RTOp_value_type
Definition: RTOp.h:69
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)
struct RTOp_ROp_max_inequ_viol_reduct_obj_t RTOp_ROp_max_inequ_viol_val(RTOp_ReductTarget targ_obj)
void * RTOp_ReductTarget
Definition: RTOp.h:191
RTOp_index_type global_offset
Definition: RTOp.h:299
#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
int RTOp_ROp_max_inequ_viol_destroy(struct RTOp_RTOp *op)
const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_inequ_viol_vtbl
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)
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[])
#define RTOp_ERR_INVALID_NUM_VECS
Definition: RTOp.h:266
const struct RTOp_obj_type_vtbl_t RTOp_obj_null_vtbl
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)
void * obj_data
Definition: RTOp.h:800
const struct RTOp_RTOp_vtbl_t * vtbl
Definition: RTOp.h:802
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)
static int RTOp_ROp_max_inequ_viol_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)
int RTOp_ROp_max_inequ_viol_construct(struct RTOp_RTOp *op)
INSERT_GET_REDUCT_OP_FUNCS(3, 2, 0, RTOp_ROp_max_inequ_viol_reduct_obj_t, reduce_reduct_objs, targ_load_state, targ_extract_state, external_reduct_op, get_reduct_op) const
static int targ_obj_reinit(const struct RTOp_obj_type_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget targ_obj)
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)
int RTOp_obj_free_free(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
RTOp_index_type sub_dim
Definition: RTOp.h:301
#define RTOp_ERR_INVALID_NUM_TARG_VECS
Definition: RTOp.h:268
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