My Project  0.0.16
QUCS Mapping
 All Classes Namespaces 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 
18 #include <qdir.h>
19 #include <qfile.h>
20 #include <qtextstream.h>
21 #include <qregexp.h>
22 #include <qstring.h>
23 #include <qstringlist.h>
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, QPen::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 extern QDir QucsHomeDir;
79 
80 // -----------------------------------------------------------
82 {
83  Property* pp;
84  QFile afile(QucsHomeDir.filePath("asco_netlist.cfg"));
85  if(afile.open(IO_WriteOnly)) {
86  QTextStream stream(&afile);
87  stream << "*\n";
88  stream << "* ASCO configuration file for '" << Name << "'\n";
89  stream << "*\n\n";
90 
91  stream << "#Optimization Flow#\n";
92  stream << "Alter:no\n";
93  stream << "MonteCarlo:no\n";
94  stream << "AlterMC cost:0.00\n";
95  stream << "ExecuteRF:no\n";
96  stream << "#\n\n";
97 
98  stream << "#DE#\n";
99  pp = Props.at(1);
100  QString val;
101  val = pp->Value.section('|',0,0);
102  stream << "choice of method:" << val << "\n";
103  val = pp->Value.section('|',1,1);
104  stream << "maximum no. of iterations:" << val << "\n";
105  val = pp->Value.section('|',2,2);
106  stream << "Output refresh cycle:" << val << "\n";
107  val = pp->Value.section('|',3,3);
108  stream << "No. of parents NP:" << val << "\n";
109  val= pp->Value.section('|',4,4);
110  stream << "Constant F:" << val << "\n";
111  val = pp->Value.section('|',5,5);
112  stream << "Crossing Over factor CR:" << val << "\n";
113  val = pp->Value.section('|',6,6);
114  stream << "Seed for pseudo random number generator:" << val << "\n";
115  val = pp->Value.section('|',7,7);
116  stream << "Minimum Cost Variance:" << val << "\n";
117  val = pp->Value.section('|',8,8);
118  stream << "Cost objectives:" << val << "\n";
119  val = pp->Value.section('|',9,9);
120  stream << "Cost constraints:" << val << "\n";
121  stream << "#\n\n";
122 
123  stream << "# Parameters #\n";
124  int i=1;
125  for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
126  if(pp->Name == "Var") {
127  stream << "Parameter " << i << ":";
128  val = pp->Value.section('|',0,0);
129  stream << "#" << val << "#" << ":";
130  val = pp->Value.section('|',2,2);
131  stream << val << ":";
132  val = pp->Value.section('|',3,3);
133  stream << val << ":";
134  val = pp->Value.section('|',4,4);
135  stream << val << ":";
136  val = pp->Value.section('|',5,5);
137  stream << val << ":";
138  val = pp->Value.section('|',1,1);
139  stream << ((val == "yes") ? "OPT" : "---") << "\n";
140  }
141  }
142  stream << "#\n\n";
143 
144  stream << "# Measurements #\n";
145  for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
146  if(pp->Name == "Goal") {
147  val = pp->Value.section('|',1,1);
148  QString Type, Value;
149  Value = pp->Value.section('|',2,2);
150  if (val == "MIN" || val == "MAX" || val == "MON") {
151  Value = "---";
152  }
153  Type = val;
154  val = pp->Value.section('|',0,0);
155  stream << val << ":"
156  << "---" << ":"
157  << Type << ":" << Value << "\n";
158  }
159  }
160  stream << "#\n\n";
161 
162  stream << "# Post Processing #\n";
163  stream << "#\n\n";
164 
165  afile.close();
166  } else return false;
167 
168  QDir ExtractDir(QucsHomeDir);
169  if(!ExtractDir.cd("extract")) {
170  if(!ExtractDir.mkdir("extract"))
171  return false;
172  if(!ExtractDir.cd("extract"))
173  return false;
174  }
175 
176  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
177  if(pp->Name == "Goal") {
178  QString VarName = pp->Value.section('|',0,0);
179  QFile efile(ExtractDir.filePath(VarName));
180  if(efile.open(IO_WriteOnly)) {
181  QTextStream stream(&efile);
182  stream << "# Info #\n";
183  stream << "#\n\n";
184  stream << "# Commands #\n";
185  stream << "#\n\n";
186  stream << "# Post Processing #\n";
187  stream << "MEASURE_VAR:#SYMBOL#:SEARCH_FOR:'<indep " << VarName
188  << "':S_COL:01:P_LINE:01:P_COL:01:31" << "\n";
189  stream << "#\n\n";
190  efile.close();
191  }
192  else return false;
193  }
194  }
195  return true;
196 }
197 
198 // -----------------------------------------------------------
200 {
201  Property* pp;
202  QStringList vars;
203  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
204  if(pp->Name == "Var") {
205  vars += pp->Value.section('|',0,0);
206  }
207  }
208 
209  QFile infile(QucsHomeDir.filePath("netlist.txt"));
210  QFile outfile(QucsHomeDir.filePath("asco_netlist.txt"));
211  if(!infile.open(IO_ReadOnly)) return false;
212  if(!outfile.open(IO_WriteOnly)) return false;
213  QTextStream instream(&infile);
214  QTextStream outstream(&outfile);
215  QString Line;
216  while(!instream.atEnd()) {
217  Line = instream.readLine();
218  for(QStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
219  QRegExp reg = QRegExp("=\"(" + *it + ")\"");
220  Line.replace(reg, "=\"#\\1#\"");
221  }
222  outstream << Line << "\n";
223  }
224  outfile.close();
225  infile.close();
226  return true;
227 }
228 
229 // -----------------------------------------------------------
231 {
232  bool changed = false;
233  Property* pp;
234  QStringList vars;
235  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
236  if(pp->Name == "Var") {
237  vars += pp->Value.section('|',0,0);
238  }
239  }
240 
241  QFile infile(QucsHomeDir.filePath("asco_out.log"));
242  if(!infile.open(IO_ReadOnly)) return false;
243  QTextStream instream(&infile);
244  QString Line;
245  while(!instream.atEnd()) Line = instream.readLine();
246  infile.close();
247 
248  QStringList entries = QStringList::split(':',Line);
249  QStringList::Iterator it;
250  for(it = entries.begin(); it != entries.end(); ++it ) {
251  QString Name = *it;
252  Name = Name.stripWhiteSpace();
253  if(vars.contains(Name)) {
254  for(pp = Props.at(2); pp != 0; pp = Props.next()) {
255  if(pp->Name == "Var") {
256  QString val[6];
257  val[0] = pp->Value.section('|',0,0);
258  if(val[0]==Name) {
259  val[1] = pp->Value.section('|',1,1);
260  val[2] = pp->Value.section('|',2,2);
261  val[3] = pp->Value.section('|',3,3);
262  val[4] = pp->Value.section('|',4,4);
263  val[5] = pp->Value.section('|',5,5);
264  ++it;
265  QString Value = *it;
266  Value = Value.stripWhiteSpace();
267  val[2] = Value;
268  pp->Value = val[0] + "|" + val[1] + "|" + val[2] + "|" +
269  val[3] + "|" + val[4] + "|" + val[5];
270  changed = true;
271  break;
272  }
273  }
274  }
275  }
276  }
277  return changed;
278 }