Home Page Downloads Contact Us

Free Open Source Artificial Intelligence C++ Parts

aiParts Open Source Project  

Home Page
Open Source
Contact
Free Downloads
Releases and Credits
Software Status
License
v0.9.3 README File
About rrproj
assigning people and/or equipment to projects
High-Hope Technique
History of the AI
View Source Files
and how they are organized
Making Programs
Development Notes
Using aiParts
Your Application
Support and Services

aiParts Source File

//======================================================================
//  main() for a stand-alone program to solve rrproj problems.
//
//  Copyright (c)  2008  Brian Marshall
//
//  See the license at end of this file.
//
//  This main() is used when the application runs a stand-alone
//  solver executable to solve rrproj problems.
//    - Problem data is passed to the solver in a disk file 
//    - Results are passed back to the application in a disk file
//
//  As an alternative to a stand-alone program using disk files,
//  the rrproj solver may be linked into an application, which:
//   - creates an instance of rppProblem
//   - calls rppProblem functions to provide problem data
//   - calls rppProblem::solve()
//   - calls rppProblem::rslt_desc() to get the result description
//   - calls rppProblem::get_pos_day_emp() to get the results
//   - deletes the instance of rppProblem if necessary

//----------------------------------------------------------------------

#include "rrproj_prob.h"

# include 
using namespace std;

//----------------------------------------------------------------------
//  Forward Declarations

void write_err (const char *outfilnam, const char *rslt_str);

//----------------------------------------------------------------------

int main (int argc, char *argv[]) {

  if (argc != 3) {
    cout << "\nUsage: "
         << "rrproj  \n\n";
    return 1;
  }

  char input_file[201]  = "";
  if (strlen(argv[1]) > 200) {
    cout << "\nError: "
         << "input_file must not be longer than 200 chars\n\n";
    return 1;
  }
  strcpy (input_file, argv[1]);

  char output_file[201] = "";
  if (strlen(argv[2]) > 200) {
    cout << "\nError: "
         << "output_file must be no longer than 200 chars\n\n";
    return 1;
  }
  strcpy (output_file, argv[2]);

  int n = 1;    // current status

  rppProblem * problem = 0;

  if (n) {
    problem = new rppProblem();
    if ( ! problem ) {
      cout << " Error creating problem\n\n";
      n=0;
    } else if ( ! problem->is_valid() ) {
      problem->log (" invalid problem");
      n=0;
    }
  }

  if (n) {
    if ( ! problem->read(input_file) ) {
      n=0;     // Error reading input
    }
  }

  if (n) {
    if ( ! problem->solve() ) {
      problem->log(" Error solving");
      n=0;
    }
  }

  if (n) {
    rppProbWrite *prob_write = new rppProbWriteWeek1(problem, 
                                                     output_file);
    if (!prob_write) {
      problem->log(" Error creating problem-writer");
      n=0;
    }
    if (n && ! prob_write->write() ) {
      problem->log(" Error writing");
      n=0;
    }
    if (prob_write) delete prob_write;
  }

  if (!n) write_err (output_file, problem->get_rslt_desc());

  if (problem) delete problem;

  if (!n) return 1;    // for main(), this means an error

  return 0;

}

//----------------------------------------------------------------------
//  Write the description of an error to the output file

void write_err (const char *outfilnam, const char *rslt_desc) {

  FILE *ofp = fopen (outfilnam, "w");
  if (!ofp) {
    printf ("\nError opening output file to describe error\n\n");
    return;
  }

  fprintf (ofp, "#  Error:\n");
  fprintf (ofp, "# %s\n", rslt_desc);

  fclose (ofp);

}
  
//======================================================================
//                           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.
//
//======================================================================