COPASI API  4.16.103
CQOptimizationWidget.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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) 2008 - 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 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQOptimizationWidget.h"
16 
17 #include <QtCore/QVariant>
18 
19 #include "UI/CQTaskBtnWidget.h"
20 #include "UI/CQTaskHeaderWidget.h"
21 #include "CQTaskMethodWidget.h"
22 #include "CQTaskMethodWidget.h"
23 #include "UI/CQFittingItemWidget.h"
24 #include "UI/CProgressBar.h"
26 #include "UI/qtUtilities.h"
27 #include "UI/CQExpressionWidget.h"
28 
29 #include "report/CKeyFactory.h"
30 #include "optimization/COptTask.h"
31 #include "optimization/COptItem.h"
36 
37 /*
38  * Constructs a CQOptimizationWidget which is a child of 'parent', with the
39  * name 'name'.'
40  */
41 CQOptimizationWidget::CQOptimizationWidget(QWidget* parent, const char* name)
42  : TaskWidget(parent, name)
43 {
44  setupUi(this);
45 
46  init();
47 }
48 
49 /*
50  * Destroys the object and frees any allocated resources
51  */
53 {
54  destroy();
55  // no need to delete child widgets, Qt does it all for us
56 }
57 
59 {
60  COptTask * pTask =
61  dynamic_cast< COptTask * >(mpTask);
62 
63  if (!pTask) return false;
64 
65  saveCommon();
66  saveMethod();
67 
68  COptProblem * pProblem =
69  dynamic_cast<COptProblem *>(mpTask->getProblem());
70 
71  if (!pProblem) return false;
72 
73  // expression
74  if (pProblem->getObjectiveFunction() != mpExpressionEMW->mpExpressionWidget->getExpression())
75  {
76  if (!pProblem->setObjectiveFunction(mpExpressionEMW->mpExpressionWidget->getExpression()))
77  {
79  return false;
80  }
81 
82  mChanged = true;
83  }
84 
85  if (mpBoxSubtask->currentText() != FROM_UTF8(CCopasiTask::TypeName[pProblem->getSubtaskType()]))
86  {
87  mChanged = true;
88  pProblem->setSubtaskType((CCopasiTask::Type) mSubtaskMap[TO_UTF8(mpBoxSubtask->currentText())]);
89  }
90 
91  if (mpBtnMaximize->isChecked() != pProblem->maximize())
92  {
93  mChanged = true;
94  pProblem->setMaximize(mpBtnMaximize->isChecked());
95  }
96 
97  if (mpCheckRandomize->isChecked() != pProblem->getRandomizeStartValues())
98  {
99  mChanged = true;
100  pProblem->setRandomizeStartValues(mpCheckRandomize->isChecked());
101  }
102 
103  if (mpCheckStatistics->isChecked() != pProblem->getCalculateStatistics())
104  {
105  mChanged = true;
106  pProblem->setCalculateStatistics(mpCheckStatistics->isChecked());
107  }
108 
109  mChanged |= mpParameters->save(NULL, NULL);
110  mChanged |= mpConstraints->save(NULL, NULL);
111 
112  if (mChanged)
113  {
114  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
115  (*CCopasiRootContainer::getDatamodelList())[0]->changed();
116  }
117 
118  mChanged = false;
119 
120  return true;
121 }
122 
124 {
125  COptTask * pTask =
126  dynamic_cast< COptTask * >(mpTask);
127 
128  if (!pTask) return false;
129 
130  loadCommon();
131  loadMethod();
132 
133  COptProblem * pProblem =
134  dynamic_cast<COptProblem *>(mpTask->getProblem());
135 
136  if (!pProblem) return false;
137 
138  // expression
139  mpExpressionEMW->mpExpressionWidget->setExpression(pProblem->getObjectiveFunction());
140  mpExpressionEMW->updateWidget();
141 
142  mpBtnMaximize->setChecked(pProblem->maximize());
143  mpBtnMinimize->setChecked(!pProblem->maximize());
144 
145  mpCheckRandomize->setChecked(pProblem->getRandomizeStartValues());
146  mpCheckStatistics->setChecked(pProblem->getCalculateStatistics());
147 
148  mpBoxSubtask->setCurrentIndex(mpBoxSubtask->findText(FROM_UTF8(CCopasiTask::TypeName[pProblem->getSubtaskType()])));
149 
150  mpParameters->load(mpDataModel, pProblem->getGroup("OptimizationItemList"), NULL, NULL);
151 
152  mpConstraints->load(mpDataModel, pProblem->getGroup("OptimizationConstraintList"), NULL, NULL);
153 
154  mChanged = false;
155 
156  return true;
157 }
158 
160 {
161  // --- expression
162 
163  mpExpressionEMW->updateWidget();
164 
165  // ----
166 
167  COptTask * pTask =
168  dynamic_cast< COptTask * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
169 
170  if (!pTask) return false;
171 
172  if (!commonBeforeRunTask()) return false;
173 
174  return commonRunTask();
175 }
176 
177 void CQOptimizationWidget::slotPageChange(QWidget * currentPage)
178 {
179  if (mpTabWidget->tabText(mpTabWidget->indexOf(currentPage)).contains("Parameters", Qt::CaseSensitive))
180  mpCurrentList = mpParameters;
181  else
182  mpCurrentList = mpConstraints;
183 }
184 
186 {return COptMethod::createMethod(type);}
187 
189 {
190  QString TabLabel = "Parameters (" + QString::number(number) + ")";
191  mpTabWidget->setTabText(mpTabWidget->indexOf(mpParameters), TabLabel);
192 }
193 
195 {
196  QString TabLabel = "Constraints (" + QString::number(number) + ")";
197  mpTabWidget->setTabText(mpTabWidget->indexOf(mpConstraints), TabLabel);
198 }
199 
201 {
202  std::string taskName = "Optimization";
203 
204  mpHeaderWidget->setTaskName(taskName);
205 
206  verticalLayout->insertWidget(0, mpHeaderWidget);
207  // verticalLayout->insertSpacing(1, 14); // space between header and body
208 
211  verticalLayout->addWidget(mpMethodWidget);
212 
213  verticalLayout->addWidget(mpBtnWidget);
214 
215  mpExpressionEMW->mpExpressionWidget->setExpressionType(CQExpressionWidget::ObjectiveFunctions);
216 
217  //mpBtnMaximize->setMinimumWidth(mpLblExpression->width());
218  const unsigned int * pSubtask = COptProblem::ValidSubtasks;
219 
220  for (; *pSubtask != CCopasiTask::unset; ++pSubtask)
221  {
222  mpBoxSubtask->insertItem(mpBoxSubtask->count(), FROM_UTF8(CCopasiTask::TypeName[*pSubtask]));
223  mSubtaskMap[CCopasiTask::TypeName[*pSubtask]] = *pSubtask;
224  }
225 
226  mpParameters->setItemType(CQFittingItemWidget::OPT_ITEM);
227 
228  connect(mpParameters, SIGNAL(numberChanged(int)), this, SLOT(slotParameterNumberChanged(int)));
229 
230  mpConstraints->setItemType(CQFittingItemWidget::OPT_CONSTRAINT);
231 
232  connect(mpConstraints, SIGNAL(numberChanged(int)), this, SLOT(slotConstraintNumberChanged(int)));
233 
234  mpCurrentList = mpParameters;
235 }
236 
238 {}
239 
241 {
242  mpBtnWidget->mpBtnRun->setEnabled(valid);
243 }
244 
245 /*!
246  The slot to activate the editor page of type CQExpressionWidget
247  for being able to type a new mathematical expression or edit the existing one
248  */
250 {}
251 
252 void CQOptimizationWidget::slotSubtaskChanged(const QString & /* subtask */)
253 {}
void slotPageChange(QWidget *currentPage)
CCopasiTask::Type getSubtaskType() const
bool saveMethod()
Definition: TaskWidget.cpp:308
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
static const std::string TypeName[]
Definition: CCopasiTask.h:67
CCopasiProblem * getProblem()
bool loadCommon()
Definition: TaskWidget.cpp:247
CCopasiObject * get(const std::string &key)
static COptMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::RandomSearch)
Definition: COptMethod.cpp:50
#define MCOptimization
void slotConstraintNumberChanged(int number)
bool saveCommon()
Definition: TaskWidget.cpp:258
bool loadMethod()
Definition: TaskWidget.cpp:284
const bool & getRandomizeStartValues() const
virtual CCopasiMethod * createMethod(const CCopasiMethod::SubType &type)
bool setTaskName(const std::string &name)
static const unsigned int ValidMethods[]
Definition: COptTask.h:50
const bool & maximize() const
bool mChanged
Definition: TaskWidget.h:133
CTSSATask * pTask
void slotExpressionValid(bool valid)
std::map< std::string, C_INT32 > mSubtaskMap
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CQOptimizationWidget(QWidget *parent=0, const char *name=0)
CCopasiTask * mpTask
Definition: TaskWidget.h:130
void showMethodParameters(const bool &show)
CQTaskMethodWidget * mpMethodWidget
Definition: TaskWidget.h:123
void slotSubtaskChanged(const QString &subtask)
static const unsigned int ValidSubtasks[]
Definition: COptProblem.h:57
CCopasiDataModel * mpDataModel
Definition: copasiWidget.h:65
void setRandomizeStartValues(const bool &randomize)
CQFittingItemWidget * mpCurrentList
static CKeyFactory * getKeyFactory()
bool commonRunTask()
Definition: TaskWidget.cpp:395
CQTaskBtnWidget * mpBtnWidget
Definition: TaskWidget.h:124
CQTaskHeaderWidget * mpHeaderWidget
Definition: TaskWidget.h:122
const std::string getObjectiveFunction()
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
const bool & getCalculateStatistics() const
bool setSubtaskType(const CCopasiTask::Type &subtaskType)
CCopasiParameterGroup * getGroup(const std::string &name)
void setMaximize(const bool &maximize)
bool commonBeforeRunTask()
Definition: TaskWidget.cpp:317
std::string mKey
Definition: copasiWidget.h:63
void slotParameterNumberChanged(int number)
void setCalculateStatistics(const bool &calculate)
bool setObjectiveFunction(const std::string &infix)
void setValidMethods(const unsigned int *validMethods)