⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aiphighhope.h

📁 aiParts is a set of C++ classes that can be used to develop artificial intelligence for multi-decisi
💻 H
📖 第 1 页 / 共 2 页
字号:
//  An application may require that a decision be decided more than
//  once to complete a solution - m_num_to_decide is set to more 
//  than one (in the constructor) and the chosen options for the
//  current and best-so-far solutions for this decision are stored 
//  in the m_chsn_opts_xxx pandemoniums.  (If the decision is only 
//  to be decided once, the pointers to these pandemoniums are zero.

class aipHHDecision : public aipDecision {

  aipHHImportance  m_importance;  // importance in decision order

  aipHHHope      * m_hope;        // how well this decision is doing

  aipHHOption    * m_opt_cur;     // The curremt option choice
  aipHHOption    * m_opt_bsf;     // The best-so-far option choice

  long             m_num_to_decide;   // times to decide in a try
  long             m_num_decided;     // times decided in this try

  aipPandemonium * m_chsn_opts_cur;   // opts chosen in cur  try
  aipPandemonium * m_chsn_opts_bsf;   // opts chosen in bsf  try

public:

  aipHHDecision (long the_num_to_decide =1);
  virtual ~aipHHDecision ();

  void add_option (aipOption *x) {}     // use add_hh_option

  void add_hh_option (aipHHOption *x);

  void dump_hope_to_ptr (char *p) const { m_hope->dump_to_ptr(p); }

  aipHHProblem * hh_prob () const { return (aipHHProblem*)owner(); }

  virtual void take_msg (aipMsg *m);

  aipHHOptionItr hh_option_iterator () const;
  aipHHOptionItr cur_opt_iterator   () const;  // current options
  aipHHOptionItr bsf_opt_iterator   () const;  // best-so-far opts

  aipG g_hope () const;

  long num_to_decide (void) const { return m_num_to_decide; }
  long num_decided   (void) const { return m_num_decided;   }

  int is_decided () const;

  virtual long imp () const { return m_importance.val(); }

  long num_opt_remaining () const;

  int any_fear () const { 
    if (!m_hope) return 0;
    return (m_hope->fear()->g() < aipNeutral); 
  }

  int any_greed () const { 
    if (!m_hope) return 0;
    return (m_hope->greed()->g() > aipNeutral); 
  }

  aipHHOption  * opt_cur  () const { return m_opt_cur;  }
  aipHHOption  * opt_bsf  () const { return m_opt_bsf;  }

  virtual aipHHOption * hh_decide();

};

//======================================================================
//  aipHHOption  -  an option used in High-Hope problem-solving
//
//  m_g_user_constant is a constant goodness that is meaningful
//  to the user (although it may have been multiplied by -1 if the
//  problem-solving is looking for a minimum).
//
//  m_g_constant is m_g_user_constant that has been normalized and
//  scaled into a range so that it can be meaningfully added to the
//  goodness of the hope.
//
//  In aipDecision::solve()...
//     g_opt() is used to make decisions
//     g_opt_cmp() is used to compare solutions 
//     g_opt_usr() is reported to the user.
//
//  Generally, g_opt() is partially or completely based on the hope
//  of the option, whereas g_opt_cmp() and g_opt_usr(), used in the
//  calculation of goodness of a solution, are not affected by hope.
//
//  g_opt_cmp() and g_opt_usr() may return the same value, or,
//  if a minimum is desired, and therefore m_g_user_constant was made 
//  negative, g_opt_usr() may return the negative of g_opt_cmp().

class aipHHOption : public aipOption {

  aipG            m_g_user_constant;
  aipG            m_g_constant;

  aipHHDecision * m_bsf_chooser;

  aipHHHope     * m_hope;           // how well this option is doing

public:

  aipHHOption(aipG g_user_const);
  virtual ~aipHHOption();

            // for aipHHProblem::normalize_option_goodnesses()
  void set_norm_g_constant (long new_val) {
    m_g_constant.set_numeric_value(new_val); 
  }

  void dump_hope_to_ptr (char *p) const { m_hope->dump_to_ptr(p); }

  aipHHDecision * hh_opt_owner () const { 
    return (aipHHDecision*)owner();       // all options have an owner
  }

  aipHHDecision * hh_opt_chooser () const {
    return (aipHHDecision*)chooser();     // or, zero before decide()
  }

  aipHHDecision * hh_bsf_chooser () const {
    return m_bsf_chooser;          // best-so-far chooser of this opt
  }

  int is_chosen () const { return chooser() ? 1 : 0; }

  virtual void take_msg (aipMsg *m);

  aipG g_constant      () const { return m_g_constant;      }
  aipG g_user_constant () const { return m_g_user_constant; }

  aipG g_hope () const;

  int any_fear  () const { 
    if (!m_hope) return 0;
    return (m_hope->fear()->g() < aipNeutral); 
  }

  int any_greed () const { 
    if (!m_hope) return 0;
    return (m_hope->greed()->g() > aipNeutral); 
  }

  virtual aipG g_opt();

  virtual aipG g_opt_cmp() { return g_user_constant(); }
  virtual aipG g_opt_usr() { return g_user_constant(); }

};

//======================================================================
//  aipHHDecisionItr  -  Iterator for aipHHDecision in a aipHHProblem

class aipHHDecisionItr : public aipDemonItr {

public:

  aipHHDecisionItr () {}

  aipHHDecisionItr (aipPandemonium *p) {
    set_demon_itr(p,0);
  }

  aipHHDecisionItr (const aipHHDecisionItr& x) {
    set_demon_itr (x.pandemonium(), x.current());
  }

  virtual ~aipHHDecisionItr () {}

  aipHHDecisionItr& operator = (const aipHHDecisionItr& x) {
    set_demon_itr (x.pandemonium(), x.current());
    return *this;
  }

  aipHHDecision * first() { 
    return (aipHHDecision*)aipDemonItr::first(); 
  }

  aipHHDecision * next() { 
    return (aipHHDecision*)aipDemonItr::next();  
  }

};

//======================================================================
//  aipHHOptionItr  -  Iterator for aipHHOption in an aipHHDecision

class aipHHOptionItr : public aipDemonItr {

public:

  aipHHOptionItr () {}

  aipHHOptionItr (aipPandemonium *p) {
    set_demon_itr(p,0);
  }

  aipHHOptionItr (const aipHHOptionItr& x) {
    set_demon_itr (x.pandemonium(), x.current());
  }

  virtual ~aipHHOptionItr () {}

  aipHHOptionItr& operator = (const aipHHOptionItr& x) {
    set_demon_itr (x.pandemonium(), x.current());
    return *this;
  }

  aipHHOption * first() { return (aipHHOption*)aipDemonItr::first(); }
  aipHHOption * next()  { return (aipHHOption*)aipDemonItr::next();  }

};

//======================================================================
//  aipHHSolveStat  -   solve() status and statistics

class aipHHSolveStat {

  aipHHProblem  * m_problem;

  friend class aipHHProblem;

              // About the solving...

  int       m_should_log_tries; // 0 = do not write about try in log

          // sum of option.g_opt_cmp()  - can be compared to each other
          // (may include dollars or miles or something)
  aipG      m_g_cmp_cur;        // Goodness of current try
  aipG      m_g_cmp_prev;       // Goodness of previous try
  aipG      m_g_cmp_bsf;        // Goodness of best-so-far try

          // sum of option.g_opt_usr  - to be reported to the user
          // (may be miles or dollars or minutes or goodness, etc.)
  aipG      m_g_usr_cur;        // Goodness (or $ or miles, etc. )
  aipG      m_g_usr_bsf;        // Goodness of best-so-far try

              // About the current try...

  long      m_i_try;

  short     m_try_result;
  int       m_solution_is_complete;
  int       m_solution_is_new_best;
  long      m_num_decisions_made;   // in current try, in solve()
  long      m_num_decisions_prev;   // in previous try, in solve()
  long      m_num_tries_to_best;

  aipHHDecision * m_failed_decision;  // or zero if success or quit

  aipHHDecision * m_worst_decision;
  aipG            m_worst_decision_g_cmp;

  long      m_tries_since_change_count;
  long      m_tries_since_improve_count;
  long      m_tries_since_bsf_count;

  int       m_is_too_bad;
  int       m_is_many_since_new_best;
  int       m_is_many_since_improve;
  int       m_is_many_since_change;

public:

  aipHHSolveStat(aipHHProblem *p);
  virtual ~aipHHSolveStat() {}

          // sum of option.g_opt_cmp()  - can be compared to each other
  aipG  g_cmp_cur  () const { return m_g_cmp_cur;  }
  aipG  g_cmp_prev () const { return m_g_cmp_prev; }
  aipG  g_cmp_bsf  () const { return m_g_cmp_bsf;  }
          // sum of option.g_opt_usr()  - can be reported to user
  aipG  g_usr_cur  () const { return m_g_usr_cur;  }
  aipG  g_usr_bsf  () const { return m_g_usr_bsf;  }

      // about the current try ...

  long  i_try () const { return m_i_try; }

  short try_result () const { return m_try_result; }

  int solution_is_complete () const { return m_solution_is_complete; }

  int solution_is_new_best () const { return m_solution_is_new_best; }

  long  num_tries_to_best () const { return m_num_tries_to_best; }

  long  percent_of_tries_done() {
    return ( (m_i_try * 100) / m_problem->num_try() );
  }

  aipHHDecision * failed_dcsn () const { return m_failed_decision; }

  int is_too_bad () const { return m_is_too_bad; }

  int try_is_a_success  () { return (m_g_cmp_cur > aipForbidden); }

  int prev_is_a_success () { return (m_g_cmp_prev > aipForbidden); }

  int try_is_new_best   () { return m_solution_is_new_best; }

  int try_is_a_best     () { return (try_is_a_success() &&
                                     m_g_cmp_cur == m_g_cmp_bsf); }

  int try_is_a_change   () { return (m_g_cmp_cur != m_g_cmp_prev); }

  int try_is_improvement() { 
    return ( (prev_is_a_success() && m_g_cmp_cur > m_g_cmp_prev) ||
             (m_problem && m_problem->dcsn_is_progress()  &&
              m_g_cmp_bsf.is_forbidden() && !m_is_too_bad &&
              m_i_try > 0 &&
              m_num_decisions_made > m_num_decisions_prev) ); 
  }

  int is_many_since_new_best () const { 
    return  m_is_many_since_new_best;
  }

  int is_many_since_improve () const { 
    return  m_is_many_since_improve;
  }

  int is_many_since_change () const { 
    return  m_is_many_since_change;
  }

  long  tries_since_change_count () const {
    return m_tries_since_change_count;
  }

  long  tries_since_improve_count () const {
    return m_tries_since_improve_count;
  }

  long  tries_since_bsf_count () const {
    return m_tries_since_bsf_count;
  }

};

//======================================================================
//  aipHHMsg  -  message for High-Hope problem-solving

class aipHHMsg : public aipMsg {

  aipHHProblem  * m_prob;
  int             m_is_in_cur_solution;
  int             m_is_the_failure;

public:

  aipHHMsg (aipHHProblem *p, short typ);
  ~aipHHMsg() {}

  aipHHProblem * prob () const { return m_prob; }

  aipHHSolveStat * solve_stat () const { 
    return m_prob ? m_prob->solve_stat() : 0; 
  }

  void set_is_in_cur_solution (int x) { m_is_in_cur_solution = x; }
  void set_is_the_failure     (int x) { m_is_the_failure     = x; }

  int is_in_cur_solution () const { return m_is_in_cur_solution; }
  int is_the_failure     () const { return m_is_the_failure;     }

};


//======================================================================

#endif

//======================================================================
//                           License
//
//   Permission is hereby granted, free of charge, to any 
//   person obtaining a copy of this software and associated 
//   documentation files (the "Software"), to deal in the Software 
//   without restriction, including without limitation the rights 
//   to use, copy, modify, merge, publish, distribute, sublicense, 
//   and/or sell copies of the Software, and to permit persons to 
//   whom the Software is furnished to do so, subject to the 
//   following conditions:
//
//   The copyright notice and this license shall be included in all 
//   copies or substantial portions of the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
//   NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
//   OTHER DEALINGS IN THE SOFTWARE.
//
//**********************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -