source: stable/1.10/Clp/src/MyMessageHandler.cpp @ 1404

Last change on this file since 1404 was 1404, checked in by tkr, 11 years ago

Merging r1375:1393 from stable/BSP to stable/1.10

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1// Copyright (C) 2003, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#if defined(_MSC_VER)
5#pragma warning(disable:4786)
6#pragma warning(disable:4503)
7#endif
8
9#include <cstdio>
10
11#include "ClpSimplex.hpp"
12#include "ClpNonLinearCost.hpp"
13#include "MyMessageHandler.hpp"
14#include "ClpMessage.hpp"
15
16
17//#############################################################################
18// Constructors / Destructor / Assignment
19//#############################################################################
20
21//-------------------------------------------------------------------
22// Default Constructor
23//-------------------------------------------------------------------
24MyMessageHandler::MyMessageHandler () 
25  : CoinMessageHandler(),
26    model_(NULL),
27    feasibleExtremePoints_(),
28    iterationNumber_(-1)
29{
30}
31
32//-------------------------------------------------------------------
33// Copy constructor
34//-------------------------------------------------------------------
35MyMessageHandler::MyMessageHandler (const MyMessageHandler & rhs) 
36: CoinMessageHandler(rhs),
37    model_(rhs.model_),
38    feasibleExtremePoints_(rhs.feasibleExtremePoints_),
39    iterationNumber_(rhs.iterationNumber_)
40{ 
41}
42
43MyMessageHandler::MyMessageHandler (const CoinMessageHandler & rhs) 
44  : CoinMessageHandler(),
45    model_(NULL),
46    feasibleExtremePoints_(),
47    iterationNumber_(-1)
48{ 
49}
50
51// Constructor with pointer to model
52MyMessageHandler::MyMessageHandler(ClpSimplex * model,
53               FILE * userPointer)
54  : CoinMessageHandler(),
55    model_(model),
56    feasibleExtremePoints_(),
57    iterationNumber_(-1)
58{
59}
60
61//-------------------------------------------------------------------
62// Destructor
63//-------------------------------------------------------------------
64MyMessageHandler::~MyMessageHandler ()
65{
66}
67
68//----------------------------------------------------------------
69// Assignment operator
70//-------------------------------------------------------------------
71MyMessageHandler &
72MyMessageHandler::operator=(const MyMessageHandler& rhs)
73{
74  if (this != &rhs) {
75    CoinMessageHandler::operator=(rhs);
76    model_ = rhs.model_;
77    feasibleExtremePoints_ = rhs.feasibleExtremePoints_;
78    iterationNumber_ = rhs.iterationNumber_;
79  }
80  return *this;
81}
82//-------------------------------------------------------------------
83// Clone
84//-------------------------------------------------------------------
85CoinMessageHandler * MyMessageHandler::clone() const
86{
87  return new MyMessageHandler(*this);
88}
89
90int 
91MyMessageHandler::print()
92{
93  if (currentSource()=="Clp") {
94    if (currentMessage().externalNumber()==102) {
95      printf("There are %d primal infeasibilities\n",
96             model_->nonLinearCost()->numberInfeasibilities());
97      // Feasibility
98      if (!model_->nonLinearCost()->numberInfeasibilities()) {
99        // Column solution
100        int numberColumns = model_->numberColumns();
101        const double * solution = model_->solutionRegion(1);
102
103        // Create vector to contain solution
104        StdVectorDouble feasibleExtremePoint;
105
106        const double *objective = model_->objective();
107        double objectiveValue = 0;
108       
109        if (!model_->columnScale()) {
110          // No scaling
111          for (int i=0;i<numberColumns;i++) {
112            feasibleExtremePoint.push_back(solution[i]);
113            objectiveValue += solution[i]*objective[i];
114          }
115        } else {
116          // scaled
117          const double * columnScale = model_->columnScale();
118          for (int i=0;i<numberColumns;i++) {
119            feasibleExtremePoint.push_back(solution[i]*columnScale[i]);
120            objectiveValue += solution[i]*objective[i]*columnScale[i];
121          }
122        }
123        std::cout << "Objective "<< objectiveValue << std::endl;
124        // Save solution
125        feasibleExtremePoints_.push_front(feasibleExtremePoint);
126
127        // Want maximum of 10 solutions, so if more then 10 get rid of oldest
128        size_t numExtremePointsSaved = feasibleExtremePoints_.size();
129        if ( numExtremePointsSaved>=10 ) {
130          feasibleExtremePoints_.pop_back();
131          assert( feasibleExtremePoints_.size() + 1 == numExtremePointsSaved );
132        };
133
134      }
135      return 0; // skip printing
136    }
137  }
138
139  // If one wants access to the message text,
140  // it is available using method messageBuffer().
141  // For example, one could code:
142  // std::cout <<messageBuffer() <<std::endl;
143
144  return CoinMessageHandler::print();
145}
146const ClpSimplex *
147MyMessageHandler::model() const
148{
149  return model_;
150}
151void 
152MyMessageHandler::setModel(ClpSimplex * model)
153{
154  model_ = model;
155}
156
157const std::deque<StdVectorDouble> & MyMessageHandler::getFeasibleExtremePoints() const
158{ 
159  return feasibleExtremePoints_; 
160}
161void MyMessageHandler::clearFeasibleExtremePoints()
162{
163  feasibleExtremePoints_.clear();
164}
Note: See TracBrowser for help on using the repository browser.