Esempio n. 1
0
  public void read(String filename) {
    logger.info("read vrp from file " + filename);
    XMLConfiguration xmlConfig = new XMLConfiguration();
    xmlConfig.setFileName(filename);
    xmlConfig.setAttributeSplittingDisabled(true);
    xmlConfig.setDelimiterParsingDisabled(true);

    if (schemaValidation) {
      final InputStream resource = Resource.getAsInputStream("vrp_xml_schema.xsd");
      if (resource != null) {
        EntityResolver resolver =
            new EntityResolver() {

              @Override
              public InputSource resolveEntity(String publicId, String systemId)
                  throws SAXException, IOException {
                {
                  InputSource is = new InputSource(resource);
                  return is;
                }
              }
            };
        xmlConfig.setEntityResolver(resolver);
        xmlConfig.setSchemaValidation(true);
        logger.info("validating " + filename + " with xsd-schema");
      } else {
        logger.warn(
            "cannot find schema-xsd file (vrp_xml_schema.xsd). try to read xml without xml-file-validation.");
      }
    }
    try {
      xmlConfig.load();
    } catch (ConfigurationException e) {
      logger.error(e);
      e.printStackTrace();
      System.exit(1);
    }
    readProblemType(xmlConfig);
    readVehiclesAndTheirTypes(xmlConfig);

    readShipments(xmlConfig);
    readServices(xmlConfig);

    readInitialRoutes(xmlConfig);
    readSolutions(xmlConfig);

    addJobsAndTheirLocationsToVrp();
  }
  @Override
  public void informAlgorithmStarts(
      VehicleRoutingProblem problem,
      VehicleRoutingAlgorithm algorithm,
      Collection<VehicleRoutingProblemSolution> solutions) {
    logger.info("prepare schrimpfAcceptanceFunction, i.e. determine initial threshold");
    double now = System.currentTimeMillis();

    /*
     * randomWalk to determine standardDev
     */
    final double[] results = new double[nOfRandomWalks];

    URL resource = Resource.getAsURL("randomWalk.xml");
    AlgorithmConfig algorithmConfig = new AlgorithmConfig();
    new AlgorithmConfigXmlReader(algorithmConfig).read(resource);
    VehicleRoutingAlgorithm vra =
        VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig);
    vra.setMaxIterations(nOfRandomWalks);
    vra.getAlgorithmListeners()
        .addListener(
            new IterationEndsListener() {

              @Override
              public void informIterationEnds(
                  int iteration,
                  VehicleRoutingProblem problem,
                  Collection<VehicleRoutingProblemSolution> solutions) {
                double result = Solutions.bestOf(solutions).getCost();
                //				logger.info("result="+result);
                results[iteration - 1] = result;
              }
            });
    vra.searchSolutions();

    StandardDeviation dev = new StandardDeviation();
    double standardDeviation = dev.evaluate(results);
    double initialThreshold = standardDeviation / 2;

    schrimpfAcceptance.setInitialThreshold(initialThreshold);

    logger.info("took " + ((System.currentTimeMillis() - now) / 1000.0) + " seconds");
    logger.debug("initial threshold: " + initialThreshold);
    logger.info("---------------------------------------------------------------------");
  }