COPASI API  4.16.103
CQOptimizationResult.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 #include "CQOptimizationResult.h"
12 
13 #include "CopasiFileDialog.h"
14 #include "CQMessageBox.h"
15 #include "qtUtilities.h"
16 
17 #include "copasi.h"
18 
19 #include "optimization/COptTask.h"
21 #include "optimization/COptItem.h"
24 #include "model/CModel.h"
25 
26 /*
27  * Constructs a CQOptimizationResult which is a child of 'parent', with the
28  * name 'name'.'
29  */
30 CQOptimizationResult::CQOptimizationResult(QWidget* parent, const char* name)
31  : CopasiWidget(parent, name)
32 {
33  setupUi(this);
34 
35  init();
36 }
37 
38 /*
39  * Destroys the object and frees any allocated resources
40  */
42 {
43  // no need to delete child widgets, Qt does it all for us
44 }
45 
47 {}
48 
50  ListViews::Action /* action */,
51  const std::string & /* key */)
52 {
53  // :TODO:
54  return true;
55 }
56 
58 {
59  // :TODO:
60  return true;
61 }
62 
64 {
65  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
67  assert(pDataModel != NULL);
68 
69  mpTask =
70  dynamic_cast<COptTask *>((*(*CCopasiRootContainer::getDatamodelList())[0]->getTaskList())["Optimization"]);
71 
72  if (!mpTask) return false;
73 
74  mpProblem = dynamic_cast<const COptProblem *>(mpTask->getProblem());
75 
76  if (!mpProblem) return false;
77 
78  // Objective Value
79  mpEditObjectiveValue->setText(QString::number(mpProblem->getSolutionValue()));
80 
81  // Performance Statistics
82  const unsigned C_INT32 & FunctionEvaluations = mpProblem->getFunctionEvaluations();
83  mpEditEvaluations->setText(QString::number(FunctionEvaluations));
84  const C_FLOAT64 & ExecutionTime = mpProblem->getExecutionTime();
85  mpEditCPUTime->setText(QString::number(ExecutionTime));
86  mpEditSpeed->setText(QString::number(FunctionEvaluations / ExecutionTime));
87 
88  size_t i, imax;
89 
90  // Loop over the optimization items
91  const std::vector< COptItem * > & Items = mpProblem->getOptItemList();
94 
95  imax = Items.size();
96  QTableWidgetItem * pItem;
97 
99  imax = 0;
100 
101  mpParameters->setRowCount((int) imax);
102 
103  QColor BackgroundColor = mpParameters->palette().brush(QPalette::Active, QPalette::Base).color();
104 
105  int h, s, v;
106  BackgroundColor.getHsv(&h, &s, &v);
107 
108  if (s < 20)
109  {
110  s = 20;
111  }
112 
113  BackgroundColor.setHsv(0, s, v);
114 
115  for (i = 0; i != imax; i++)
116  {
117  //1st column: parameter name
118  const CCopasiObject *pObject =
119  pDataModel->getDataObject(Items[i]->getObjectCN());
120 
121  if (pObject)
122  pItem = new QTableWidgetItem(FROM_UTF8(pObject->getObjectDisplayName()));
123  else
124  pItem = new QTableWidgetItem("Not Found");
125 
126  mpParameters->setItem((int) i, 0, pItem);
127 
128  const C_FLOAT64 & Solution = Solutions[i];
129 
130  //2nd column: lower bound
131  pItem = new QTableWidgetItem(FROM_UTF8(Items[i]->getLowerBound()));
132  mpParameters->setItem((int) i, 1, pItem);
133 
134  if (1.01 * *Items[i]->getLowerBoundValue() > Solution)
135  {
136  pItem->setBackgroundColor(BackgroundColor);
137  }
138 
139  //3rd column: start value
140  pItem = new QTableWidgetItem(QString::number(Items[i]->getLastStartValue()));
141  pItem->setForeground(QColor(120, 120, 140));
142  mpParameters->setItem((int) i, 2, pItem);
143 
144  //4th column: solution value
145  pItem = new QTableWidgetItem(QString::number(Solution));
146  mpParameters->setItem((int) i, 3, pItem);
147 
148  //5th column: upper bound
149  pItem = new QTableWidgetItem(FROM_UTF8(Items[i]->getUpperBound()));
150  mpParameters->setItem((int) i, 4, pItem);
151 
152  if (0.99 * *Items[i]->getUpperBoundValue() < Solution)
153  {
154  pItem->setBackgroundColor(BackgroundColor);
155  }
156 
157  pItem = new QTableWidgetItem(QString::number(Gradients[i]));
158  mpParameters->setItem((int) i, 5, pItem);
159  }
160 
161  mpParameters->resizeColumnsToContents();
162  mpParameters->resizeRowsToContents();
163 
164  return true;
165 }
166 
168 {
169  C_INT32 Answer = QMessageBox::No;
170  QString fileName;
171 
172  while (Answer == QMessageBox::No)
173  {
174  fileName =
175  CopasiFileDialog::getSaveFileName(this, "Save File Dialog",
176  "untitled.txt", "TEXT Files (*.txt)", "Save to");
177 
178  if (fileName.isEmpty()) return;
179 
180  // Checks whether the file exists
181  Answer = checkSelection(fileName);
182 
183  if (Answer == QMessageBox::Cancel) return;
184  }
185 
186  std::ofstream file(CLocaleString::fromUtf8(TO_UTF8(fileName)).c_str());
187 
188  if (file.fail()) return;
189 
190  size_t i, imax;
191 
192  // The global result and statistics
193  file << "Objective Value" << std::endl;
194  file << mpProblem->getSolutionValue() << std::endl;
195 
196  file << "Function Evaluations\tCPU Time [s]\tEvaluations/second [1/s]" << std::endl;
197  const size_t & FunctionEvaluations = mpProblem->getFunctionEvaluations();
198  const C_FLOAT64 & ExecutionTime = mpProblem->getExecutionTime();
199  file << FunctionEvaluations << "\t";
200  file << ExecutionTime << "\t";
201  file << FunctionEvaluations / ExecutionTime << std::endl << std::endl;
202 
203  // Set up the parameters table
204  file << "Parameters:" << std::endl;
205  file << "Parameter\tLower Bound\tStart Value\tValue\tUpper Bound\tGradient" << std::endl;
206 
207  // Loop over the fitted values objects
208  imax = mpParameters->rowCount();
209 
210  for (i = 0; i != imax; i++)
211  {
212  file << TO_UTF8(mpParameters->item((int) i, 0)->text()) << "\t";
213  file << TO_UTF8(mpParameters->item((int) i, 1)->text()) << "\t";
214  file << TO_UTF8(mpParameters->item((int) i, 2)->text()) << "\t";
215  file << TO_UTF8(mpParameters->item((int) i, 3)->text()) << "\t";
216  file << TO_UTF8(mpParameters->item((int) i, 4)->text()) << "\t";
217  file << TO_UTF8(mpParameters->item((int) i, 5)->text()) << std::endl;
218  }
219 
220  file << std::endl;
221 }
222 
224 {
225  const_cast< COptProblem * >(mpProblem)->restoreModel(true);
226 }
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
CCopasiProblem * getProblem()
const CVector< C_FLOAT64 > & getVariableGradients() const
const COptProblem * mpProblem
#define C_INT32
Definition: copasi.h:90
const C_FLOAT64 & getSolutionValue() const
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
const C_FLOAT64 & getExecutionTime() const
const std::vector< COptItem * > & getOptItemList() const
C_INT32 checkSelection(const QString &file)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
const CVector< C_FLOAT64 > & getSolutionVariables() const
#define C_FLOAT64
Definition: copasi.h:92
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
static CLocaleString fromUtf8(const std::string &utf8)
CQOptimizationResult(QWidget *parent=0, const char *name=0)
const unsigned C_INT32 & getFunctionEvaluations() const
static QString getSaveFileName(QWidget *parent=0, const char *name=0, const QString &startWith=QString::null, const QString &filter=QString::null, const QString &caption=QString::null, QString *pSelectedFilter=NULL, QFileDialog::Options options=0)