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