Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# APPSPACK_Cache_Manager.cpp

Go to the documentation of this file.

    00001 // $Id: APPSPACK_Cache_Manager.cpp,v 1.10.2.1 2005/12/15 02:08:11 tgkolda Exp $
    00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Cache_Manager.cpp,v $
    00003
    00004 //@HEADER
    00005 // ************************************************************************
    00006 //
    00007 //          APPSPACK: Asynchronous Parallel Pattern Search
    00008 //                 Copyright (2003) Sandia Corporation
    00009 //
    00010 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
    00011 // license for use of this work by or on behalf of the U.S. Government.
    00012 //
    00013 // This library is free software; you can redistribute it and/or modify
    00014 // it under the terms of the GNU Lesser General Public License as
    00015 // published by the Free Software Foundation; either version 2.1 of the
    00016 // License, or (at your option) any later version.
    00017 //
    00018 // This library is distributed in the hope that it will be useful, but
    00019 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00021 // Lesser General Public License for more details.
    00022 //
    00023 // You should have received a copy of the GNU Lesser General Public
    00024 // License along with this library; if not, write to the Free Software
    00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00026 // USA.                                                                           .
    00027 //
    00028 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov)
    00029 //
    00030 // ************************************************************************
    00031 //@HEADER
    00032
    00037 #include "APPSPACK_Common.hpp"
    00038 #include "APPSPACK_Cache_Manager.hpp"
    00039 #include "APPSPACK_Utils.hpp"
    00040 #include "APPSPACK_Print.hpp"
    00041
    00042 APPSPACK::Cache::Manager::Manager(Parameter::List& params, const Vector& scaling) :
    00043   isFout(false)
    00044 {
    00045   Cache::Point::setStaticScaling(scaling);
    00046   Cache::Point::setStaticTolerance(params.getParameter("Cache Comparison Tolerance", params.getDoubleParameter("Step Tolerance") / 2.0));
    00047   precision = params.getParameter("Cache File Precision", 14);
    00048   treeptr = new SplayTree<Point>;
    00049
    00050   parseInputFile(params);
    00051   openOutputFile(params);
    00052
    00053   if (params.isParameterVector("Initial X") && ((params.isParameterDouble("Initial F")) || (params.isParameterValue("Initial F"))))
    00054   {
    00055     Vector x = params.getVectorParameter("Initial X");
    00056     Value f = params.isParameterDouble("Initial F") ? params.getDoubleParameter("Initial F") : params.getValueParameter("Initial F");
    00057     insert (x,f);
    00058   }
    00059
    00060 }
    00061
    00062 APPSPACK::Cache::Manager::~Manager()
    00063 {
    00064   delete treeptr;
    00065   closeOutputFile();
    00066 }
    00067
    00068 bool APPSPACK::Cache::Manager::insert(const Vector& x, const Value& f)
    00069 {
    00070   Point cp(x,f);
    00071   bool isInserted = treeptr->insert(cp);
    00072   if (isInserted)
    00073     writeToOutputFile(x,f);
    00074   return isInserted;
    00075 }
    00076
    00077 bool APPSPACK::Cache::Manager::isCached(const Vector& x, Value& f)
    00078 {
    00079   Point cp(x,f);
    00080
    00081   if ( ! (treeptr->find(cp)) )
    00082   {
    00083     return false;
    00084   }
    00085
    00086   f = cp.getF();
    00087   return true;
    00088 }
    00089
    00090 void APPSPACK::Cache::Manager::parseInputFile(APPSPACK::Parameter::List& params)
    00091 {
    00092   string inname;
    00093   inname = params.getParameter("Cache Input File", inname);
    00094   if (inname.empty())
    00095     return;
    00096
    00097   ifstream fin;
    00098   fin.open(inname.c_str());
    00099
    00100   if (!fin)
    00101   {
    00102     cout << "The cache input file \"" << inname << "\" does not exist." << endl;
    00103     return;
    00104   }
    00105
    00106   string line;
    00107   while (!fin.eof())
    00108   {
    00109     getline(fin, line);
    00110     processInputLine(line);
    00111   }
    00112
    00113   fin.close();
    00114 }
    00115
    00116 void APPSPACK::Cache::Manager::processInputLine(string& line)
    00117 {
    00118
    00119   string::size_type line_pos;
    00120
    00121   string element;
    00122   string::size_type element_pos;
    00123
    00124   double d;
    00125   Value v;
    00126   Vector x;
    00127
    00128   // Skip "f="
    00129   line_pos = 0;
    00130
    00131   if (!getNextString(line, line_pos, element))
    00132     return;
    00133
    00134   // Do nothing if line does not have the right format
    00135   if (element != "f=")
    00136     return;
    00137
    00138   // Read the function value
    00139   if (!getNextString(line, line_pos, element))
    00140     return;
    00141
    00142   // Process into a value
    00143   element_pos = 0;
    00144   if (getNextDouble(element, element_pos, d))
    00145     v.setValueTo(true, d);
    00146   else
    00147     v.setValueTo(false, 0);
    00148
    00149   // Skip "x=["
    00150   if (!getNextString(line, line_pos, element))
    00151     return;
    00152
    00153   // Do nothing if line does not have the right format
    00154   if (element != "x=[")
    00155     return;
    00156
    00157   // Read elements of x
    00158   x.resize(0);
    00159   while (1)
    00160   {
    00161     // Read the next element of x
    00162     if (!getNextString(line, line_pos, element))
    00163       return;
    00164
    00165     // Check for termination
    00166     if (element == "]")
    00167       break;
    00168
    00169     // Process into a value
    00170     element_pos = 0;
    00171     if (!getNextDouble(element, element_pos, d))
    00172       return;
    00173     x.push_back(d);
    00174   }
    00175
    00176   // Insert into the queue
    00177   insert(x, v);
    00178
    00179 }
    00180
    00181 void APPSPACK::Cache::Manager::openOutputFile(APPSPACK::Parameter::List& params)
    00182 {
    00183   string outname;
    00184   outname = params.getParameter("Cache Output File", outname);
    00185   if (outname.empty())
    00186     return;
    00187
    00188   // Open file for append
    00189   fout.open(outname.c_str(), std::ios::out|std::ios::app);
    00190
    00191   if (!fout)
    00192   {
    00193     cerr << "APPSPACK::Cache::Manager::openOutputFile - Unable to open cache output file" << endl;
    00194     return;
    00195   }
    00196
    00197   isFout = true;
    00198 }
    00199
    00200 void APPSPACK::Cache::Manager::writeToOutputFile(const Vector& x, const Value& f)
    00201 {
    00202   if (!isFout)
    00203     return;
    00204
    00205   fout << "f= ";
    00206   if (f.getIsValue())
    00207     fout << Print::formatDouble(f.getValue(), precision);
    00208   else
    00209   {
    00210     fout << "<null>";
    00211   }
    00212
    00213   fout << " x=[ ";
    00214   for (int i = 0; i < x.size(); i ++)
    00215   {
    00216     fout << Print::formatDouble(x[i], precision) << " ";
    00217   }
    00218   fout << " ]\n";
    00219 }
    00220
    00221 void APPSPACK::Cache::Manager::closeOutputFile()
    00222 {
    00223   if (!isFout)
    00224     fout.close();
    00225 }

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Wed Dec 14 18:41:04 2005 for APPSPACK 4.0.2 by 1.3.8 written by Dimitri van Heesch, © 1997-2002