Logo Search packages:      
Sourcecode: gaffitter version File versions  Download package

Optimizer.hh

// ---------------------------------------------------------------------
// $Id: Optimizer.hh,v 1.26 2006/08/26 10:20:30 daaugusto Exp $
//
//   Optimizer.hh (created on Thu Nov 17 18:25:35 BRT 2005)
// 
//   Genetic Algorithm File Fitter (gaffitter)
//
//   Copyright (C) 2005-2006 Douglas A. Augusto
// 
// This file is part of gaffitter.
// 
// gaffitter is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
// 
// gaffitter is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with gaffitter; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// ---------------------------------------------------------------------

#ifndef Optimizer_hh
#define Optimizer_hh

#include "Input.hh"

#include <vector>
#include <iostream>

using namespace std;

// ---------------------------------------------------------------------
/** Base class for search algorithms. */
00040 class Optimizer {
public:
   /** Initialize some variables */
00043    Optimizer(vector<SizeName>& sn, Params& p): 
             m_files(sn), m_params(p), m_best(0), m_cur_iter(0)
   {
      if (m_params.m_verbose) cout << "> Searching... " << flush << endl;
   }

private:
   /** Copies are not allowed. */
   Optimizer(const Optimizer&);
   /** Attributions are not allowed. */
   Optimizer& operator= (const Optimizer&);

public:
   /**
    * Frees memory pointed by m_best (vector)
    */
00059    virtual ~Optimizer() { delete m_best; }
   /**
    * Searches for a optimal solution (via Genetic Algorithm, Best
    * First or Brute Force)
    */
   virtual void Evolve() = 0;
   /**
    * Print the result to standard output.
    *
    * Print selected files, a brief summary (sum, diff and number of
    * selected files) and optionally the same for unselected files.
    */
   bool Output();
   /**
    * This overloaded operator prints the name and parameters of the
    * search algorithms
    */
   friend ostream& operator<< (ostream&, const Optimizer&);

protected:
   /**
    * Write to streams the name and parameters of search algorithms
    */
00082    virtual ostream& Write(ostream& s) const
   {
      s << "> Target: " << m_params.PrettySize(m_params.m_target) 
        << endl
        << "> Input size: " << m_files.size()
        << endl;

      return s;
   }

protected:
   /**
    * Calculate the total space used by the items for the given vector.
    */
   Params::Size_t CalcSum(const vector<bool>&) const;
   /**
    * Evaluate candidates.
    *
    * - Zero is the best fitness (perfect fit)
    * - Positive values indicate lesses than target
    * - Negative values indicate greater than target
    */
00104    Params::Size_t Evaluate(const vector<bool>& v) const { return Diff(v); }
   /**
    * Calculate the distance (diff) between the input target and a given size.
    */
00108    Params::Size_t Diff(Params::Size_t s) const { return m_params.m_target-s; }
   /**
    * Calculate the distance (diff) between the input target and a given
    * vector of items.
    */
00113    Params::Size_t Diff(const vector<bool>& v) const { return Diff(CalcSum(v)); }
   /**
    * Reset some varibles
    */
00117    virtual void Reset() { delete m_best; m_best = 0; }
   /**
    * Vector of input files/sizes
    */
00121    vector<SizeName>& m_files;
   /**
    * Global parameters.
    */
00125    Params& m_params;

protected:
   /**
    * \var m_best
    *
    * Pointer to the best solution found
    */
00133    vector<bool>* m_best;

private:
   /**
    * This data member holds the current iteration. \sa Output()
    */
00139    int m_cur_iter;
};

// --------------------------------------------------------------------
00143 inline ostream& operator<< (ostream& s, const Optimizer& optimizer)
{
   return optimizer.Write(s);
}

// --------------------------------------------------------------------
#endif

Generated by  Doxygen 1.6.0   Back to index