Ejemplo n.º 1
0
  bool OptionsList::GetNumericValue(const std::string& tag, Number& value,
                                    const std::string& prefix) const
  {
    SmartPtr<const RegisteredOption> option = NULL;

    if (IsValid(reg_options_)) {
      option = reg_options_->GetOption(tag);
      if (IsNull(option)) {
        std::string msg = "IPOPT tried to get the value of Option: " + tag;
        msg += ". It is not a valid registered option.";
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }

      if (option->Type() != OT_Number) {
        std::string msg = "IPOPT tried to get the value of Option: " + tag;
        msg += ". It is a valid option, but it is of type ";
        if (option->Type() == OT_Integer) {
          msg += " Integer";
        }
        else if (option->Type() == OT_String) {
          msg += " String";
        }
        else {
          msg += " Unknown";
        }
        msg += ", not of type Number. Please check the documentation for options.";
        if (IsValid(jnlst_)) {
          option->OutputDescription(*jnlst_);
        }
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }
    }

    std::string strvalue;
    if (find_tag(tag, prefix, strvalue)) {
      char* p_end;
      Number retval = strtod(strvalue.c_str(), &p_end);
      if (*p_end!='\0' && !isspace(*p_end)) {
        std::string msg = "Option \"" + tag +
                          "\": Double value expected, but non-numeric value \"" +
                          strvalue+"\" found.\n";
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }
      value = retval;
      return true;
    }
    else if (IsValid(option)) {
      value = option->DefaultNumber();
      return false;
    }
    return false;
  }
Ejemplo n.º 2
0
  bool OptionsList::GetNumericValue(const std::string& tag, Number& value,
                                    const std::string& prefix) const
  {
    SmartPtr<const RegisteredOption> option = NULL;

    if (IsValid(reg_options_)) {
      option = reg_options_->GetOption(tag);
      if (IsNull(option)) {
        std::string msg = "IPOPT tried to get the value of Option: " + tag;
        msg += ". It is not a valid registered option.";
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }

      if (option->Type() != OT_Number) {
        std::string msg = "IPOPT tried to get the value of Option: " + tag;
        msg += ". It is a valid option, but it is of type ";
        if (option->Type() == OT_Integer) {
          msg += " Integer";
        }
        else if (option->Type() == OT_String) {
          msg += " String";
        }
        else {
          msg += " Unknown";
        }
        msg += ", not of type Number. Please check the documentation for options.";
        if (IsValid(jnlst_)) {
          option->OutputDescription(*jnlst_);
        }
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }
    }

    std::string strvalue;
    if (find_tag(tag, prefix, strvalue)) {
      // Some people like to use 'd' instead of 'e' in floating point
      // numbers.  Therefore, we change a 'd' to an 'e'
      char* buffer = new char[strvalue.length()+1];
      strcpy(buffer, strvalue.c_str());
      for (int i=0; i<(int)strvalue.length(); ++i) {
        if (buffer[i]=='d' || buffer[i]=='D') {
          buffer[i] = 'e';
        }
      }
      char* p_end;
      Number retval = strtod(buffer, &p_end);
      if (*p_end!='\0' && !isspace(*p_end)) {
        delete [] buffer;
        std::string msg = "Option \"" + tag +
                          "\": Double value expected, but non-numeric value \"" +
                          strvalue+"\" found.\n";
        THROW_EXCEPTION(OPTION_INVALID, msg);
      }
      delete [] buffer;
      value = retval;
      return true;
    }
    else if (IsValid(option)) {
      value = option->DefaultNumber();
      return false;
    }
    return false;
  }