Scippy

SCIP

Solving Constraint Integer Programs

nlpi_ipopt_dummy.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2015 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file nlpi_ipopt_dummy.c
17  * @brief dummy Ipopt NLP interface for the case that Ipopt is not available
18  * @author Stefan Vigerske
19  * @author Benjamin Müller
20  *
21  * This code has been separate from nlpi_ipopt.cpp, so the SCIP build system recognizes it as pure C code,
22  * thus the linker does not need to be changed to C++.
23  */
24 
25 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
26 
27 #include "scip/pub_message.h"
28 #include "nlpi/nlpi_ipopt.h"
29 
30 /** create solver interface for Ipopt solver */
32  BMS_BLKMEM* blkmem, /**< block memory data structure */
33  SCIP_NLPI** nlpi /**< pointer to buffer for nlpi address */
34  )
35 {
36  assert(nlpi != NULL);
37 
38  *nlpi = NULL;
39 
40  return SCIP_OKAY;
41 } /*lint !e715*/
42 
43 /** gets string that identifies Ipopt (version number) */
44 const char* SCIPgetSolverNameIpopt(void)
45 {
46  return "";
47 }
48 
49 /** gets string that describes Ipopt (version number) */
50 const char* SCIPgetSolverDescIpopt(void)
51 {
52  return "";
53 }
54 
55 /** returns whether Ipopt is available, i.e., whether it has been linked in */
57 {
58  return FALSE;
59 }
60 
61 /** gives a pointer to the IpoptApplication object stored in Ipopt-NLPI's NLPI problem data structure */
63  SCIP_NLPIPROBLEM* nlpiproblem /**< NLP problem of Ipopt-NLPI */
64  )
65 {
66  SCIPerrorMessage("Ipopt not available!\n");
67  SCIPABORT();
68  return NULL; /*lint !e527*/
69 } /*lint !e715*/
70 
71 /** gives a pointer to the NLPIORACLE object stored in Ipopt-NLPI's NLPI problem data structure */
73  SCIP_NLPIPROBLEM* nlpiproblem /**< NLP problem of Ipopt-NLPI */
74  )
75 {
76  SCIPerrorMessage("Ipopt not available!\n");
77  SCIPABORT();
78  return NULL; /*lint !e527*/
79 } /*lint !e715*/
80 
81 /** sets modified default settings that are used when setting up an Ipopt problem
82  *
83  * Do not forget to add a newline after the last option in optionsstring.
84  */
86  SCIP_NLPI* nlpi, /**< Ipopt NLP interface */
87  const char* optionsstring /**< string with options as in Ipopt options file */
88  )
89 {
90  SCIPerrorMessage("Ipopt not available!\n");
91  SCIPABORT();
92 } /*lint !e715*/
93 
94 /** Calls Lapacks Dsyev routine to compute eigenvalues and eigenvectors of a dense matrix.
95  * It's here, because Ipopt is linked against Lapack.
96  */
98  SCIP_Bool computeeigenvectors,/**< should also eigenvectors should be computed ? */
99  int N, /**< dimension */
100  SCIP_Real* a, /**< matrix data on input (size N*N); eigenvectors on output if computeeigenvectors == TRUE */
101  SCIP_Real* w /**< buffer to store eigenvalues (size N) */
102  )
103 {
104  SCIPerrorMessage("Ipopt not available, cannot use it's Lapack link!\n");
105  return SCIP_ERROR;
106 } /*lint !e715*/
107 
108 /* solves a linear problem of the form Ax = b */
110  int N, /**< dimension */
111  SCIP_Real** a, /**< matrix data on input (size N*N) */
112  SCIP_Real* b, /**< right hand side vector (size N) */
113  SCIP_Real* x, /**< buffer to store solution (size N) */
114  SCIP_Bool* success /**< pointer to store if the solving routine was successful */
115  )
116 {
117  SCIP_Real** LU;
118  SCIP_Real* y;
119  int* pivot;
120  int k;
121 
122  assert(N > 0);
123  assert(a != NULL);
124  assert(b != NULL);
125  assert(x != NULL);
126  assert(success != NULL);
127 
128  *success = TRUE;
129 
130  /* copy arrays */
131  SCIP_ALLOC( BMSallocMemoryArray(&LU, N) );
133  SCIP_ALLOC( BMSallocMemoryArray(&pivot, N) );
134 
135  /* initialize values */
136  for( k = 0; k < N; ++k )
137  {
138  int j;
139  SCIP_ALLOC( BMSallocMemoryArray(&LU[k], N) ); /*lint !e866*/
140  for( j = 0; j < N; ++j )
141  LU[k][j] = a[k][j];
142 
143  y[k] = b[k];
144  pivot[k] = k;
145  }
146 
147  /* first step: compute LU factorization */
148  for( k = 0; k < N; ++k )
149  {
150  int p;
151  int i;
152 
153  p = k;
154  for( i = k+1; i < N; ++i )
155  {
156  if( ABS( LU[pivot[i]][k] ) > ABS( LU[pivot[p]][k] ) )
157  p = i;
158  }
159 
160  if( ABS(LU[pivot[p]][k]) < 1e-08 )
161  {
162  SCIPerrorMessage("Error in nlpi_ipopt_dummy - matrix is singular!\n");
163  *success = FALSE;
164  goto TERMINATE;
165  }
166 
167  if( p != k )
168  {
169  int tmp;
170 
171  tmp = pivot[k];
172  pivot[k] = pivot[p];
173  pivot[p] = tmp;
174  }
175 
176  for( i = k+1; i < N; ++i )
177  {
178  SCIP_Real m;
179  int j;
180 
181  m = LU[pivot[i]][k] / LU[pivot[k]][k];
182 
183  for( j = k+1; j < N; ++j )
184  LU[pivot[i]][j] -= m * LU[pivot[k]][j];
185 
186  LU[pivot[i]][k] = m;
187  }
188  }
189 
190  /* second step: forward substitution */
191  y[0] = b[pivot[0]];
192 
193  for( k = 1; k < N; ++k )
194  {
195  SCIP_Real s;
196  int j;
197 
198  s = b[pivot[k]];
199  for( j = 0; j < k; ++j )
200  {
201  s -= LU[pivot[k]][j] * y[j];
202  }
203  y[k] = s;
204  }
205 
206  /* third step: backward substitution */
207  x[N-1] = y[N-1] / LU[pivot[N-1]][N-1];
208  for( k = N-2; k >= 0; --k )
209  {
210  SCIP_Real s;
211  int j;
212 
213  s = y[k];
214  for( j = k+1; j < N; ++j )
215  {
216  s -= LU[pivot[k]][j] * x[j];
217  }
218  x[k] = s / LU[pivot[k]][k];
219  }
220 
221  TERMINATE:
222  /* free arrays */
223  for( k = 0; k < N; ++k )
224  BMSfreeMemoryArray(&LU[k]);
225 
226  BMSfreeMemoryArray(&pivot);
227  BMSfreeMemoryArray(&LU);
228  BMSfreeMemoryArray(&y);
229 
230  return SCIP_OKAY;
231 }
SCIP_RETCODE SCIPcreateNlpSolverIpopt(BMS_BLKMEM *blkmem, SCIP_NLPI **nlpi)
void * SCIPgetIpoptApplicationPointerIpopt(SCIP_NLPIPROBLEM *nlpiproblem)
SCIP_RETCODE LapackDsyev(SCIP_Bool computeeigenvectors, int N, SCIP_Real *a, SCIP_Real *w)
#define NULL
Definition: lpi_spx.cpp:130
void SCIPsetModifiedDefaultSettingsIpopt(SCIP_NLPI *nlpi, const char *optionsstring)
#define FALSE
Definition: def.h:53
#define TRUE
Definition: def.h:52
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define BMSallocMemoryArray(ptr, num)
Definition: memory.h:78
#define BMSfreeMemoryArray(ptr)
Definition: memory.h:102
#define SCIPerrorMessage
Definition: pub_message.h:45
void * SCIPgetNlpiOracleIpopt(SCIP_NLPIPROBLEM *nlpiproblem)
Ipopt NLP interface.
#define SCIP_Bool
Definition: def.h:50
public methods for message output
SCIP_RETCODE SCIPsolveLinearProb(int N, SCIP_Real **a, SCIP_Real *b, SCIP_Real *x, SCIP_Bool *success)
SCIP_Bool SCIPisIpoptAvailableIpopt(void)
#define SCIP_Real
Definition: def.h:124
const char * SCIPgetSolverDescIpopt(void)
struct BMS_BlkMem BMS_BLKMEM
Definition: memory.h:392
const char * SCIPgetSolverNameIpopt(void)
#define SCIP_ALLOC(x)
Definition: def.h:274
#define SCIPABORT()
Definition: def.h:235