Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opt_sim.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  opt_sim.cpp
3  -------------
4  begin : Sat Jul 22 2006
5  copyright : (C) 2006 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <QtGui>
18 #include <QDir>
19 #include <QFile>
20 #include <QTextStream>
21 #include <QRegExp>
22 #include <QString>
23 #include <QStringList>
24 
25 #include "opt_sim.h"
26 #include "main.h"
27 
28 
30 {
31  Description = QObject::tr("Optimization");
32 
33  Texts.append(new Text(0, 0, Description, Qt::darkBlue, QucsSettings.largeFontSize));
34 
35  x1 = -10; y1 = -9;
36  x2 = x1+128; y2 = y1+41;
37 
38  tx = 0;
39  ty = y2+1;
40  Model = ".Opt";
41  Name = "Opt";
42 
43  Props.append(new Property("Sim", "", false, ""));
44  Props.append(new Property("DE", "3|50|2|20|0.85|1|3|1e-6|10|100", false, ""));
45 }
46 
48 {
49 }
50 
52 {
53  return new Optimize_Sim();
54 }
55 
56 Element* Optimize_Sim::info(QString& Name, char* &BitmapFile, bool getNewOne)
57 {
58  Name = QObject::tr("optimization");
59  BitmapFile = (char *) "optimize";
60 
61  if(getNewOne) return new Optimize_Sim();
62  return 0;
63 }
64 
65 // -------------------------------------------------------
67 {
68  QString s = "#\n";
69  if (createASCOFiles()) {
70  s += "# ASCO configuration file(s) created\n";
71  } else {
72  s += "# Failed to create ASCO configuration file(s)\n";
73  }
74  s += "#\n\n";
75  return s;
76 }
77 
78 
79 // -----------------------------------------------------------
81 {
82  Property* pp;
83  QFile afile(QucsSettings.QucsHomeDir.filePath("asco_netlist.cfg"));
84  if(afile.open(QIODevice::WriteOnly)) {
85  QTextStream stream(&afile);
86  stream << "*\n";
87  stream << "* ASCO configuration file for '" << Name << "'\n";
88  stream << "*\n\n";
89 
90  stream << "#Optimization Flow#\n";
91  stream << "Alter:no\n";
92  stream << "MonteCarlo:no\n";
93  stream << "AlterMC cost:0.00\n";
94  stream << "ExecuteRF:no\n";
95  stream << "#\n\n";
96 
97  stream << "#DE#\n";
98  pp = Props.at(1);
99  QString val;
100  val = pp->Value.section('|',0,0);
101  stream << "choice of method:" << val << "\n";
102  val = pp->Value.section('|',1,1);
103  stream << "maximum no. of iterations:" << val << "\n";
104  val = pp->Value.section('|',2,2);
105  stream << "Output refresh cycle:" << val << "\n";
106  val = pp->Value.section('|',3,3);
107  stream << "No. of parents NP:" << val << "\n";
108  val= pp->Value.section('|',4,4);
109  stream << "Constant F:" << val << "\n";
110  val = pp->Value.section('|',5,5);
111  stream << "Crossing Over factor CR:" << val << "\n";
112  val = pp->Value.section('|',6,6);
113  stream << "Seed for pseudo random number generator:" << val << "\n";
114  val = pp->Value.section('|',7,7);
115  stream << "Minimum Cost Variance:" << val << "\n";
116  val = pp->Value.section('|',8,8);
117  stream << "Cost objectives:" << val << "\n";
118  val = pp->Value.section('|',9,9);
119  stream << "Cost constraints:" << val << "\n";
120  stream << "#\n\n";
121 
122  stream << "# Parameters #\n";
123  int i=1;
124  for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
125  if(pp->Name == "Var") {
126  stream << "Parameter " << i << ":";
127  val = pp->Value.section('|',0,0);
128  stream << "#" << val << "#" << ":";
129  val = pp->Value.section('|',2,2);
130  stream << val << ":";
131  val = pp->Value.section('|',3,3);
132  stream << val << ":";
133  val = pp->Value.section('|',4,4);
134  stream << val << ":";
135  val = pp->Value.section('|',5,5);
136  stream << val << ":";
137  val = pp->Value.section('|',1,1);
138  stream << ((val == "yes") ? "OPT" : "---") << "\n";
139  }
140  }
141  stream << "#\n\n";
142 
143  stream << "# Measurements #\n";
144  for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
145  if(pp->Name == "Goal") {
146  val = pp->Value.section('|',1,1);
147  QString Type, Value;
148  Value = pp->Value.section('|',2,2);
149  if (val == "MIN" || val == "MAX" || val == "MON") {
150  Value = "---";
151  }
152  Type = val;
153  val = pp->Value.section('|',0,0);
154  stream << val << ":"
155  << "---" << ":"
156  << Type << ":" << Value << "\n";
157  }
158  }
159  stream << "#\n\n";
160 
161  stream << "# Post Processing #\n";
162  stream << "#\n\n";
163 
164  afile.close();
165  } else return false;
166 
167  QDir ExtractDir(QucsSettings.QucsHomeDir);
168  if(!ExtractDir.cd("extract")) {
169  if(!ExtractDir.mkdir("extract"))
170  return false;
171  if(!ExtractDir.cd("extract"))
172  return false;
173  }
174 
175  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
176  if(pp->Name == "Goal") {
177  QString VarName = pp->Value.section('|',0,0);
178  QFile efile(ExtractDir.filePath(VarName));
179  if(efile.open(QIODevice::WriteOnly)) {
180  QTextStream stream(&efile);
181  stream << "# Info #\n";
182  stream << "#\n\n";
183  stream << "# Commands #\n";
184  stream << "#\n\n";
185  stream << "# Post Processing #\n";
186  stream << "MEASURE_VAR:#SYMBOL#:SEARCH_FOR:'<indep " << VarName
187  << "':S_COL:01:P_LINE:01:P_COL:01:31" << "\n";
188  stream << "#\n\n";
189  efile.close();
190  }
191  else return false;
192  }
193  }
194  return true;
195 }
196 
197 // -----------------------------------------------------------
204 {
205  Property* pp;
206  QStringList vars;
207  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
208  if(pp->Name == "Var") {
209  vars += pp->Value.section('|',0,0);
210  }
211  }
212 
213  QFile infile(QucsSettings.QucsHomeDir.filePath("netlist.txt"));
214  QFile outfile(QucsSettings.QucsHomeDir.filePath("asco_netlist.txt"));
215  if(!infile.open(QIODevice::ReadOnly)) return false;
216  if(!outfile.open(QIODevice::WriteOnly)) return false;
217  QTextStream instream(&infile);
218  QTextStream outstream(&outfile);
219  QString Line;
220  while(!instream.atEnd()) {
221  Line = instream.readLine();
222  for(QStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
223  if(Line.contains("Eqn:"))
224  {
225  QStringList splitLine = Line.split("\"");
226 
227  for(int i=1;i<splitLine.size()-3;i+=2)
228  {
229  if(splitLine[i].compare("yes")!=0 && splitLine[i].compare("no")!=0) //ignore last piece between quotes
230  {
231  splitLine[i].replace(*it, "#"+*it+"#");
232  }
233  }
234  Line = splitLine.join("\"");
235 
236  }
237  else
238  {
239  QRegExp reg = QRegExp("=\"(" + *it + ")\"");
240  Line.replace(reg, "=\"#\\1#\"");
241  }
242 
243  }
244  outstream << Line << "\n";
245  }
246  outfile.close();
247  infile.close();
248  return true;
249 }
250 
251 // -----------------------------------------------------------
253 {
254  bool changed = false;
255  Property* pp;
256  QStringList vars;
257  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
258  if(pp->Name == "Var") {
259  vars += pp->Value.section('|',0,0);
260  }
261  }
262 
263  QFile infile(QucsSettings.QucsHomeDir.filePath("asco_out.log"));
264  if(!infile.open(QIODevice::ReadOnly)) return false;
265  QTextStream instream(&infile);
266  QString Line;
267  while(!instream.atEnd()) Line = instream.readLine();
268  infile.close();
269 
270  QStringList entries = QStringList::split(':',Line);
271  QStringList::Iterator it;
272  for(it = entries.begin(); it != entries.end(); ++it ) {
273  QString Name = *it;
274  Name = Name.stripWhiteSpace();
275  if(vars.contains(Name)) {
276  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
277  if(pp->Name == "Var") {
278  QString val[6];
279  val[0] = pp->Value.section('|',0,0);
280  if(val[0]==Name) {
281  val[1] = pp->Value.section('|',1,1);
282  val[2] = pp->Value.section('|',2,2);
283  val[3] = pp->Value.section('|',3,3);
284  val[4] = pp->Value.section('|',4,4);
285  val[5] = pp->Value.section('|',5,5);
286  ++it;
287  QString Value = *it;
288  Value = Value.stripWhiteSpace();
289  val[2] = Value;
290  pp->Value = val[0] + "|" + val[1] + "|" + val[2] + "|" +
291  val[3] + "|" + val[4] + "|" + val[5];
292  changed = true;
293  break;
294  }
295  }
296  }
297  }
298  }
299  return changed;
300 }
int Type
Definition: element.h:152
int y1
Definition: element.h:153
Component * newOne()
Definition: opt_sim.cpp:51
tQucsSettings QucsSettings
Definition: main.cpp:52
int tx
Definition: component.h:78
static Element * info(QString &, char *&, bool getNewOne=false)
Definition: opt_sim.cpp:56
int y2
Definition: element.h:153
int x1
Definition: element.h:153
bool createASCOnetlist()
Optimize_Sim::createASCOnetlist create ASCO netlist out or input input netlist.
Definition: opt_sim.cpp:203
bool loadASCOout()
Definition: opt_sim.cpp:252
Definitions and declarations for the main application.
int ty
Definition: component.h:78
QString Value
Definition: element.h:97
Q3PtrList< Property > Props
Definition: component.h:72
Definition: element.h:82
bool createASCOFiles()
Definition: opt_sim.cpp:80
QString netlist()
Definition: opt_sim.cpp:66
Definition: element.h:48
Superclass of all schematic drawing elements.
Definition: element.h:142
QDir QucsHomeDir
Definition: main.h:66
QString Name
Definition: element.h:97
QString Name
Definition: component.h:80
Q3PtrList< Text > Texts
Definition: component.h:71
QString Model
Definition: component.h:80
QString Description
Definition: component.h:81
float largeFontSize
Definition: main.h:47
int x2
Definition: element.h:153