source: trunk/Samples/ClpQuadInterface.cpp @ 12

Last change on this file since 12 was 12, checked in by forrest, 15 years ago

MIQP example

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1// Copyright (C) 2004, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include <cassert>
5
6#include "CoinHelperFunctions.hpp"
7//#include "CoinIndexedVector.hpp"
8#include "ClpQuadraticObjective.hpp"
9#include "ClpLinearObjective.hpp"
10#include "ClpObjective.hpp"
11//#include "ClpSimplex.hpp"
12#include "ClpQuadInterface.hpp"
13//#############################################################################
14// Solve methods
15//#############################################################################
16void ClpQuadInterface::initialSolve()
17{
18  // save cutoff
19  double cutoff = modelPtr_->dualObjectiveLimit();
20  modelPtr_->setDualObjectiveLimit(1.0e50);
21  modelPtr_->scaling(0);
22  modelPtr_->setLogLevel(0);
23  // solve with no objective to get feasible solution
24  setBasis(basis_,modelPtr_);
25  modelPtr_->dual();
26  basis_ = getBasis(modelPtr_);
27  modelPtr_->setDualObjectiveLimit(cutoff);
28  if (modelPtr_->problemStatus()) {
29    assert (modelPtr_->problemStatus()==1);
30    return;
31  }
32  ClpObjective * saveObjective  = modelPtr_->objectiveAsObject();
33  modelPtr_->setObjectivePointer(quadraticObjective_);
34  // Could load up any data into a solver
35  modelPtr_->primal();
36  modelPtr_->setDualObjectiveLimit(cutoff);
37  if (modelPtr_->objectiveValue()>cutoff)
38    modelPtr_->setProblemStatus(1);
39  // zero reduced costs
40  // Should not have to as convex
41  //CoinZeroN(modelPtr_->dualRowSolution(),modelPtr_->numberRows());
42  //CoinZeroN(modelPtr_->dualColumnSolution(),modelPtr_->numberColumns());
43  modelPtr_->setObjectivePointer(saveObjective);
44}
45
46//-----------------------------------------------------------------------------
47void ClpQuadInterface::resolve()
48{
49  initialSolve();
50}
51
52//#############################################################################
53// Constructors, destructors clone and assignment
54//#############################################################################
55
56//-------------------------------------------------------------------
57// Default Constructor
58//-------------------------------------------------------------------
59ClpQuadInterface::ClpQuadInterface ()
60  : OsiClpSolverInterface()
61{
62  quadraticObjective_=NULL;
63}
64
65//-------------------------------------------------------------------
66// Clone
67//-------------------------------------------------------------------
68OsiSolverInterface * 
69ClpQuadInterface::clone(bool CopyData) const
70{
71  if (CopyData) {
72    return new ClpQuadInterface(*this);
73  } else {
74    printf("warning ClpQuadInterface clone with copyData false\n");
75    return new ClpQuadInterface();
76  }
77}
78
79
80//-------------------------------------------------------------------
81// Copy constructor
82//-------------------------------------------------------------------
83ClpQuadInterface::ClpQuadInterface (
84                  const ClpQuadInterface & rhs)
85  : OsiClpSolverInterface(rhs)
86{
87  if (rhs.quadraticObjective_)
88    quadraticObjective_=rhs.quadraticObjective_->clone();
89  else
90    quadraticObjective_=NULL;
91}
92
93//-------------------------------------------------------------------
94// Destructor
95//-------------------------------------------------------------------
96ClpQuadInterface::~ClpQuadInterface ()
97{
98  delete quadraticObjective_;
99}
100
101//-------------------------------------------------------------------
102// Assignment operator
103//-------------------------------------------------------------------
104ClpQuadInterface &
105ClpQuadInterface::operator=(const ClpQuadInterface& rhs)
106{
107  if (this != &rhs) { 
108    OsiClpSolverInterface::operator=(rhs);
109    if (rhs.quadraticObjective_)
110      quadraticObjective_=rhs.quadraticObjective_->clone();
111    else
112      quadraticObjective_=NULL;
113  }
114  return *this;
115}
116//-------------------------------------------------------------------
117// Real initializer
118//-------------------------------------------------------------------
119void
120ClpQuadInterface::initialize ()
121{
122  // Save true objective and create a fake one
123  delete quadraticObjective_;
124  quadraticObjective_ = modelPtr_->objectiveAsObject();
125  ClpLinearObjective * linearObjective = new ClpLinearObjective(NULL,modelPtr_->numberColumns());
126  modelPtr_->setObjectivePointer(linearObjective);
127}
128// Get objective function value (can't use default)
129double 
130ClpQuadInterface::getObjValue() const
131{
132  // first try easy way
133  return modelPtr_->objectiveValue();
134}
Note: See TracBrowser for help on using the repository browser.