Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/components/opt_sim.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                                opt_sim.cpp
00003                               -------------
00004     begin                : Sat Jul 22 2006
00005     copyright            : (C) 2006 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include <QDir>
00018 #include <QFile>
00019 #include <QTextStream>
00020 #include <QRegExp>
00021 #include <QString>
00022 #include <QStringList>
00023 
00024 #include "opt_sim.h"
00025 #include "main.h"
00026 
00027 
00028 Optimize_Sim::Optimize_Sim()
00029 {
00030   Description = QObject::tr("Optimization");
00031 
00032   Texts.append(new Text(0, 0, Description, Qt::darkBlue, QucsSettings.largeFontSize));
00033 
00034   x1 = -10; y1 = -9;
00035   x2 = x1+128; y2 = y1+41;
00036 
00037   tx = 0;
00038   ty = y2+1;
00039   Model = ".Opt";
00040   Name  = "Opt";
00041 
00042   Props.append(new Property("Sim", "", false, ""));
00043   Props.append(new Property("DE", "3|50|2|20|0.85|1|3|1e-6|10|100", false, ""));
00044 }
00045 
00046 Optimize_Sim::~Optimize_Sim()
00047 {
00048 }
00049 
00050 Component* Optimize_Sim::newOne()
00051 {
00052   return new Optimize_Sim();
00053 }
00054 
00055 Element* Optimize_Sim::info(QString& Name, char* &BitmapFile, bool getNewOne)
00056 {
00057   Name = QObject::tr("optimization");
00058   BitmapFile = (char *) "optimize";
00059 
00060   if(getNewOne)  return new Optimize_Sim();
00061   return 0;
00062 }
00063 
00064 // -------------------------------------------------------
00065 QString Optimize_Sim::netlist()
00066 {
00067   QString s = "#\n";
00068   if (createASCOFiles()) {
00069     s += "# ASCO configuration file(s) created\n";
00070   } else {
00071     s += "# Failed to create ASCO configuration file(s)\n";
00072   }
00073   s += "#\n\n";
00074   return s;
00075 }
00076 
00077  
00078 // -----------------------------------------------------------
00079 bool Optimize_Sim::createASCOFiles()
00080 {
00081   Property* pp;
00082   QFile afile(QucsSettings.QucsHomeDir.filePath("asco_netlist.cfg"));
00083   if(afile.open(QIODevice::WriteOnly)) {
00084     QTextStream stream(&afile);
00085     stream << "*\n";
00086     stream << "* ASCO configuration file for '" << Name << "'\n";
00087     stream << "*\n\n";
00088 
00089     stream << "#Optimization Flow#\n";
00090     stream << "Alter:no\n";
00091     stream << "MonteCarlo:no\n";
00092     stream << "AlterMC cost:0.00\n";
00093     stream << "ExecuteRF:no\n";
00094     stream << "#\n\n";
00095 
00096     stream << "#DE#\n";
00097     pp = Props.at(1);
00098     QString val;
00099     val = pp->Value.section('|',0,0);
00100     stream << "choice of method:" << val << "\n";
00101     val = pp->Value.section('|',1,1);
00102     stream << "maximum no. of iterations:" << val << "\n";
00103     val = pp->Value.section('|',2,2);
00104     stream << "Output refresh cycle:" << val << "\n";
00105     val = pp->Value.section('|',3,3);
00106     stream << "No. of parents NP:" << val << "\n";
00107     val= pp->Value.section('|',4,4);
00108     stream << "Constant F:" << val << "\n";
00109     val = pp->Value.section('|',5,5);
00110     stream << "Crossing Over factor CR:" << val << "\n";
00111     val = pp->Value.section('|',6,6);
00112     stream << "Seed for pseudo random number generator:" << val << "\n";
00113     val = pp->Value.section('|',7,7);
00114     stream << "Minimum Cost Variance:" << val << "\n";
00115     val = pp->Value.section('|',8,8);
00116     stream << "Cost objectives:" << val << "\n";
00117     val = pp->Value.section('|',9,9);
00118     stream << "Cost constraints:" << val << "\n";
00119     stream << "#\n\n";
00120 
00121     stream << "# Parameters #\n";
00122     int i=1;
00123     for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
00124       if(pp->Name == "Var") {
00125   stream << "Parameter " << i << ":";
00126   val = pp->Value.section('|',0,0);
00127   stream << "#" << val << "#" << ":";
00128   val = pp->Value.section('|',2,2);
00129   stream << val << ":";
00130   val = pp->Value.section('|',3,3);
00131   stream << val << ":";
00132   val = pp->Value.section('|',4,4);
00133   stream << val << ":";
00134   val = pp->Value.section('|',5,5);
00135   stream << val << ":";
00136   val = pp->Value.section('|',1,1);
00137   stream << ((val == "yes") ? "OPT" : "---") << "\n";
00138       }
00139     }
00140     stream << "#\n\n";
00141 
00142     stream << "# Measurements #\n";
00143     for(pp = Props.at(2); pp != 0; pp = Props.next(), i++) {
00144       if(pp->Name == "Goal") {
00145   val = pp->Value.section('|',1,1);
00146   QString Type, Value;
00147   Value = pp->Value.section('|',2,2);
00148   if (val == "MIN" || val == "MAX" || val == "MON") {
00149     Value = "---";
00150   }
00151   Type = val;
00152   val = pp->Value.section('|',0,0);
00153   stream << val <<  ":"
00154          << "---" << ":"
00155          << Type << ":" << Value << "\n";
00156       }
00157     }
00158     stream << "#\n\n";
00159 
00160     stream << "# Post Processing #\n";
00161     stream << "#\n\n";
00162 
00163     afile.close();
00164   } else return false;
00165 
00166   QDir ExtractDir(QucsSettings.QucsHomeDir);
00167   if(!ExtractDir.cd("extract")) {
00168     if(!ExtractDir.mkdir("extract"))
00169       return false;
00170     if(!ExtractDir.cd("extract"))
00171       return false;
00172   }      
00173 
00174   for(pp = Props.at(2); pp != 0; pp = Props.next()) {
00175     if(pp->Name == "Goal") {
00176       QString VarName = pp->Value.section('|',0,0);
00177       QFile efile(ExtractDir.filePath(VarName));
00178       if(efile.open(QIODevice::WriteOnly)) {
00179     QTextStream stream(&efile);
00180   stream << "# Info #\n";
00181   stream << "#\n\n";
00182   stream << "# Commands #\n";
00183   stream << "#\n\n";
00184   stream << "# Post Processing #\n";
00185   stream << "MEASURE_VAR:#SYMBOL#:SEARCH_FOR:'<indep " << VarName
00186          << "':S_COL:01:P_LINE:01:P_COL:01:31" << "\n";
00187   stream << "#\n\n";
00188   efile.close();
00189       }
00190       else return false;
00191     }
00192   }
00193   return true;
00194 }
00195 
00196 // -----------------------------------------------------------
00202 bool Optimize_Sim::createASCOnetlist()
00203 {
00204   Property* pp;
00205   QStringList vars;
00206   for(pp = Props.at(2); pp != 0; pp = Props.next()) {
00207     if(pp->Name == "Var") {
00208       vars += pp->Value.section('|',0,0);
00209     }
00210   }
00211 
00212   QFile infile(QucsSettings.QucsHomeDir.filePath("netlist.txt"));
00213   QFile outfile(QucsSettings.QucsHomeDir.filePath("asco_netlist.txt"));
00214   if(!infile.open(QIODevice::ReadOnly)) return false;
00215   if(!outfile.open(QIODevice::WriteOnly)) return false;
00216   QTextStream instream(&infile);
00217   QTextStream outstream(&outfile);
00218   QString Line;
00219   while(!instream.atEnd()) {
00220     Line = instream.readLine();
00221     for(QStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
00222       if(Line.contains("Eqn:"))
00223       {
00224           QStringList splitLine = Line.split("\"");
00225 
00226           for(int i=1;i<splitLine.size()-3;i+=2)
00227           {
00228               if(splitLine[i].compare("yes")!=0 && splitLine[i].compare("no")!=0) //ignore last piece between quotes
00229               {
00230                   splitLine[i].replace(*it, "#"+*it+"#");
00231               }
00232           }
00233           Line = splitLine.join("\"");
00234 
00235       }
00236       else
00237       {
00238           QRegExp reg = QRegExp("=\"(" + *it + ")\"");
00239           Line.replace(reg, "=\"#\\1#\"");
00240       }
00241 
00242     }
00243     outstream << Line << "\n";
00244   }
00245   outfile.close();
00246   infile.close();
00247   return true;
00248 }
00249 
00250 // -----------------------------------------------------------
00251 bool Optimize_Sim::loadASCOout()
00252 {
00253   bool changed = false;
00254   Property* pp;
00255   QStringList vars;
00256   for(pp = Props.at(2); pp != 0; pp = Props.next()) {
00257     if(pp->Name == "Var") {
00258       vars += pp->Value.section('|',0,0);
00259     }
00260   }
00261 
00262   QFile infile(QucsSettings.QucsHomeDir.filePath("asco_out.log"));
00263   if(!infile.open(QIODevice::ReadOnly)) return false;
00264   QTextStream instream(&infile);
00265   QString Line;
00266   // we need just the last line with the final result
00267   while(!instream.atEnd()) Line = instream.readLine();
00268   infile.close();
00269 
00270   QStringList entries = QStringList::split(':',Line);
00271   QStringList::Iterator it;
00272   for(it = entries.begin(); it != entries.end(); ++it ) {
00273     QString Name = *it;
00274     Name = Name.trimmed();
00275     if(vars.contains(Name)) {
00276       for(pp = Props.at(2); pp != 0; pp = Props.next()) {
00277   if(pp->Name == "Var") {
00278     QString val[6];
00279     val[0] = pp->Value.section('|',0,0); // variable name
00280     if(val[0]==Name) {
00281       val[1] = pp->Value.section('|',1,1);
00282       val[2] = pp->Value.section('|',2,2);
00283       val[3] = pp->Value.section('|',3,3);
00284       val[4] = pp->Value.section('|',4,4);
00285       val[5] = pp->Value.section('|',5,5);
00286       ++it; // field after variable name is its value
00287       QString Value = *it;
00288       Value = Value.trimmed();
00289       val[2] = Value;
00290       pp->Value = val[0] + "|" + val[1] + "|" + val[2] + "|" +
00291         val[3] + "|" + val[4] + "|" + val[5];
00292       changed = true;
00293       break;
00294     }
00295   }
00296       }
00297     }
00298   }
00299   return changed;
00300 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines