source: trunk/Clp/src/Clp_C_Interface.cpp @ 2366

Last change on this file since 2366 was 2366, checked in by forrest, 5 months ago

Clp_printModel mistakenly assumed matrix has no gaps

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 38.8 KB
Line 
1// $Id: Clp_C_Interface.cpp 2366 2018-11-11 13:46:31Z forrest $
2// Copyright (C) 2003, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6
7#include "CoinPragma.hpp"
8
9#include <cmath>
10#include <cstring>
11
12#include "CoinHelperFunctions.hpp"
13#include "ClpConfig.h"
14#include "ClpSimplex.hpp"
15#include "ClpInterior.hpp"
16#ifndef SLIM_CLP
17#include "Idiot.hpp"
18#endif
19#include <cfloat>
20// Get C stuff but with extern C
21#define CLP_EXTERN_C
22#include "Coin_C_defines.h"
23
24/// To allow call backs
25class CMessageHandler : public CoinMessageHandler {
26
27public:
28     /**@name Overrides */
29     //@{
30     virtual int print();
31     //@}
32     /**@name set and get */
33     //@{
34     /// Model
35     const Clp_Simplex * model() const;
36     void setModel(Clp_Simplex * model);
37     /// Call back
38     void setCallBack(clp_callback callback);
39     //@}
40
41     /**@name Constructors, destructor */
42     //@{
43     /** Default constructor. */
44     CMessageHandler();
45     /// Constructor with pointer to model
46     CMessageHandler(Clp_Simplex * model,
47                     FILE * userPointer = NULL);
48     /** Destructor */
49     virtual ~CMessageHandler();
50     //@}
51
52     /**@name Copy method */
53     //@{
54     /** The copy constructor. */
55     CMessageHandler(const CMessageHandler&);
56     /** The copy constructor from an CoinSimplexMessageHandler. */
57     CMessageHandler(const CoinMessageHandler&);
58
59     CMessageHandler& operator=(const CMessageHandler&);
60     /// Clone
61     virtual CoinMessageHandler * clone() const ;
62     //@}
63
64
65protected:
66     /**@name Data members
67        The data members are protected to allow access for derived classes. */
68     //@{
69     /// Pointer back to model
70     Clp_Simplex * model_;
71     /// call back
72     clp_callback callback_;
73     //@}
74};
75
76
77//-------------------------------------------------------------------
78// Default Constructor
79//-------------------------------------------------------------------
80CMessageHandler::CMessageHandler ()
81     : CoinMessageHandler(),
82       model_(NULL),
83       callback_(NULL)
84{
85}
86
87//-------------------------------------------------------------------
88// Copy constructor
89//-------------------------------------------------------------------
90CMessageHandler::CMessageHandler (const CMessageHandler & rhs)
91     : CoinMessageHandler(rhs),
92       model_(rhs.model_),
93       callback_(rhs.callback_)
94{
95}
96
97CMessageHandler::CMessageHandler (const CoinMessageHandler & rhs)
98     : CoinMessageHandler(rhs),
99       model_(NULL),
100       callback_(NULL)
101{
102}
103
104// Constructor with pointer to model
105CMessageHandler::CMessageHandler(Clp_Simplex * model,
106                                 FILE * )
107     : CoinMessageHandler(),
108       model_(model),
109       callback_(NULL)
110{
111}
112
113//-------------------------------------------------------------------
114// Destructor
115//-------------------------------------------------------------------
116CMessageHandler::~CMessageHandler ()
117{
118}
119
120//----------------------------------------------------------------
121// Assignment operator
122//-------------------------------------------------------------------
123CMessageHandler &
124CMessageHandler::operator=(const CMessageHandler& rhs)
125{
126     if (this != &rhs) {
127          CoinMessageHandler::operator=(rhs);
128          model_ = rhs.model_;
129          callback_ = rhs.callback_;
130     }
131     return *this;
132}
133//-------------------------------------------------------------------
134// Clone
135//-------------------------------------------------------------------
136CoinMessageHandler * CMessageHandler::clone() const
137{
138     return new CMessageHandler(*this);
139}
140
141int
142CMessageHandler::print()
143{
144     if (callback_) {
145          int messageNumber = currentMessage().externalNumber();
146          if (currentSource() != "Clp")
147               messageNumber += 1000000;
148          int i;
149          int nDouble = numberDoubleFields();
150          assert (nDouble <= 10);
151          double vDouble[10];
152          for (i = 0; i < nDouble; i++)
153               vDouble[i] = doubleValue(i);
154          int nInt = numberIntFields();
155          assert (nInt <= 10);
156          CoinBigIndex vInt[10];
157          for (i = 0; i < nInt; i++)
158               vInt[i] = intValue(i);
159          int nString = numberStringFields();
160          assert (nString <= 10);
161          char * vString[10];
162          for (i = 0; i < nString; i++) {
163               std::string value = stringValue(i);
164               vString[i] = CoinStrdup(value.c_str());
165          }
166          callback_(model_, messageNumber,
167                    nDouble, vDouble,
168                    nInt, vInt,
169                    nString, vString);
170          for (i = 0; i < nString; i++)
171               free(vString[i]);
172
173     }
174     return CoinMessageHandler::print();
175}
176const Clp_Simplex *
177CMessageHandler::model() const
178{
179     return model_;
180}
181void
182CMessageHandler::setModel(Clp_Simplex * model)
183{
184     model_ = model;
185}
186// Call back
187void
188CMessageHandler::setCallBack(clp_callback callback)
189{
190     callback_ = callback;
191}
192
193#include "Clp_C_Interface.h"
194#include <string>
195#include <stdio.h>
196#include <iostream>
197
198#if defined(__MWERKS__)
199#pragma export on
200#endif
201
202
203COINLIBAPI const char* COINLINKAGE
204Clp_Version(void)
205{
206   return CLP_VERSION;
207}
208COINLIBAPI int COINLINKAGE
209Clp_VersionMajor(void)
210{
211   return CLP_VERSION_MAJOR;
212}
213COINLIBAPI int COINLINKAGE Clp_VersionMinor(void)
214{
215   return CLP_VERSION_MINOR;
216}
217COINLIBAPI int COINLINKAGE Clp_VersionRelease(void)
218{
219   return CLP_VERSION_RELEASE;
220}
221
222/* Default constructor */
223COINLIBAPI Clp_Simplex *  COINLINKAGE
224Clp_newModel()
225{
226     Clp_Simplex * model = new Clp_Simplex;
227     model->model_ = new ClpSimplex();
228     model->handler_ = NULL;
229     return model;
230}
231/* Destructor */
232COINLIBAPI void COINLINKAGE
233Clp_deleteModel(Clp_Simplex * model)
234{
235     delete model->model_;
236     delete model->handler_;
237     delete model;
238}
239
240/* Loads a problem (the constraints on the
241    rows are given by lower and upper bounds). If a pointer is NULL then the
242    following values are the default:
243    <ul>
244    <li> <code>colub</code>: all columns have upper bound infinity
245    <li> <code>collb</code>: all columns have lower bound 0
246    <li> <code>rowub</code>: all rows have upper bound infinity
247    <li> <code>rowlb</code>: all rows have lower bound -infinity
248    <li> <code>obj</code>: all variables have 0 objective coefficient
249    </ul>
250*/
251/* Just like the other loadProblem() method except that the matrix is
252   given in a standard column major ordered format (without gaps). */
253COINLIBAPI void COINLINKAGE
254Clp_loadProblem (Clp_Simplex * model,  const int numcols, const int numrows,
255                 const CoinBigIndex * start, const int* index,
256                 const double* value,
257                 const double* collb, const double* colub,
258                 const double* obj,
259                 const double* rowlb, const double* rowub)
260{
261     const char prefix[] = "Clp_c_Interface::Clp_loadProblem(): ";
262     const int  verbose = 0;
263     if (verbose > 1) {
264          printf("%s numcols = %i, numrows = %i\n",
265                 prefix, numcols, numrows);
266          printf("%s model = %p, start = %p, index = %p, value = %p\n",
267                 prefix, reinterpret_cast<const void *>(model), reinterpret_cast<const void *>(start), reinterpret_cast<const void *>(index), reinterpret_cast<const void *>(value));
268          printf("%s collb = %p, colub = %p, obj = %p, rowlb = %p, rowub = %p\n",
269                 prefix, reinterpret_cast<const void *>(collb), reinterpret_cast<const void *>(colub), reinterpret_cast<const void *>(obj), reinterpret_cast<const void *>(rowlb), reinterpret_cast<const void *>(rowub));
270     }
271     model->model_->loadProblem(numcols, numrows, start, index, value,
272                                collb, colub, obj, rowlb, rowub);
273}
274/* read quadratic part of the objective (the matrix part) */
275COINLIBAPI void COINLINKAGE
276Clp_loadQuadraticObjective(Clp_Simplex * model,
277                           const int numberColumns,
278                           const CoinBigIndex * start,
279                           const int * column,
280                           const double * element)
281{
282
283     model->model_->loadQuadraticObjective(numberColumns,
284                                           start, column, element);
285
286}
287/* Read an mps file from the given filename */
288COINLIBAPI int COINLINKAGE
289Clp_readMps(Clp_Simplex * model, const char *filename,
290            int keepNames,
291            int ignoreErrors)
292{
293     return model->model_->readMps(filename, keepNames != 0, ignoreErrors != 0);
294}
295/* Copy in integer informations */
296COINLIBAPI void COINLINKAGE
297Clp_copyInIntegerInformation(Clp_Simplex * model, const char * information)
298{
299     model->model_->copyInIntegerInformation(information);
300}
301/* Drop integer informations */
302COINLIBAPI void COINLINKAGE
303Clp_deleteIntegerInformation(Clp_Simplex * model)
304{
305     model->model_->deleteIntegerInformation();
306}
307/* Resizes rim part of model  */
308COINLIBAPI void COINLINKAGE
309Clp_resize (Clp_Simplex * model, int newNumberRows, int newNumberColumns)
310{
311     model->model_->resize(newNumberRows, newNumberColumns);
312}
313/* Deletes rows */
314COINLIBAPI void COINLINKAGE
315Clp_deleteRows(Clp_Simplex * model, int number, const int * which)
316{
317     model->model_->deleteRows(number, which);
318}
319/* Add rows */
320COINLIBAPI void COINLINKAGE
321Clp_addRows(Clp_Simplex * model, int number, const double * rowLower,
322            const double * rowUpper,
323            const CoinBigIndex * rowStarts, const int * columns,
324            const double * elements)
325{
326     model->model_->addRows(number, rowLower, rowUpper, rowStarts, columns, elements);
327}
328
329/* Deletes columns */
330COINLIBAPI void COINLINKAGE
331Clp_deleteColumns(Clp_Simplex * model, int number, const int * which)
332{
333     model->model_->deleteColumns(number, which);
334}
335/* Add columns */
336COINLIBAPI void COINLINKAGE
337Clp_addColumns(Clp_Simplex * model, int number, const double * columnLower,
338               const double * columnUpper,
339               const double * objective,
340               const CoinBigIndex * columnStarts, const int * rows,
341               const double * elements)
342{
343     model->model_->addColumns(number, columnLower, columnUpper, objective,
344                               columnStarts, rows, elements);
345}
346/* Change row lower bounds */
347COINLIBAPI void COINLINKAGE
348Clp_chgRowLower(Clp_Simplex * model, const double * rowLower)
349{
350     model->model_->chgRowLower(rowLower);
351}
352/* Change row upper bounds */
353COINLIBAPI void COINLINKAGE
354Clp_chgRowUpper(Clp_Simplex * model, const double * rowUpper)
355{
356     model->model_->chgRowUpper(rowUpper);
357}
358/* Change column lower bounds */
359COINLIBAPI void COINLINKAGE
360Clp_chgColumnLower(Clp_Simplex * model, const double * columnLower)
361{
362     model->model_->chgColumnLower(columnLower);
363}
364/* Change column upper bounds */
365COINLIBAPI void COINLINKAGE
366Clp_chgColumnUpper(Clp_Simplex * model, const double * columnUpper)
367{
368     model->model_->chgColumnUpper(columnUpper);
369}
370/* Change objective coefficients */
371COINLIBAPI void COINLINKAGE
372Clp_chgObjCoefficients(Clp_Simplex * model, const double * objIn)
373{
374     model->model_->chgObjCoefficients(objIn);
375}
376/* Drops names - makes lengthnames 0 and names empty */
377COINLIBAPI void COINLINKAGE
378Clp_dropNames(Clp_Simplex * model)
379{
380     model->model_->dropNames();
381}
382/* Copies in names */
383COINLIBAPI void COINLINKAGE
384Clp_copyNames(Clp_Simplex * model, const char * const * rowNamesIn,
385              const char * const * columnNamesIn)
386{
387     int iRow;
388     std::vector<std::string> rowNames;
389     int numberRows = model->model_->numberRows();
390     rowNames.reserve(numberRows);
391     for (iRow = 0; iRow < numberRows; iRow++) {
392          rowNames.push_back(rowNamesIn[iRow]);
393     }
394
395     int iColumn;
396     std::vector<std::string> columnNames;
397     int numberColumns = model->model_->numberColumns();
398     columnNames.reserve(numberColumns);
399     for (iColumn = 0; iColumn < numberColumns; iColumn++) {
400          columnNames.push_back(columnNamesIn[iColumn]);
401     }
402     model->model_->copyNames(rowNames, columnNames);
403}
404
405/* Number of rows */
406COINLIBAPI int COINLINKAGE
407Clp_numberRows(Clp_Simplex * model)
408{
409     return model->model_->numberRows();
410}
411/* Number of columns */
412COINLIBAPI int COINLINKAGE
413Clp_numberColumns(Clp_Simplex * model)
414{
415     return model->model_->numberColumns();
416}
417/* Primal tolerance to use */
418COINLIBAPI double COINLINKAGE
419Clp_primalTolerance(Clp_Simplex * model)
420{
421     return model->model_->primalTolerance();
422}
423COINLIBAPI void COINLINKAGE
424Clp_setPrimalTolerance(Clp_Simplex * model,  double value)
425{
426     model->model_->setPrimalTolerance(value);
427}
428/* Dual tolerance to use */
429COINLIBAPI double COINLINKAGE
430Clp_dualTolerance(Clp_Simplex * model)
431{
432     return model->model_->dualTolerance();
433}
434COINLIBAPI void COINLINKAGE
435Clp_setDualTolerance(Clp_Simplex * model,  double value)
436{
437     model->model_->setDualTolerance(value);
438}
439/* Dual objective limit */
440COINLIBAPI double COINLINKAGE
441Clp_dualObjectiveLimit(Clp_Simplex * model)
442{
443     return model->model_->dualObjectiveLimit();
444}
445COINLIBAPI void COINLINKAGE
446Clp_setDualObjectiveLimit(Clp_Simplex * model, double value)
447{
448     model->model_->setDualObjectiveLimit(value);
449}
450/* Objective offset */
451COINLIBAPI double COINLINKAGE
452Clp_objectiveOffset(Clp_Simplex * model)
453{
454     return model->model_->objectiveOffset();
455}
456COINLIBAPI void COINLINKAGE
457Clp_setObjectiveOffset(Clp_Simplex * model, double value)
458{
459     model->model_->setObjectiveOffset(value);
460}
461/* Fills in array with problem name  */
462COINLIBAPI void COINLINKAGE
463Clp_problemName(Clp_Simplex * model, int maxNumberCharacters, char * array)
464{
465     std::string name = model->model_->problemName();
466     maxNumberCharacters = CoinMin(maxNumberCharacters,
467                                   ((int) strlen(name.c_str()))+1) ;
468     strncpy(array, name.c_str(), maxNumberCharacters - 1);
469     array[maxNumberCharacters-1] = '\0';
470}
471/* Sets problem name.  Must have \0 at end.  */
472COINLIBAPI int COINLINKAGE
473Clp_setProblemName(Clp_Simplex * model, int /*maxNumberCharacters*/, char * array)
474{
475     return model->model_->setStrParam(ClpProbName, array);
476}
477/* Number of iterations */
478COINLIBAPI int COINLINKAGE
479Clp_numberIterations(Clp_Simplex * model)
480{
481     return model->model_->numberIterations();
482}
483COINLIBAPI void COINLINKAGE
484Clp_setNumberIterations(Clp_Simplex * model, int numberIterations)
485{
486     model->model_->setNumberIterations(numberIterations);
487}
488/* Maximum number of iterations */
489COINLIBAPI int maximumIterations(Clp_Simplex * model)
490{
491     return model->model_->maximumIterations();
492}
493COINLIBAPI void COINLINKAGE
494Clp_setMaximumIterations(Clp_Simplex * model, int value)
495{
496     model->model_->setMaximumIterations(value);
497}
498/* Maximum time in seconds (from when set called) */
499COINLIBAPI double COINLINKAGE
500Clp_maximumSeconds(Clp_Simplex * model)
501{
502     return model->model_->maximumSeconds();
503}
504COINLIBAPI void COINLINKAGE
505Clp_setMaximumSeconds(Clp_Simplex * model, double value)
506{
507     model->model_->setMaximumSeconds(value);
508}
509/* Returns true if hit maximum iteratio`ns (or time) */
510COINLIBAPI int COINLINKAGE
511Clp_hitMaximumIterations(Clp_Simplex * model)
512{
513     return model->model_->hitMaximumIterations() ? 1 : 0;
514}
515/* Status of problem:
516   0 - optimal
517   1 - primal infeasible
518   2 - dual infeasible
519   3 - stopped on iterations etc
520   4 - stopped due to errors
521*/
522COINLIBAPI int COINLINKAGE
523Clp_status(Clp_Simplex * model)
524{
525     return model->model_->status();
526}
527/* Set problem status */
528COINLIBAPI void COINLINKAGE
529Clp_setProblemStatus(Clp_Simplex * model, int problemStatus)
530{
531     model->model_->setProblemStatus(problemStatus);
532}
533/* Secondary status of problem - may get extended
534   0 - none
535   1 - primal infeasible because dual limit reached
536   2 - scaled problem optimal - unscaled has primal infeasibilities
537   3 - scaled problem optimal - unscaled has dual infeasibilities
538   4 - scaled problem optimal - unscaled has both dual and primal infeasibilities
539*/
540COINLIBAPI int COINLINKAGE
541Clp_secondaryStatus(Clp_Simplex * model)
542{
543     return model->model_->secondaryStatus();
544}
545COINLIBAPI void COINLINKAGE
546Clp_setSecondaryStatus(Clp_Simplex * model, int status)
547{
548     model->model_->setSecondaryStatus(status);
549}
550/* Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore */
551COINLIBAPI double COINLINKAGE
552Clp_optimizationDirection(Clp_Simplex * model)
553{
554     return model->model_->optimizationDirection();
555}
556COINLIBAPI void COINLINKAGE
557Clp_setOptimizationDirection(Clp_Simplex * model, double value)
558{
559     model->model_->setOptimizationDirection(value);
560}
561/* Primal row solution */
562COINLIBAPI double * COINLINKAGE
563Clp_primalRowSolution(Clp_Simplex * model)
564{
565     return model->model_->primalRowSolution();
566}
567/* Primal column solution */
568COINLIBAPI double * COINLINKAGE
569Clp_primalColumnSolution(Clp_Simplex * model)
570{
571     return model->model_->primalColumnSolution();
572}
573/* Dual row solution */
574COINLIBAPI double * COINLINKAGE
575Clp_dualRowSolution(Clp_Simplex * model)
576{
577     return model->model_->dualRowSolution();
578}
579/* Reduced costs */
580COINLIBAPI double * COINLINKAGE
581Clp_dualColumnSolution(Clp_Simplex * model)
582{
583     return model->model_->dualColumnSolution();
584}
585/* Row lower */
586COINLIBAPI double* COINLINKAGE
587Clp_rowLower(Clp_Simplex * model)
588{
589     return model->model_->rowLower();
590}
591/* Row upper  */
592COINLIBAPI double* COINLINKAGE
593Clp_rowUpper(Clp_Simplex * model)
594{
595     return model->model_->rowUpper();
596}
597/* Objective */
598COINLIBAPI double * COINLINKAGE
599Clp_objective(Clp_Simplex * model)
600{
601     return model->model_->objective();
602}
603/* Column Lower */
604COINLIBAPI double * COINLINKAGE
605Clp_columnLower(Clp_Simplex * model)
606{
607     return model->model_->columnLower();
608}
609/* Column Upper */
610COINLIBAPI double * COINLINKAGE
611Clp_columnUpper(Clp_Simplex * model)
612{
613     return model->model_->columnUpper();
614}
615/* Number of elements in matrix */
616COINLIBAPI CoinBigIndex COINLINKAGE
617Clp_getNumElements(Clp_Simplex * model)
618{
619     return model->model_->getNumElements();
620}
621// Column starts in matrix
622COINLIBAPI const CoinBigIndex * COINLINKAGE Clp_getVectorStarts(Clp_Simplex * model)
623{
624     CoinPackedMatrix * matrix;
625     matrix = model->model_->matrix();
626     return (matrix == NULL) ? NULL : matrix->getVectorStarts();
627}
628
629// Row indices in matrix
630COINLIBAPI const int * COINLINKAGE Clp_getIndices(Clp_Simplex * model)
631{
632     CoinPackedMatrix * matrix = model->model_->matrix();
633     return (matrix == NULL) ? NULL : matrix->getIndices();
634}
635
636// Column vector lengths in matrix
637COINLIBAPI const int * COINLINKAGE Clp_getVectorLengths(Clp_Simplex * model)
638{
639     CoinPackedMatrix * matrix = model->model_->matrix();
640     return (matrix == NULL) ? NULL : matrix->getVectorLengths();
641}
642
643// Element values in matrix
644COINLIBAPI const double * COINLINKAGE Clp_getElements(Clp_Simplex * model)
645{
646     CoinPackedMatrix * matrix = model->model_->matrix();
647     return (matrix == NULL) ? NULL : matrix->getElements();
648}
649/* Objective value */
650COINLIBAPI double COINLINKAGE
651Clp_objectiveValue(Clp_Simplex * model)
652{
653     return model->model_->objectiveValue();
654}
655/* Integer information */
656COINLIBAPI char * COINLINKAGE
657Clp_integerInformation(Clp_Simplex * model)
658{
659     return model->model_->integerInformation();
660}
661/* Infeasibility/unbounded ray (NULL returned if none/wrong)
662   Up to user to use free() on these arrays.  */
663COINLIBAPI double * COINLINKAGE
664Clp_infeasibilityRay(Clp_Simplex * model)
665{
666     const double * ray = model->model_->internalRay();
667     double * array = NULL;
668     int numberRows = model->model_->numberRows(); 
669     int status = model->model_->status();
670     if (status == 1 && ray) {
671          array = static_cast<double*>(malloc(numberRows*sizeof(double)));
672          memcpy(array,ray,numberRows*sizeof(double));
673#ifdef PRINT_RAY_METHOD
674          printf("Infeasibility ray obtained by algorithm %s\n",model->model_->algorithm()>0 ?
675              "primal" : "dual");
676#endif
677     }
678     return array;
679}
680COINLIBAPI double * COINLINKAGE
681Clp_unboundedRay(Clp_Simplex * model)
682{
683     const double * ray = model->model_->internalRay();
684     double * array = NULL;
685     int numberColumns = model->model_->numberColumns(); 
686     int status = model->model_->status();
687     if (status == 2 && ray) {
688          array = static_cast<double*>(malloc(numberColumns*sizeof(double)));
689          memcpy(array,ray,numberColumns*sizeof(double));
690     }
691     return array;
692}
693COINLIBAPI void COINLINKAGE
694Clp_freeRay(Clp_Simplex * model, double * ray)
695{
696     free(ray);
697}
698/* See if status array exists (partly for OsiClp) */
699COINLIBAPI int COINLINKAGE
700Clp_statusExists(Clp_Simplex * model)
701{
702     return model->model_->statusExists() ? 1 : 0;
703}
704/* Return address of status array (char[numberRows+numberColumns]) */
705COINLIBAPI unsigned char *  COINLINKAGE
706Clp_statusArray(Clp_Simplex * model)
707{
708     return model->model_->statusArray();
709}
710/* Copy in status vector */
711COINLIBAPI void COINLINKAGE
712Clp_copyinStatus(Clp_Simplex * model, const unsigned char * statusArray)
713{
714     model->model_->copyinStatus(statusArray);
715}
716
717/* User pointer for whatever reason */
718COINLIBAPI void COINLINKAGE
719Clp_setUserPointer (Clp_Simplex * model, void * pointer)
720{
721     model->model_->setUserPointer(pointer);
722}
723COINLIBAPI void * COINLINKAGE
724Clp_getUserPointer (Clp_Simplex * model)
725{
726     return model->model_->getUserPointer();
727}
728/* Pass in Callback function */
729COINLIBAPI void COINLINKAGE
730Clp_registerCallBack(Clp_Simplex * model,
731                     clp_callback userCallBack)
732{
733     // Will be copy of users one
734     delete model->handler_;
735     model->handler_ = new CMessageHandler(*(model->model_->messageHandler()));
736     model->handler_->setCallBack(userCallBack);
737     model->handler_->setModel(model);
738     model->model_->passInMessageHandler(model->handler_);
739}
740/* Unset Callback function */
741COINLIBAPI void COINLINKAGE
742Clp_clearCallBack(Clp_Simplex * model)
743{
744     delete model->handler_;
745     model->handler_ = NULL;
746}
747/* Amount of print out:
748   0 - none
749   1 - just final
750   2 - just factorizations
751   3 - as 2 plus a bit more
752   4 - verbose
753   above that 8,16,32 etc just for selective debug
754*/
755COINLIBAPI void COINLINKAGE
756Clp_setLogLevel(Clp_Simplex * model, int value)
757{
758     model->model_->setLogLevel(value);
759}
760COINLIBAPI int COINLINKAGE
761Clp_logLevel(Clp_Simplex * model)
762{
763     return model->model_->logLevel();
764}
765/* length of names (0 means no names0 */
766COINLIBAPI int COINLINKAGE
767Clp_lengthNames(Clp_Simplex * model)
768{
769     return model->model_->lengthNames();
770}
771/* Fill in array (at least lengthNames+1 long) with a row name */
772COINLIBAPI void COINLINKAGE
773Clp_rowName(Clp_Simplex * model, int iRow, char * name)
774{
775     std::string rowName = model->model_->rowName(iRow);
776     strcpy(name, rowName.c_str());
777}
778/* Fill in array (at least lengthNames+1 long) with a column name */
779COINLIBAPI void COINLINKAGE
780Clp_columnName(Clp_Simplex * model, int iColumn, char * name)
781{
782     std::string columnName = model->model_->columnName(iColumn);
783     strcpy(name, columnName.c_str());
784}
785
786/* General solve algorithm which can do presolve.
787   See  ClpSolve.hpp for options
788*/
789COINLIBAPI int COINLINKAGE
790Clp_initialSolve(Clp_Simplex * model)
791{
792     return model->model_->initialSolve();
793}
794/* Pass solve options. (Exception to direct analogue rule) */
795COINLIBAPI int COINLINKAGE
796Clp_initialSolveWithOptions(Clp_Simplex * model, Clp_Solve * s)
797{
798     return model->model_->initialSolve(s->options);
799}
800/* Barrier initial solve */
801COINLIBAPI int COINLINKAGE
802Clp_initialBarrierSolve(Clp_Simplex * model0)
803{
804     ClpSimplex *model = model0->model_;
805
806     return model->initialBarrierSolve();
807
808}
809/* Barrier initial solve */
810COINLIBAPI int COINLINKAGE
811Clp_initialBarrierNoCrossSolve(Clp_Simplex * model0)
812{
813     ClpSimplex *model = model0->model_;
814
815     return model->initialBarrierNoCrossSolve();
816
817}
818/* Dual initial solve */
819COINLIBAPI int COINLINKAGE
820Clp_initialDualSolve(Clp_Simplex * model)
821{
822     return model->model_->initialDualSolve();
823}
824/* Primal initial solve */
825COINLIBAPI int COINLINKAGE
826Clp_initialPrimalSolve(Clp_Simplex * model)
827{
828     return model->model_->initialPrimalSolve();
829}
830/* Dual algorithm - see ClpSimplexDual.hpp for method */
831COINLIBAPI int COINLINKAGE
832Clp_dual(Clp_Simplex * model, int ifValuesPass)
833{
834     return model->model_->dual(ifValuesPass);
835}
836/* Primal algorithm - see ClpSimplexPrimal.hpp for method */
837COINLIBAPI int COINLINKAGE
838Clp_primal(Clp_Simplex * model, int ifValuesPass)
839{
840     return model->model_->primal(ifValuesPass);
841}
842/* Sets or unsets scaling, 0 -off, 1 equilibrium, 2 geometric, 3, auto, 4 dynamic(later) */
843COINLIBAPI void COINLINKAGE
844Clp_scaling(Clp_Simplex * model, int mode)
845{
846     model->model_->scaling(mode);
847}
848/* Gets scalingFlag */
849COINLIBAPI int COINLINKAGE
850Clp_scalingFlag(Clp_Simplex * model)
851{
852     return model->model_->scalingFlag();
853}
854/* Crash - at present just aimed at dual, returns
855   -2 if dual preferred and crash basis created
856   -1 if dual preferred and all slack basis preferred
857   0 if basis going in was not all slack
858   1 if primal preferred and all slack basis preferred
859   2 if primal preferred and crash basis created.
860
861   if gap between bounds <="gap" variables can be flipped
862
863   If "pivot" is
864   0 No pivoting (so will just be choice of algorithm)
865   1 Simple pivoting e.g. gub
866   2 Mini iterations
867*/
868COINLIBAPI int COINLINKAGE
869Clp_crash(Clp_Simplex * model, double gap, int pivot)
870{
871     return model->model_->crash(gap, pivot);
872}
873/* If problem is primal feasible */
874COINLIBAPI int COINLINKAGE
875Clp_primalFeasible(Clp_Simplex * model)
876{
877     return model->model_->primalFeasible() ? 1 : 0;
878}
879/* If problem is dual feasible */
880COINLIBAPI int COINLINKAGE
881Clp_dualFeasible(Clp_Simplex * model)
882{
883     return model->model_->dualFeasible() ? 1 : 0;
884}
885/* Dual bound */
886COINLIBAPI double COINLINKAGE
887Clp_dualBound(Clp_Simplex * model)
888{
889     return model->model_->dualBound();
890}
891COINLIBAPI void COINLINKAGE
892Clp_setDualBound(Clp_Simplex * model, double value)
893{
894     model->model_->setDualBound(value);
895}
896/* Infeasibility cost */
897COINLIBAPI double COINLINKAGE
898Clp_infeasibilityCost(Clp_Simplex * model)
899{
900     return model->model_->infeasibilityCost();
901}
902COINLIBAPI void COINLINKAGE
903Clp_setInfeasibilityCost(Clp_Simplex * model, double value)
904{
905     model->model_->setInfeasibilityCost(value);
906}
907/* Perturbation:
908   50  - switch on perturbation
909   100 - auto perturb if takes too long (1.0e-6 largest nonzero)
910   101 - we are perturbed
911   102 - don't try perturbing again
912   default is 100
913   others are for playing
914*/
915COINLIBAPI int COINLINKAGE
916Clp_perturbation(Clp_Simplex * model)
917{
918     return model->model_->perturbation();
919}
920COINLIBAPI void COINLINKAGE
921Clp_setPerturbation(Clp_Simplex * model, int value)
922{
923     model->model_->setPerturbation(value);
924}
925/* Current (or last) algorithm */
926COINLIBAPI int COINLINKAGE
927Clp_algorithm(Clp_Simplex * model)
928{
929     return model->model_->algorithm();
930}
931/* Set algorithm */
932COINLIBAPI void COINLINKAGE
933Clp_setAlgorithm(Clp_Simplex * model, int value)
934{
935     model->model_->setAlgorithm(value);
936}
937/* Sum of dual infeasibilities */
938COINLIBAPI double COINLINKAGE
939Clp_sumDualInfeasibilities(Clp_Simplex * model)
940{
941     return model->model_->sumDualInfeasibilities();
942}
943/* Number of dual infeasibilities */
944COINLIBAPI int COINLINKAGE
945Clp_numberDualInfeasibilities(Clp_Simplex * model)
946{
947     return model->model_->numberDualInfeasibilities();
948}
949/* Sum of primal infeasibilities */
950COINLIBAPI double COINLINKAGE
951Clp_sumPrimalInfeasibilities(Clp_Simplex * model)
952{
953     return model->model_->sumPrimalInfeasibilities();
954}
955/* Number of primal infeasibilities */
956COINLIBAPI int COINLINKAGE
957Clp_numberPrimalInfeasibilities(Clp_Simplex * model)
958{
959     return model->model_->numberPrimalInfeasibilities();
960}
961/* Save model to file, returns 0 if success.  This is designed for
962   use outside algorithms so does not save iterating arrays etc.
963   It does not save any messaging information.
964   Does not save scaling values.
965   It does not know about all types of virtual functions.
966*/
967COINLIBAPI int COINLINKAGE
968Clp_saveModel(Clp_Simplex * model, const char * fileName)
969{
970     return model->model_->saveModel(fileName);
971}
972/* Restore model from file, returns 0 if success,
973   deletes current model */
974COINLIBAPI int COINLINKAGE
975Clp_restoreModel(Clp_Simplex * model, const char * fileName)
976{
977     return model->model_->restoreModel(fileName);
978}
979
980/* Just check solution (for external use) - sets sum of
981   infeasibilities etc */
982COINLIBAPI void COINLINKAGE
983Clp_checkSolution(Clp_Simplex * model)
984{
985     model->model_->checkSolution();
986}
987/* Number of rows */
988COINLIBAPI int COINLINKAGE
989Clp_getNumRows(Clp_Simplex * model)
990{
991     return model->model_->getNumRows();
992}
993/* Number of columns */
994COINLIBAPI int COINLINKAGE
995Clp_getNumCols(Clp_Simplex * model)
996{
997     return model->model_->getNumCols();
998}
999/* Number of iterations */
1000COINLIBAPI int COINLINKAGE
1001Clp_getIterationCount(Clp_Simplex * model)
1002{
1003     return model->model_->getIterationCount();
1004}
1005/* Are there a numerical difficulties? */
1006COINLIBAPI int COINLINKAGE
1007Clp_isAbandoned(Clp_Simplex * model)
1008{
1009     return model->model_->isAbandoned() ? 1 : 0;
1010}
1011/* Is optimality proven? */
1012COINLIBAPI int COINLINKAGE
1013Clp_isProvenOptimal(Clp_Simplex * model)
1014{
1015     return model->model_->isProvenOptimal() ? 1 : 0;
1016}
1017/* Is primal infeasiblity proven? */
1018COINLIBAPI int COINLINKAGE
1019Clp_isProvenPrimalInfeasible(Clp_Simplex * model)
1020{
1021     return model->model_->isProvenPrimalInfeasible() ? 1 : 0;
1022}
1023/* Is dual infeasiblity proven? */
1024COINLIBAPI int COINLINKAGE
1025Clp_isProvenDualInfeasible(Clp_Simplex * model)
1026{
1027     return model->model_->isProvenDualInfeasible() ? 1 : 0;
1028}
1029/* Is the given primal objective limit reached? */
1030COINLIBAPI int COINLINKAGE
1031Clp_isPrimalObjectiveLimitReached(Clp_Simplex * model)
1032{
1033     return model->model_->isPrimalObjectiveLimitReached() ? 1 : 0;
1034}
1035/* Is the given dual objective limit reached? */
1036COINLIBAPI int COINLINKAGE
1037Clp_isDualObjectiveLimitReached(Clp_Simplex * model)
1038{
1039     return model->model_->isDualObjectiveLimitReached() ? 1 : 0;
1040}
1041/* Iteration limit reached? */
1042COINLIBAPI int COINLINKAGE
1043Clp_isIterationLimitReached(Clp_Simplex * model)
1044{
1045     return model->model_->isIterationLimitReached() ? 1 : 0;
1046}
1047/* Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore */
1048COINLIBAPI double COINLINKAGE
1049Clp_getObjSense(Clp_Simplex * model)
1050{
1051     return model->model_->getObjSense();
1052}
1053/* Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore */
1054COINLIBAPI void COINLINKAGE
1055Clp_setObjSense(Clp_Simplex * model, double objsen)
1056{
1057     model->model_->setOptimizationDirection(objsen);
1058}
1059/* Primal row solution */
1060COINLIBAPI const double * COINLINKAGE
1061Clp_getRowActivity(Clp_Simplex * model)
1062{
1063     return model->model_->getRowActivity();
1064}
1065/* Primal column solution */
1066COINLIBAPI const double * COINLINKAGE
1067Clp_getColSolution(Clp_Simplex * model)
1068{
1069     return model->model_->getColSolution();
1070}
1071COINLIBAPI void COINLINKAGE
1072Clp_setColSolution(Clp_Simplex * model, const double * input)
1073{
1074     model->model_->setColSolution(input);
1075}
1076/* Dual row solution */
1077COINLIBAPI const double * COINLINKAGE
1078Clp_getRowPrice(Clp_Simplex * model)
1079{
1080     return model->model_->getRowPrice();
1081}
1082/* Reduced costs */
1083COINLIBAPI const double * COINLINKAGE
1084Clp_getReducedCost(Clp_Simplex * model)
1085{
1086     return model->model_->getReducedCost();
1087}
1088/* Row lower */
1089COINLIBAPI const double* COINLINKAGE
1090Clp_getRowLower(Clp_Simplex * model)
1091{
1092     return model->model_->getRowLower();
1093}
1094/* Row upper  */
1095COINLIBAPI const double* COINLINKAGE
1096Clp_getRowUpper(Clp_Simplex * model)
1097{
1098     return model->model_->getRowUpper();
1099}
1100/* Objective */
1101COINLIBAPI const double * COINLINKAGE
1102Clp_getObjCoefficients(Clp_Simplex * model)
1103{
1104     return model->model_->getObjCoefficients();
1105}
1106/* Column Lower */
1107COINLIBAPI const double * COINLINKAGE
1108Clp_getColLower(Clp_Simplex * model)
1109{
1110     return model->model_->getColLower();
1111}
1112/* Column Upper */
1113COINLIBAPI const double * COINLINKAGE
1114Clp_getColUpper(Clp_Simplex * model)
1115{
1116     return model->model_->getColUpper();
1117}
1118/* Objective value */
1119COINLIBAPI double COINLINKAGE
1120Clp_getObjValue(Clp_Simplex * model)
1121{
1122     return model->model_->getObjValue();
1123}
1124/* Get variable basis info */
1125COINLIBAPI int COINLINKAGE
1126Clp_getColumnStatus(Clp_Simplex * model, int sequence)
1127{
1128     return (int) model->model_->getColumnStatus(sequence);
1129}
1130/* Get row basis info */
1131COINLIBAPI int COINLINKAGE
1132Clp_getRowStatus(Clp_Simplex * model, int sequence)
1133{
1134     return (int) model->model_->getRowStatus(sequence);
1135}
1136/* Set variable basis info */
1137COINLIBAPI void COINLINKAGE
1138Clp_setColumnStatus(Clp_Simplex * model, int sequence, int value)
1139{
1140     if (value >= 0 && value <= 5) {
1141          model->model_->setColumnStatus(sequence, (ClpSimplex::Status) value );
1142          if (value == 3 || value == 5)
1143               model->model_->primalColumnSolution()[sequence] =
1144                    model->model_->columnLower()[sequence];
1145          else if (value == 2)
1146               model->model_->primalColumnSolution()[sequence] =
1147                    model->model_->columnUpper()[sequence];
1148     }
1149}
1150/* Set row basis info */
1151COINLIBAPI void COINLINKAGE
1152Clp_setRowStatus(Clp_Simplex * model, int sequence, int value)
1153{
1154     if (value >= 0 && value <= 5) {
1155          model->model_->setRowStatus(sequence, (ClpSimplex::Status) value );
1156          if (value == 3 || value == 5)
1157               model->model_->primalRowSolution()[sequence] =
1158                    model->model_->rowLower()[sequence];
1159          else if (value == 2)
1160               model->model_->primalRowSolution()[sequence] =
1161                    model->model_->rowUpper()[sequence];
1162     }
1163}
1164/* Small element value - elements less than this set to zero,
1165   default is 1.0e-20 */
1166COINLIBAPI double COINLINKAGE
1167Clp_getSmallElementValue(Clp_Simplex * model)
1168{
1169     return model->model_->getSmallElementValue();
1170}
1171COINLIBAPI void COINLINKAGE
1172Clp_setSmallElementValue(Clp_Simplex * model, double value)
1173{
1174     model->model_->setSmallElementValue(value);
1175}
1176/* Print model */
1177COINLIBAPI void COINLINKAGE
1178Clp_printModel(Clp_Simplex * model, const char * prefix)
1179{
1180     ClpSimplex *clp_simplex = model->model_;
1181     int numrows    = clp_simplex->numberRows();
1182     int numcols    = clp_simplex->numberColumns();
1183     CoinBigIndex numelem    = clp_simplex->getNumElements();
1184     const CoinBigIndex *start = clp_simplex->matrix()->getVectorStarts();
1185     const int *length = clp_simplex->matrix()->getVectorLengths();
1186     const int *index     = clp_simplex->matrix()->getIndices();
1187     const double *value  = clp_simplex->matrix()->getElements();
1188     const double *collb  = model->model_->columnLower();
1189     const double *colub  = model->model_->columnUpper();
1190     const double *obj    = model->model_->objective();
1191     const double *rowlb  = model->model_->rowLower();
1192     const double *rowub  = model->model_->rowUpper();
1193     printf("%s numcols = %i, numrows = %i, numelem = %i\n",
1194            prefix, numcols, numrows, numelem);
1195     printf("%s model = %p, start = %p, index = %p, value = %p\n",
1196            prefix, reinterpret_cast<const void *>(model), reinterpret_cast<const void *>(start), reinterpret_cast<const void *>(index), reinterpret_cast<const void *>(value));
1197     clp_simplex->matrix()->dumpMatrix(NULL);
1198     {
1199          int i;
1200          for (i = 0; i <= numcols; i++)
1201               printf("%s start[%i] = %i\n", prefix, i, start[i]);
1202          // may be gaps
1203          for (i = 0; i < numcols; i++) {
1204            for (CoinBigIndex j=start[i];j<start[i]+length[i];j++)
1205               printf("%s index[%i] = %i, value[%i] = %g\n",
1206                      prefix, j, index[j], j, value[j]);
1207          }
1208     }
1209
1210     printf("%s collb = %p, colub = %p, obj = %p, rowlb = %p, rowub = %p\n",
1211            prefix, reinterpret_cast<const void *>(collb), reinterpret_cast<const void *>(colub), reinterpret_cast<const void *>(obj), reinterpret_cast<const void *>(rowlb), reinterpret_cast<const void *>(rowub));
1212     printf("%s optimization direction = %g\n", prefix, Clp_optimizationDirection(model));
1213     printf("  (1 - minimize, -1 - maximize, 0 - ignore)\n");
1214     {
1215          int i;
1216          for (i = 0; i < numcols; i++)
1217               printf("%s collb[%i] = %g, colub[%i] = %g, obj[%i] = %g\n",
1218                      prefix, i, collb[i], i, colub[i], i, obj[i]);
1219          for (i = 0; i < numrows; i++)
1220               printf("%s rowlb[%i] = %g, rowub[%i] = %g\n",
1221                      prefix, i, rowlb[i], i, rowub[i]);
1222     }
1223}
1224
1225#ifndef SLIM_CLP
1226/** Solve the problem with the idiot code */
1227/* tryhard values:
1228   tryhard & 7:
1229      0: NOT lightweight, 105 iterations within a pass (when mu stays fixed)
1230      1: lightweight, but focus more on optimality (mu is high)
1231         (23 iters in a pass)
1232      2: lightweight, but focus more on feasibility (11 iters in a pass)
1233      3: lightweight, but focus more on feasibility (23 iters in a pass, so it
1234         goes closer to opt than option 2)
1235   tryhard >> 3:
1236      number of passes, the larger the number the closer it gets to optimality
1237*/
1238COINLIBAPI void COINLINKAGE
1239Clp_idiot(Clp_Simplex * model, int tryhard)
1240{
1241     ClpSimplex *clp = model->model_;
1242     Idiot info(*clp);
1243     int numberpass = tryhard >> 3;
1244     int lightweight = tryhard & 7;
1245     info.setLightweight(lightweight);
1246     info.crash(numberpass, clp->messageHandler(), clp->messagesPointer(), false);
1247}
1248#endif
1249
1250COINLIBAPI Clp_Solve * COINLINKAGE
1251ClpSolve_new() 
1252{ 
1253    return new Clp_Solve(); 
1254}
1255
1256COINLIBAPI void COINLINKAGE
1257ClpSolve_delete(Clp_Solve * solve) 
1258{ 
1259    delete solve; 
1260}
1261
1262// space- and error-saving macros
1263#define ClpSolveGetIntProperty(prop) \
1264COINLIBAPI int COINLINKAGE \
1265ClpSolve_ ## prop (Clp_Solve *s) \
1266{ \
1267    return s->options.prop(); \
1268}
1269
1270#define ClpSolveSetIntProperty(prop) \
1271COINLIBAPI void COINLINKAGE \
1272ClpSolve_ ## prop (Clp_Solve *s, int val) \
1273{ \
1274    s->options.prop(val); \
1275}
1276
1277COINLIBAPI void COINLINKAGE
1278ClpSolve_setSpecialOption(Clp_Solve * s, int which, int value, int extraInfo) 
1279{
1280    s->options.setSpecialOption(which,value,extraInfo);
1281}
1282
1283COINLIBAPI int COINLINKAGE
1284ClpSolve_getSpecialOption(Clp_Solve * s, int which)
1285{
1286    return s->options.getSpecialOption(which);
1287}
1288
1289COINLIBAPI void COINLINKAGE
1290ClpSolve_setSolveType(Clp_Solve * s, int method, int extraInfo)
1291{
1292    s->options.setSolveType(static_cast<ClpSolve::SolveType>(method), extraInfo);
1293}
1294
1295ClpSolveGetIntProperty(getSolveType)
1296
1297COINLIBAPI void COINLINKAGE ClpSolve_setPresolveType(Clp_Solve * s, int amount, int extraInfo)
1298{
1299    s->options.setPresolveType(static_cast<ClpSolve::PresolveType>(amount),extraInfo);
1300}
1301
1302ClpSolveGetIntProperty(getPresolveType)
1303
1304ClpSolveGetIntProperty(getPresolvePasses)
1305
1306
1307COINLIBAPI int COINLINKAGE
1308ClpSolve_getExtraInfo(Clp_Solve * s, int which) {
1309     return s->options.getExtraInfo(which);
1310}
1311
1312ClpSolveSetIntProperty(setInfeasibleReturn)
1313ClpSolveGetIntProperty(infeasibleReturn)
1314
1315ClpSolveGetIntProperty(doDual)
1316ClpSolveSetIntProperty(setDoDual)
1317
1318ClpSolveGetIntProperty(doSingleton)
1319ClpSolveSetIntProperty(setDoSingleton)
1320
1321ClpSolveGetIntProperty(doDoubleton)
1322ClpSolveSetIntProperty(setDoDoubleton)
1323
1324ClpSolveGetIntProperty(doTripleton)
1325ClpSolveSetIntProperty(setDoTripleton)
1326
1327ClpSolveGetIntProperty(doTighten)
1328ClpSolveSetIntProperty(setDoTighten)
1329
1330ClpSolveGetIntProperty(doForcing)
1331ClpSolveSetIntProperty(setDoForcing)
1332
1333ClpSolveGetIntProperty(doImpliedFree)
1334ClpSolveSetIntProperty(setDoImpliedFree)
1335
1336ClpSolveGetIntProperty(doDupcol)
1337ClpSolveSetIntProperty(setDoDupcol)
1338
1339ClpSolveGetIntProperty(doDuprow)
1340ClpSolveSetIntProperty(setDoDuprow)
1341
1342ClpSolveGetIntProperty(doSingletonColumn)
1343ClpSolveSetIntProperty(setDoSingletonColumn)
1344
1345ClpSolveGetIntProperty(presolveActions)
1346ClpSolveSetIntProperty(setPresolveActions)
1347
1348ClpSolveGetIntProperty(substitution)
1349ClpSolveSetIntProperty(setSubstitution)
1350
1351#if defined(__MWERKS__)
1352#pragma export off
1353#endif
1354
Note: See TracBrowser for help on using the repository browser.