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

📄 rrproj_prob.cpp

📁 aiParts is a set of C++ classes that can be used to develop artificial intelligence for multi-decisi
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//======================================================================
//  rppAssignment
//
//----------------------------------------------------------------------
//  Constructor

rppAssignment::rppAssignment (long a_proj_id, long a_pos_id, 
                              long a_emp_id,
                              long a_start_yyyymmdd, 
                              long a_end_yyyymmdd) {

  m_proj_id        = a_proj_id;
  m_pos_id         = a_pos_id;
  m_emp_id         = a_emp_id;
  m_start_yyyymmdd = a_start_yyyymmdd;
  m_end_yyyymmdd   = a_end_yyyymmdd;

}

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

rppAssignment::~rppAssignment () {}

//----------------------------------------------------------------------
//  Read data from string to setup this object

int rppAssignment::read_str (const char *x) {

  char s_start_date[31], s_end_date[31];

  if (x && *x) x = aip_get_val(x, 10, &m_proj_id,   '|');
  if (x && *x) x = aip_get_val(x, 10, &m_pos_id,    '|');
  if (x && *x) x = aip_get_val(x, 10, &m_emp_id,    '|');
  if (x && *x) x = aip_get_str(x, 10, s_start_date, '|');
  if (x && *x) x = aip_get_str(x, 10, s_end_date,   '|');

  if (!x) return 0;

  rppFileDate x_start_date(s_start_date);
  if ( !x_start_date.is_valid() ) return 0;
  m_start_yyyymmdd = x_start_date.long_date();

  rppFileDate x_end_date(s_end_date);
  if ( !x_end_date.is_valid() ) return 0;
  m_end_yyyymmdd = x_end_date.long_date();

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to the string; return 0 on failure

int rppAssignment::write_str (char *x) const {

  rppFileDate x_start_date(m_start_yyyymmdd);
  if ( !x_start_date.is_valid() ) return 0;

  rppFileDate x_end_date(m_end_yyyymmdd);
  if ( !x_end_date.is_valid() ) return 0;

  int n = sprintf (x, "rppAssignment|%ld|%ld|%ld|%s|%s",
                         m_proj_id, m_pos_id, m_emp_id, 
                         x_start_date.file_date(),
                         x_end_date.file_date());

  return (n ? 1 : 0);

}

//======================================================================
//  rppEmpDayPos
//
//----------------------------------------------------------------------
//  Constructor

rppEmpDayPos::rppEmpDayPos (long a_emp_id, long a_yyyymmdd,
                            long a_proj_id, long a_pos_id) { 

  m_emp_id   = a_emp_id;
  m_yyyymmdd = a_yyyymmdd;
  m_proj_id  = a_proj_id;
  m_pos_id   = a_pos_id;

}

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

rppEmpDayPos::~rppEmpDayPos () {}

//----------------------------------------------------------------------
//  Read data from string to setup this object

int rppEmpDayPos::read_str (const char *x) {

  char s_date[31];

  if (x && *x) x = aip_get_val(x, 10, &m_emp_id,  '|');
  if (x && *x) x = aip_get_str(x, 30, s_date,     '|');
  if (x && *x) x = aip_get_val(x, 10, &m_proj_id, '|');
  if (x && *x) x = aip_get_val(x, 10, &m_pos_id,  '|');

  if (!x) return 0;

  rppFileDate x_date(s_date);
  if ( !x_date.is_valid() ) return 0;
  m_yyyymmdd = x_date.long_date();

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to the string; return 0 on failure

int rppEmpDayPos::write_str (char *x) const {

  rppFileDate x_date(m_yyyymmdd);
  if ( !x_date.is_valid() ) return 0;

  int n = sprintf (x, "rppEmpDayPos|%ld|%s|%ld|%ld",
                      m_emp_id, x_date.file_date(),
                      m_proj_id, m_pos_id);

  return (n ? 1 : 0);

}

//======================================================================
//  rppProjWeekEmpDay
//
//----------------------------------------------------------------------
//  Constructor

rppProjWeekEmpDay::rppProjWeekEmpDay () {

  m_proj_id  = m_week_num = m_emp_id = 0;
  m_week_pos = m_yyyymmdd = m_pos_id = 0;

}

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

rppProjWeekEmpDay::~rppProjWeekEmpDay () {}

//----------------------------------------------------------------------
//  Set the data members

void rppProjWeekEmpDay::set (long a_proj_id, long a_week_num, 
                             long a_emp_id,  long a_week_pos, 
                             long a_yyyymmdd, long a_pos_id) {

  m_proj_id = a_proj_id;
  m_week_num = a_week_num;
  m_emp_id   = a_emp_id;
  m_week_pos = a_week_pos;
  m_yyyymmdd = a_yyyymmdd;
  m_pos_id   = a_pos_id;

}

//======================================================================
//  rppPosDayEmp
//
//----------------------------------------------------------------------
//  Constructor

rppPosDayEmp::rppPosDayEmp (long a_proj_id, long a_pos_id, 
                            long a_yyyymmdd, long a_emp_id) {

  m_proj_id  = a_proj_id;
  m_pos_id   = a_pos_id;
  m_yyyymmdd = a_yyyymmdd;
  m_emp_id   = a_emp_id;

}

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

rppPosDayEmp::~rppPosDayEmp () {}

//----------------------------------------------------------------------
//  Write the header for the data in this object

int rppPosDayEmp::write_hdr (char *x) const {

  int n = sprintf (x, "\n#rppPosDayEmp|proj_id|pos_id|date|emp_id");

  return (n ? 1 : 0);

}

//----------------------------------------------------------------------
//  Write the data in this object to the string; return 0 on failure

int rppPosDayEmp::write_str (char *x) const {

  rppFileDate x_date(m_yyyymmdd);
  if ( !x_date.is_valid() ) return 0;

  int n = sprintf (x, "rppPosDayEmp|%ld|%ld|%s|%ld",
                      m_proj_id, m_pos_id, 
                      x_date.file_date(), m_emp_id);

  return (n ? 1 : 0);

}

//======================================================================
//  rppProbWrite
//
//----------------------------------------------------------------------
//  Constructor

rppProbWrite::rppProbWrite (const rppProblem *x, 
                            const char * out_file_name)
                                               : m_prob(x) {

  if (out_file_name) {
    aip_strncpy (m_out_file_name, out_file_name, Max_File_Name);
  }

  m_any_pos_day_emp_written = 0;

}

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

rppProbWrite::~rppProbWrite () {}

//----------------------------------------------------------------------
//  Write the solution data - return zero on error

int rppProbWrite::write () {

  FILE *ofp = fopen (m_out_file_name, "w");
  if (!ofp) {
    log("Error opening output file: ");
    log(m_out_file_name);
    return 0;
  }

  if ( !pre_proc(ofp) ) return 0;

  rppPosDayEmpItr pdeitr = m_prob->posdayemp_iterator();
  rppPosDayEmp *pde;
  for (pde = pdeitr.first(); pde; pde = pdeitr.next()) {

    if ( !handle_pos_day_emp(ofp, pde) ) return 0;

  }   // end of loop through pos-day-emp data

  if ( !post_proc(ofp) ) return 0;

  fclose(ofp);

  return 1;

}

//----------------------------------------------------------------------
//  pre_proc - called before solution-detail data is handled

int rppProbWrite::pre_proc (FILE *ofp) {

  if (!ofp) return 0;

  int n = 1;

  if (n) n = fprintf (ofp, "#  rrproj output file\n\n");

  if (n) n = fprintf (ofp, "rppRsltDscr|%s\n", 
                           m_prob->get_rslt_desc());

  if (!n) {
    log("Error writing beginning of output file");
    return 0;
  }

  return 1;

}

//----------------------------------------------------------------------
//  handle a normalized solution detail

int rppProbWrite::handle_pos_day_emp (FILE *ofp, 
                                      const rppPosDayEmp *pde) {

  if (!ofp || !pde) return 0;

  char rec[201];

  if (!m_any_pos_day_emp_written) {

    if ( !pde->write_hdr(rec) ) {
      log("Error writing pos-emp-day header to the output string");
      return 0;
    }

    if ( !fprintf (ofp, "%s\n", rec) ) {
      log("Error writing pos-emp-day header to the output file");
      return 0;
    }

    m_any_pos_day_emp_written = 1;

  }

  if ( !pde->write_str(rec) ) {
    log("Error writing pos-emp-day data to the output string");
    return 0;
  }

  if ( !fprintf (ofp, "%s\n", rec) ) {
    log("Error writing pos-emp-day data to the output file");
    return 0;
  }

  return 1;

}

//----------------------------------------------------------------------
//  post_proc - called after solution-detail data is handled

int rppProbWrite::post_proc (FILE *ofp) {

  if (!ofp) return 0;

  return 1;

}

//----------------------------------------------------------------------
//  write a blank line to the output file

int rppProbWrite::write_blank_line (FILE *ofp) {

  if (!ofp) return 0;

  if ( !fprintf (ofp, "\n") ) {
    log("Error writing blank line to the output file");
    return 0;
  }

  return 1;

}

//======================================================================
//  rppProbWriteWeek1
//
//----------------------------------------------------------------------
//  Constructor

rppProbWriteWeek1::rppProbWriteWeek1 (const rppProblem *x, 
                                      const char *out_file_name)
                                   : rppProbWrite(x,out_file_name) {

  set_week1_start_dt();

  m_proj_week_emp_days = new aipPandemonium;
  if (!m_proj_week_emp_days) { log("new m_proj_week_emp_days error"); }

  m_proj_week = new rppProjWeek;
  if (!m_proj_week) { log("new m_proj_week error"); }

  m_proj_week_emp = new rppProjWeekEmp;
  if (!m_proj_week_emp) { log("new m_proj_week_emp error"); }

  reset();

}

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

rppProbWriteWeek1::~rppProbWriteWeek1 () {

  if (m_proj_week_emp_days) delete m_proj_week_emp_days;

  if (m_proj_week) delete m_proj_week;
  if (m_proj_week_emp) delete m_proj_week_emp;

}

//----------------------------------------------------------------------
//  Return zero if this object is not valid

int rppProbWriteWeek1::is_valid () const {

  if (!m_proj_week_emp_days || 
      !m_proj_week          || !m_proj_week_emp) return 0;

  return 1;

}

//----------------------------------------------------------------------
//  Reset current data - ie. everything except m_week1_start_dt

void rppProbWriteWeek1::reset () {

  if (!is_valid()) return;

  m_proj_week->reset();
  m_proj_week_emp->reset();

  m_any_week_data = m_any_emp_data = 0;
  
}

//----------------------------------------------------------------------
//  Set the date of the first day of week 1

void rppProbWriteWeek1::set_week1_start_dt () {

  aipTime prob_start_dt(prob()->start_yyyymmdd());
  aipTime prob_end_dt(prob()->end_yyyymmdd());

  m_week1_start_dt = prob_start_dt;

  long prob_start_day = prob_start_dt.days_since_sunday();
  long wk_offset = (prob_start_day + 7 - prob()->week_start_day()) % 7;
  if (wk_offset) {
    wk_offset = 0 - wk_offset;    // now zero or negative
    m_week1_start_dt.add_days(wk_offset);
  }

    // if the problem will fit into a week, force it if necessary
  long prob_num_days = prob_end_dt.days_since(prob_start_dt) + 1;
  if (prob_num_days <= 7) {
    long wnum1, wpos1, wnum2, wpos2;
    if( !week_dt(prob_start_dt, &wnum1, &wpos1) ) return;
    if( !week_dt(prob_end_dt,   &wnum2, &wpos2) ) return;
    if (wnum1 != wnum2) {
      m_week1_start_dt = prob_start_dt;
    }
  }

}

//----------------------------------------------------------------------
//  Determine the week-number and week_position (1 to 7) of a date.
//  Return zero on failure.

int rppProbWriteWeek1::week_dt (aipTime x,
                                long *week_num, long *week_pos) const {

  if ( !week_num || !week_pos ) return 0;

  long a, b;

  long ddif = x.days_after(m_week1_start_dt);

  if (ddif < -50000 || ddif > 50000) return 0;

  if (ddif >= 0) {
    a = 0;    b = 1;
  } else {
    a = 1;    b = 0;
  }

  *week_num = (ddif + a) / 7 + b;

  *week_pos = ((ddif + 70000) % 7) + 1;

  return 1;

}

//----------------------------------------------------------------------
//  handle a normalized solution detail
//
//  add the data to m_proj_week_emp_days to reorder it

int rppProbWriteWeek1::handle_pos_day_emp (FILE *ofp,
                                      const rppPosDayEmp *pde) {

⌨️ 快捷键说明

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