source: branches/devel/Cbc/src/OMI_code/omi_model_getobjective.c @ 604

Last change on this file since 604 was 572, checked in by forrest, 13 years ago

for Bob Entriken

File size: 5.7 KB
Line 
1/**
2  * @(#)omi_model_getobjective.c
3  * @author Robert Entriken<br>
4  * Copyright (C) 1999-2007 EPRI<br>
5  * All rights reserved.
6  * @version 07-03-07
7  * @since OMI_1.0
8  *
9  * Revisions:
10  *   06-02-20 bobe     added copyright
11  *   06-01-28 jmm      new package layout
12  *   06-04-10 jmm      minor adjustments to compile under Linux
13  *   07-03-07 bobe     updated Copyright
14  *
15  */
16#include <time.h>
17#include "com_epri_omi_Model.h"
18#include "omi_Model.h"
19#include "omi_State.h"
20
21/**
22  *  Call back to get the objective value computed in Java.
23  *
24  */
25void omi_model_getobjective__(integer *mode, integer *n, doublereal *x,
26  doublereal *f, doublereal *g, integer* nstate, integer* nprob) {
27  int VERBOSE = 0;
28 
29/*
30  extern time_t time(int);
31  extern char * asctime(int);
32  extern int localtime(time_t *);
33*/
34
35  // local state variables
36  JNIEnv         *env;
37  jobject        jmodel;
38  ClassOMI_Model *model;
39  FILE           *out;
40 
41  // io variables
42// unused  FILE *out2, *out_old;
43  time_t     t;
44 
45  // local variables
46  jclass    cls;  // holds pointer to omi.Model.class
47  jmethodID mid;  // holds offset for omi.Model.getObjective()
48  jfieldID  fid;
49  int i;
50 
51  char prefix[] = "Model.c:omi_model_getobjective__():";
52  char javaName[] = "getObjective";
53//      char mySig[]  = "(II[DD[DII)V";
54//  char javaSig[]  = "(IIII)V"; // for the minos version
55  char javaSig[]  = "()V"; // for the omi version
56 
57  jsize start, len;
58
59  // fill in state
60  env    = omi_state.env;
61  jmodel = omi_state.jmodel;
62  model  = omi_state.model;
63  out    = omi_state.out;
64
65  /*
66  out2 = fopen("minos.txt", "a");
67  fprintf(out2,"begin\n");
68  fprintf(out2,"%s begin\n",prefix);
69  if (OMI_state_1.env    == NULL) fprintf(out2,"%s env is NULL\n",prefix);
70  if (OMI_state_1.jmodel == NULL) fprintf(out2,"%s jmodel is NULL\n",prefix);
71  if (OMI_state_1.out    == NULL) fprintf(out2,"%s out is NULL\n",prefix);
72  fprintf(out2,"%s &env = %X, &jmodel = %X, &out = %X\n",prefix,&env,&jmodel,&out);
73  fprintf(out2,"%s  env = %X,  jmodel = %X,  out = %X\n",prefix,env,jmodel,out);
74  fprintf(out2,"%s &OMI_state_1.env = %X, &OMI_state_1.jmodel = %X, &OMI_state_1.out = %X\n",
75    prefix,&OMI_state_1.env,&OMI_state_1.jmodel,&OMI_state_1.out);
76  fclose(out2);
77  */
78 
79  // Print entry time
80  t = time(NULL);
81  if (VERBOSE>0) fprintf(out,"\n%s started %s\n", prefix, asctime(localtime(&t)));
82 
83  // Print arguments
84  if (VERBOSE>1) {
85    fprintf(out, "%s *mode = %i, *n = %i, *nstate = %i, *nprob = %i\n",
86      prefix, *mode, *n, *nstate, *nprob);
87    for (i = 0; i < *n; i++) fprintf(out, "%s x[%i] = %g\n",prefix, i, x[i]);
88  }
89 
90  // Check state.
91  if (env == NULL) {
92    fprintf(out, "%s !ERROR, NULL 'env' from OMI_state.\n", prefix);
93    fprintf(out,"%s return\n",prefix);
94    fclose(out);
95    return;
96
97  }; 
98  if (jmodel == NULL) {
99    fprintf(out, "%s !ERROR, NULL 'jmodel' from OMI_state.\n", prefix);
100    fprintf(out,"%s return\n",prefix);
101    fclose(out);
102    return;
103  };
104  if (VERBOSE>0) fprintf(out, "%s State OK.\n", prefix);
105
106  // get the class pointer and method offset
107  cls = (*env)->GetObjectClass(env, jmodel);
108  if (VERBOSE>2) fprintf(out, "%s Got Class pointer.\n", prefix);
109 
110  mid = (*env)->GetMethodID(env, cls, javaName, javaSig);
111  if (VERBOSE>2) fprintf(out, "%s Got Method ID.\n", prefix);
112
113  if (mid == NULL) {
114    fprintf(out,"%s ERROR: Cannot GetMethodID for '%s', '%s'\n",
115      prefix,javaName,javaSig);
116    fprintf(out,"%s return\n",prefix);
117    fclose(out);
118    return;
119  }
120 
121  // put x in jgetObjectiveArg
122  fid = (*env)->GetFieldID(env, cls, "getObjectiveArg", "[D");
123  if (fid == 0) {
124    printf("%s!ERROR, Cannot GetFieldID of 'getObjectiveArg'.\n",prefix);
125    return;
126  };
127  if ((model->jgetObjectiveArg = (*env)->GetObjectField(env,jmodel,fid)) == NULL) {
128      printf("%s!ERROR, Cannot GetDoubleField of 'getObjectiveArg'.\n",prefix);
129    return;
130  };
131  start = 0;
132  len   = *n;
133  (*env)->SetDoubleArrayRegion(env, model->jgetObjectiveArg, start, len, x);
134
135  // call back to Java
136  if (VERBOSE>0) fprintf(out, "%s In C, calling omi_model_getobjective__\n", prefix);
137 
138//  (*env)->CallVoidMethod(env, jmodel, mid, *mode, *n, *nstate, *nprob);  // pass state
139  (*env)->CallVoidMethod(env, jmodel, mid);  // get state from primal_solution
140 
141  if (VERBOSE>0) fprintf(out, "%s In C, back from omi_model_getobjective__\n", prefix);
142 
143  // get f from jmodel
144  fid = (*env)->GetFieldID(env, cls, "objective_value", "D");
145  if (fid == 0) {
146    fprintf(out, "%s !ERROR: Cannot GetFieldID of 'objective_value'\n", prefix);
147    fclose(out);
148    return;
149  };
150  model->objective_value = (*env)->GetDoubleField(env, jmodel, fid);
151  *f = model->objective_value;
152  if (VERBOSE>1) {
153    fprintf(out, "%s model->objective_value = %g\n", prefix, model->objective_value);
154    fprintf(out, "%s f = %X, *f = %g\n", prefix, f, *f);
155  }
156 
157  // transfer jmodel.jobjective to g
158  if ((model->objective=(*env)->GetDoubleArrayElements(env,model->jobjective,0))==NULL) {
159    fprintf(out,"%s !ERROR, Cannot GetDoubleArrayElements of 'model->jobjective'.\n",prefix);
160    fclose(out);
161    return;
162  };
163  for (i = 0; i < *n; i++)  g[i] = model->objective[i];  // hack tbd pass a ref?
164 
165  if (VERBOSE>1) {
166    fprintf(out, "%s &model->objective = %X, model->objective = %X, *model->objective = %g\n", 
167      prefix, &model->objective, model->objective, *model->objective);
168    fprintf(out, "%s &g = %X, g = %X, *g = %g\n", prefix, &g, g, *g);
169    for (i = 0; i < *n; i++) 
170      fprintf(out, "Model.c:omi_model_getobjective__(): &g[%i] = %X, g[%i] = %g\n",i,&g[i],i,g[i]);
171  }
172
173    if (VERBOSE>0) fprintf(out,"%s return\n",prefix);
174  fclose(out);
175  return;
176
177}  //  omi_model_getobjective__()
178
Note: See TracBrowser for help on using the repository browser.