Sandia Home Sandia Home
Main Page | Publications | Downloads | Configuration | Running the Code | Solver Parameters | FAQ | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

example2a.cpp

Go to the documentation of this file.
00001 /* $Id: example2a.cpp,v 1.1 2006/06/30 01:07:26 tgkolda Exp $ */
00002 /* $Source: /space/CVS-Acro/acro/packages/appspack/appspack/examples/example2a.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 
00069 #include <iostream>
00070 #include <fstream>
00071 #include <vector>
00072 
00073 using std::vector;
00074 using std::ifstream;
00075 using std::ofstream;
00076 using std::cout;
00077 using std::endl;
00078 
00080 double feval(const vector<double>& x)
00081 {
00082   double f = 0;
00083 
00084   for (int i = 0; i < x.size(); i ++)
00085     f += (i + 1) * x[i] * x[i];
00086 
00087   return(f);
00088 
00089 } // end feval 
00090 
00092 double constraint(vector<double>& x)
00093 {
00094   double tmp = 0;
00095 
00096   for (int i = 0; i < x.size(); i ++)
00097     tmp += x[i] * x[i];
00098 
00099   return (tmp - 1.0);
00100 
00101 } // end constraint 
00102 
00104 int main(int argc, char* argv[]) 
00105 {
00106 
00107   int i;                        // counter 
00108   vector<double> x;             // x-vector 
00109   double y;                     // solution of f(x) 
00110   double c;                     // constraint c(x)
00111   ifstream fin;                 // input file
00112   ofstream fout;                // output file
00113 
00114   // Check input arguments 
00115 
00116   if (argc < 2) 
00117   {
00118     cout << "usage: " << argv[0] << " <input file> <output file>" << endl;
00119     return -1;
00120   }
00121 
00122   // Open input file 
00123 
00124   fin.open(argv[1]);
00125   if (!fin)
00126   {
00127     cout << argv[0] << " - Error opening input file " << argv[1] << endl;
00128     return -1;
00129   }
00130 
00131   // Read size of x 
00132 
00133   int n;
00134   fin >> n;
00135 
00136   // Allocate memory for x 
00137 
00138   x.resize(n);
00139 
00140   // Read x 
00141 
00142   for (i = 0; i < n; i ++)
00143     fin >> x[i];
00144   
00145   // Close input file 
00146 
00147   fin.close();
00148 
00149   // Open output file 
00150 
00151   fout.open(argv[2]);
00152   if (!fout)
00153   {
00154     cout << argv[0] << " - Error opening output file " << argv[2] << endl;
00155     return -1;
00156   }
00157 
00158   y = feval(x);
00159   c = constraint(x);
00160 
00161   // Write function value to output file 
00162   fout << y << "\n";
00163   fout << c;
00164 
00165   // Close output file 
00166 
00167   fout.close();
00168 
00169   // Exit 
00170 
00171   return 0;
00172 }
00173 

 

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Fri Feb 16 10:33:35 2007 for APPSPACK 5.0.1 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2002