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

📄 rrproj_solv.cpp

📁 aiParts is a set of C++ classes that can be used to develop artificial intelligence for multi-decisi
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//**********************************************************************
//  rrproj_solv.cpp  -  function bodies for rrproj_solv.h
//
//  Copyright (c)  2008  Brian Marshall
//
//  See the license at end of this file.
//
//  Developers/Contributers:
//    [BRM] Brian Marshall - Calgary - bmarshal@agt.net
//
//  07/11/21  [BRM] began development
//
//----------------------------------------------------------------------

#include "rrproj_solv.h"

#include <string.h>
#include <iostream>
using namespace std;

//======================================================================
//  rpsProblem
//
//----------------------------------------------------------------------
//  Constructor

rpsProblem::rpsProblem () {

  aipTime tmp("Jan 1 1900");
  m_start_day = m_end_day = tmp;

}

//----------------------------------------------------------------------
//  Destructor

rpsProblem::~rpsProblem () {}

//----------------------------------------------------------------------
//  add_position

void rpsProblem::add_position (rpsPosition *x) {

  add_requirement(x);

}

//----------------------------------------------------------------------
//  add_employee

void rpsProblem::add_employee (rpsEmployee *x) {

  add_resource(x);

}

//----------------------------------------------------------------------
//  set_day_range

void rpsProblem::set_day_range (long start_yyyymmdd, 
                                long end_yyyymmdd) {

  m_start_day = aipTime(start_yyyymmdd);
  m_end_day   = aipTime(end_yyyymmdd);

}

//----------------------------------------------------------------------
//  position_iterator - return an iterator: positions in the problem

rpsPositionItr rpsProblem::position_iterator() const {

  rpsPositionItr i(requirement_pandemonium());

  return i;

}

//----------------------------------------------------------------------
//  employee_iterator  - return an iterator: employees in the problem

rpsEmployeeItr rpsProblem::employee_iterator() const {

  rpsEmployeeItr i(resource_pandemonium());

  return i;

}

//----------------------------------------------------------------------
//  decision_iterator - return an iterator: decisions in the problem

rpsDecisionItr rpsProblem::decision_iterator() const {

  rpsDecisionItr i(decision_pandemonium());

  return i;

}

//----------------------------------------------------------------------
//  take_msg 

void rpsProblem::take_msg (aipMsg *mm) {

  aiprrProblem::take_msg(mm);

}

//----------------------------------------------------------------------
//  Note that a decision has decided or failed to decide

void rpsProblem::note_decide (aipHHDecision *dd, aipHHOption *oo) {

  if (!dd) return;

  aiprrProblem::note_decide (dd, oo);

  rpsDecision *d = (rpsDecision*)dd;
  rpsOption   *o = (rpsOption*)oo;

  if (should_log_options()) log_decide (d, o);

  if (o) apply_emp_aspects (d, o);

}

//----------------------------------------------------------------------
//  A decision has just chosen an option - apply some aspects to
//  some other unchosen options (by changing their dynamic goodness).
//  The aspects, here, are related to the employee of the chosen option.

void rpsProblem::apply_emp_aspects (rpsDecision *d1, rpsOption *o1) {

        // regarding the emp, dcsn and chosen opt
  rpsPosDay   * pd1   = d1->posday();
  rpsPosition * p1    = pd1->pos();
  rpsEmpDay   * ed1   = o1->empday();
  rpsEmployee * e1    = ed1->emp();

        // regarding other dcsns and opts for this emp
  rpsDecision * d2;
  rpsOption   * o2;
  rpsPosDay   * pd2;
  rpsEmpDay   * ed2;

        // good for an emp to work if has not worked for a while
           // but now emp has worked
  if ( e1->g_last_work() > aipNeutral) e1->reset_g_last_work();

  rpsEmpDayItr editr = e1->empday_iterator();              // emp
  for ( ed2 = editr.first(); ed2; ed2 = editr.next() ) {   // empday
    if (e1->g_last_work() == aipNeutral) {
      long day_diff = ed2->day().days_since(pd1->day());
      if ( day_diff < -1 || day_diff > 2 ) continue;
    }
    rpsOptItr oitr = ed2->opt_iterator();
    for ( o2 = oitr.first(); o2; o2 = oitr.next() ) {      // opt
      if ( o2->is_chosen() ) continue;
      if ( o2->g_opt().is_forbidden() ) continue;
      d2 = (rpsDecision*)(o2->owner());                    // dcsn
      if ( d2->is_decided() ) continue;
      pd2 = d2->posday();                                  // posday

        // emp cannot work overlapping posdays
      if ( pd2->end_time()   > pd1->start_time() &&
           pd2->start_time() < pd1->end_time() )  {
        o2->set_g_dynamic(aipForbidden);
        continue;
      }

        // good for an emp to work same pos as previous day
      if ( pd2->pos()->pos_id() ==  p1->pos_id() &&
           pd2->proj_id()       == pd1->proj_id() &&
           pd2->day().days_since(pd1->day()) == 1 ) {
        o2->add_g_dynamic(aipGood);
      }

    }  // end of loop through options in ed2
  }   // end of loop through empdays ed2 for the emp e1

}

//----------------------------------------------------------------------
//  Write about a decision being decided if enable_log_options
//  has been called

void rpsProblem::log_decide (rpsDecision *d, rpsOption *o) {

  cout << "log: Decision for: "
       << " proj " << d->posday()->proj_id()
       << " pos "  << d->posday()->pos_id()
       << " on "   << d->posday()->yyyymmdd();

  if (o) {
    cout << " emp: " << o->emp()->emp_id()
         << "  g: " << o->g_opt_cmp().numeric_value();
  } else {
    cout << "  Failed";
  }

  cout << "\n";

}

//----------------------------------------------------------------------
//  Write about the try to cout if enable_log_tries() has been called

void rpsProblem::log_this_try () {

  aipHHSolveStat * ss = solve_stat();

  long g_val     = ss->g_usr_cur().numeric_value();
  long g_val_bsf = ss->g_usr_bsf().numeric_value();

  cout << "\n  +++ A Try has ended  -  Goodness: " << g_val 
       << "   (Best-so-far: " << g_val_bsf << ")\n"
       << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";

}

//======================================================================
//  rpsDecision
//
//----------------------------------------------------------------------
//  constructor

rpsDecision::rpsDecision (rpsPosDay *a_posday, long a_num_to_decide) 
                            : aiprrDecision(a_posday,a_num_to_decide) {}

//----------------------------------------------------------------------
//  destructor

rpsDecision::~rpsDecision() {}

//----------------------------------------------------------------------
//  add_rps_option

void rpsDecision::add_rps_option (rpsOption *x) { 

  add_aiprr_option(x); 

}

//----------------------------------------------------------------------
//  take_msg  - take and react to a message

void rpsDecision::take_msg (aipMsg *mm) {

  aiprrDecision::take_msg(mm);

//  rpsMsg *m = (rpsMsg *)mm;

//  if (m->typ() == HH_Starting_New_Try) {
//  }

}

//======================================================================
//  rpsOption  -  an employee working a position on a day
//
//----------------------------------------------------------------------
//  constructor

rpsOption::rpsOption(rpsEmpDay *a_empday, aipG g_constant)
                             : aiprrOption(a_empday, g_constant) {}

//----------------------------------------------------------------------
//  destructor

rpsOption::~rpsOption () {}

//----------------------------------------------------------------------
//  return emp, pos, day

rpsEmployee * rpsOption::emp () const { return empday()->emp(); }

rpsPosition * rpsOption::pos () const { return posday()->pos(); }

aipTime       rpsOption::day () const { return empday()->day(); }

//----------------------------------------------------------------------
//  take_msg  - take and react to a message

void rpsOption::take_msg (aipMsg *mm) {

  aiprrOption::take_msg(mm);

//  rpsMsg *m = (rpsMsg *)mm;

//  if (m->typ() == HH_Starting_New_Try) {

//  }

⌨️ 快捷键说明

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