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

# APPSPACK_Constraints_Bounds.cpp

Go to the documentation of this file.

    00001 // $Id: APPSPACK_Constraints_Bounds.cpp,v 1.12 2004/10/21 23:06:53 tgkolda Exp $
    00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Constraints_Bounds.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
    00037 #include "APPSPACK_Vector.hpp"
    00038 #include "APPSPACK_Constraints_Bounds.hpp"
    00039 #include "APPSPACK_Print.hpp"
    00040
    00041 void APPSPACK::Constraints::Bounds::error(const string& fname, const string& msg)
    00042 {
    00043   cout << "APPSPACK::Constraints::" << fname << " - " << msg << endl;
    00044   throw "APPSPACK Error";
    00045 }
    00046
    00047 //static
    00048 APPSPACK::Vector APPSPACK::Constraints::Bounds::setup(Parameter::List& params)
    00049 {
    00050
    00051   Vector s,l,u,isl,isu;
    00052
    00053   if (params.isParameterVector("Scaling"))
    00054     s = params.getVectorParameter("Scaling");
    00055
    00056   if (params.isParameterVector("Upper"))
    00057     u = params.getVectorParameter("Upper");
    00058
    00059   if (params.isParameterVector("Lower"))
    00060     l = params.getVectorParameter("Lower");
    00061
    00062   if (params.isParameterVector("Is Lower"))
    00063     isl = params.getVectorParameter("Is Lower");
    00064
    00065   if (params.isParameterVector("Is Upper"))
    00066     isu = params.getVectorParameter("Is Upper");
    00067
    00068
    00069   // Determine the vector length
    00070   int n;
    00071   if (s.size() > 0)
    00072     n = s.size();
    00073   else if (l.size() > 0)
    00074     n = l.size();
    00075   else
    00076     error("setup", "Neither \"Scaling\" nor \"Lower\" is defined in the paramater list");
    00077
    00078
    00079   // Fill l and isl (if necessary)
    00080   if ((l.empty()) && (isl.empty()))
    00081   {
    00082     for (int i = 0; i < n; i ++)
    00083     {
    00084       l.push_back(0);
    00085       isl.push_back(0);
    00086     }
    00087
    00088     params.getParameter("Lower", l);
    00089     params.getParameter("Is Lower", isl);
    00090   }
    00091   else if (l.empty())
    00092   {
    00093     for (int i = 0; i < n; i ++)
    00094       if (isl[i] != 0)
    00095         error("setup", "\"Is Lower\" has a true (nonzero) entry but \"Lower\" is not defined");
    00096
    00097     for (int i = 0; i < n; i ++)
    00098       l.push_back(0);
    00099
    00100     params.getParameter("Lower", l);
    00101   }
    00102   else if (isl.empty())
    00103   {
    00104     for (int i = 0; i < n; i ++)
    00105       isl.push_back(1);
    00106
    00107     params.getParameter("Is Lower", isl);
    00108   }
    00109
    00110   // Fill u and isu (if necessary)
    00111   if ((u.empty()) && (isu.empty()))
    00112   {
    00113     for (int i = 0; i < n; i ++)
    00114     {
    00115       u.push_back(0);
    00116       isu.push_back(0);
    00117     }
    00118
    00119     params.getParameter("Upper", u);
    00120     params.getParameter("Is Upper", isu);
    00121   }
    00122   else if (u.empty())
    00123   {
    00124     for (int i = 0; i < n; i ++)
    00125       if (isl[i] != 0)
    00126         error("setup", "\"Is Upper\" has a true (nonzero) entry but \"Upper\" is not defined");
    00127
    00128     for (int i = 0; i < n; i ++)
    00129       u.push_back(0);
    00130
    00131     params.getParameter("Upper", u);
    00132   }
    00133   else if (isu.empty())
    00134   {
    00135     for (int i = 0; i < n; i ++)
    00136       isu.push_back(1);
    00137
    00138     params.getParameter("Is Upper", isu);
    00139   }
    00140
    00141   // Check sizes
    00142   if (l.size() != n)
    00143     error("setup", "Size mismatch for lower bounds");
    00144
    00145   if (u.size() != n)
    00146     error("setup", "Size mismatch for upper bounds");
    00147
    00148   if (isu.size() != n)
    00149     error("setup", "Size mismatch for isupper bounds");
    00150
    00151   if (isl.size() != n)
    00152     error("setup", "Size mismatch for islower bounds");
    00153
    00154
    00155   // Fix the scaling (if necessary)
    00156   if (s.empty())
    00157   {
    00158     // Check that all the bounds are specified
    00159     for (int i = 0; i < n; i ++)
    00160       if ((isu[i] == 0) || (isl[i] == 0))
    00161         error("setup", "\"Scaling\" must be specified if upper and lower bounds are not fully specified");
    00162
    00163     for (int i = 0; i < n; i ++)
    00164       s.push_back(u[i] - l[i]);
    00165
    00166     params.getParameter("Scaling", s);
    00167   }
    00168
    00169   if (s.size() != n)
    00170     error("setup", "Size mismatch for scaling");
    00171
    00172   return s;
    00173 }
    00174
    00175 //static
    00176 vector<bool> APPSPACK::Constraints::Bounds::convertToBool(const Vector& v)
    00177 {
    00178   vector<bool> b;
    00179   b.resize(v.size());
    00180   for (int i = 0; i < v.size(); i ++)
    00181     b[i] = (v[i] == 0) ? false : true;
    00182   return b;
    00183 }
    00184
    00185 //static
    00186 void APPSPACK::Constraints::Bounds::checkVector(const string& name, const Vector& v, int n)
    00187 {
    00188   if (v.size() != n)
    00189   {
    00190     cout << "APPSPACK::Constraints::Bounds::checkVector - \"" << name << "\" is the wrong size." << endl;
    00191     throw "APPSPACK Error";
    00192   }
    00193 }
    00194
    00195
    00196 //static
    00197 void APPSPACK::Constraints::Bounds::checkVector(const string& name, const vector<bool>& v, int n)
    00198 {
    00199   if (v.size() != n)
    00200   {
    00201     cout << "APPSPACK::Constraints::Bounds::checkVector - \"" << name << "\" is the wrong size." << endl;
    00202     throw "APPSPACK Error";
    00203   }
    00204 }
    00205
    00206
    00207 APPSPACK::Constraints::Bounds::Bounds(Parameter::List& params) :
    00208   scaling(setup(params)),
    00209   lower(params.getVectorParameter("Lower")),
    00210   upper(params.getVectorParameter("Upper")),
    00211   isLower(convertToBool(params.getVectorParameter("Is Lower"))),
    00212   isUpper(convertToBool(params.getVectorParameter("Is Upper")))
    00213 {
    00214   errorCheck();
    00215 }
    00216
    00217 void APPSPACK::Constraints::Bounds::errorCheck() const
    00218 {
    00219   int n = scaling.size();
    00220
    00221   if (n == 0)
    00222   {
    00223     cout << "APPSPACK::Constraints::Bounds::errorCheck() - No \"Scaling\" defined." << endl;
    00224     throw "APPSPACK Error";
    00225   }
    00226
    00227   checkVector("Lower", lower, n);
    00228   checkVector("Upper", upper, n);
    00229   checkVector("Is Lower", isLower, n);
    00230   checkVector("Is Upper", isUpper, n);
    00231
    00232   for (int i = 0; i < n; i ++)
    00233   {
    00234     if (scaling[i] <= 0)
    00235     {
    00236       cout << "APPSPACK::Constraints::Bounds::errorCheck() - Negative \"Scaling\" value." << endl;
    00237       throw "APPSPACK Error";
    00238     }
    00239
    00240     if ( (isLower[i]) && (isUpper[i]) && (lower[i] > upper[i]) )
    00241     {
    00242       cout << "APPSPACK::Constraints::Bounds::errorCheck() - Upper bound is less than lower bound." << endl;
    00243       throw "APPSPACK Error";
    00244     }
    00245   }
    00246
    00247 }
    00248
    00249 const APPSPACK::Vector& APPSPACK::Constraints::Bounds::getScaling() const
    00250 {
    00251   return scaling;
    00252 }
    00253
    00254 const APPSPACK::Vector& APPSPACK::Constraints::Bounds::getLower() const
    00255 {
    00256   return lower;
    00257 }
    00258
    00259 const APPSPACK::Vector& APPSPACK::Constraints::Bounds::getUpper() const
    00260 {
    00261   return upper;
    00262 }
    00263
    00264 const vector<bool>& APPSPACK::Constraints::Bounds::getIsLower() const
    00265 {
    00266   return isLower;
    00267 }
    00268
    00269 const vector<bool>& APPSPACK::Constraints::Bounds::getIsUpper() const
    00270 {
    00271   return isUpper;
    00272 }
    00273
    00274 void APPSPACK::Constraints::Bounds::print() const
    00275 {
    00276   cout << "\n";
    00277   cout << "Bound Constraints " << endl;
    00278
    00279   cout << "lower = ";
    00280   cout << "[";
    00281   for (int i = 0; i < scaling.size(); i ++)
    00282   {
    00283     if (isLower[i])
    00284     {
    00285       cout  << APPSPACK::Print::formatDouble(lower[i]) << " ";
    00286     }
    00287     else
    00288     {
    00289       cout << "<null>";
    00290       for (int i = 0; i < APPSPACK::Print::precision + 1; i ++)
    00291         cout << " ";
    00292     }
    00293
    00294   }
    00295   cout << "]\n";
    00296
    00297   cout << "upper = ";
    00298   cout << "[";
    00299   for (int i = 0; i < scaling.size(); i ++)
    00300   {
    00301     if (isUpper[i])
    00302     {
    00303       cout  << APPSPACK::Print::formatDouble(upper[i]) << " ";
    00304     }
    00305     else
    00306     {
    00307       cout << "<null>";
    00308       for (int i = 0; i < APPSPACK::Print::precision + 1; i ++)
    00309         cout << " ";
    00310     }
    00311
    00312   }
    00313   cout << "]\n";
    00314
    00315   cout << "scaling = " << scaling << endl;
    00316 }
    00317

© 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