source: branches/pre/Test/MyMessageHandler.cpp @ 192

Last change on this file since 192 was 192, checked in by forrest, 18 years ago

For Yiming

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 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        int numExtremePointsSaved = feasibleExtremePoints_.size();
129        if ( numExtremePointsSaved>=10 ) {
130          feasibleExtremePoints_.pop_back();
131          assert( feasibleExtremePoints_.size() == 
132                  (unsigned int) numExtremePointsSaved-1 );
133        };
134
135      }
136      return 0; // skip printing
137    }
138  }
139  return CoinMessageHandler::print();
140}
141const ClpSimplex *
142MyMessageHandler::model() const
143{
144  return model_;
145}
146void 
147MyMessageHandler::setModel(ClpSimplex * model)
148{
149  model_ = model;
150}
151
152const std::deque<StdVectorDouble> & MyMessageHandler::getFeasibleExtremePoints() const
153{ 
154  return feasibleExtremePoints_; 
155}
156void MyMessageHandler::clearFeasibleExtremePoints()
157{
158  feasibleExtremePoints_.clear();
159}
Note: See TracBrowser for help on using the repository browser.