Example #1
0
  def _placement_algorithm_execute(self, scenario, reqid):
    """Executes the placement algorithm for the given scenario.If for some reason
    the algorithm fails to produce a valid placement (e.g., some constraint is violated),
    the function still returns the placement, but specific error information is included
    inside the JSON object which represents the solution. 
    """

    cfg = copy.deepcopy(self.configuration)
    cfg["scenario"] = scenario
    g = GA(cfg)
    if g.error:
      err = g.error_string
      solution = copy.deepcopy(scenario)
    else:
      solution = g.execute()
      # Check for errors/constrain violations
      err = None
      if not solution["solution_performance"]["link_capacity_constraints_ok"]:
        err = "Link capacity exceeded."
      if not solution["solution_performance"]["delay_constraints_ok"]:
        err = "Delay constraints violated."
      if not solution["solution_performance"]["host_capacity_constraints_ok"]:
        err = "Host capacity constraints violated."
      if not solution["solution_performance"]["legal_placement"]:
        err = "Illegal placement."

      self._output_solution(solution)

    # update the record in the database with the new status and the derived solution
    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    if err is not None:
      status = "FAIL"
    else:
      status = "SUCCESS"
    req = self.db_collection.find_one_and_update(
      {"ReqId": reqid}, 
      {'$set': {"finished": timestamp, "solution": solution, "status": status, "info": err}},
      projection={"_id": False},
      return_document=pymongo.collection.ReturnDocument.AFTER)

    # translate solution to the external format
    translated_solution = translator.translate_solution(solution)

    req_ext = self.db_collection_external.find_one_and_update(
      {"ReqId": reqid}, 
      {'$set': {"finished": timestamp, "solution": translated_solution, "status": status, "info": err}},
      projection={"_id": False},
      return_document=pymongo.collection.ReturnDocument.AFTER)

    return translated_solution, err
Example #2
0
class GaTestCase(TestCase):
    def setUp(self):
        self.fake_iter_plugin = FakeIterPlugin()
        self.fake_codec_plugin = FakeCodecPlugin()
        self.ga = GA(
            [self.fake_codec_plugin],
            [], [self.fake_iter_plugin], "")

    def testSetup(self):
        fake_population = FakePopulation()
        fake_selector = FakeSelector()
        self.ga.setup_population(fake_population)
        self.ga.use_selector(fake_selector)

        self.assertEqual(self.ga.population, fake_population)
        self.assertEqual(fake_population.selector, fake_selector)

        # run next
        self.ga.next()
        self.assertEqual(self.fake_iter_plugin.ga, self.ga)
        self.assertEqual(
            fake_population.codec_plugin, [self.fake_codec_plugin])
Example #3
0
 def setUp(self):
     self.fake_iter_plugin = FakeIterPlugin()
     self.fake_codec_plugin = FakeCodecPlugin()
     self.ga = GA(
         [self.fake_codec_plugin],
         [], [self.fake_iter_plugin], "")
Example #4
0
    net, train_loss, test_loss = trainBP(x_train_scaled.shape[1], hidden_number, 1,
                                         learning_rate, epochs, shuffle_seed, BATCH_SIZE, loss_fn,
                                         x_train_scaled, y_train_scaled,
                                         x_test_scaled, y_test_scaled)
    plotBP(train_loss, test_loss)
    plot_net_predict_result('BP ', net, x_test_scaled, target_sc, y_test)

    #遗传算法参数
    popsize = 100 #遗传算法种群数
    iter_max = 6 #遗传算法迭代次数
    PM = 0.05 #变异概率
    PC = 0.7 #交叉概率
    input_number = 21#输入维度
    output_number = 1#输出维度
     # 自适应参数
    PM1 = 0.05  # 变异概率下限
    PM2 = 0.25  # 变异概率上限
    PC1 = 0.5  # 交叉概率下限
    PC2 = 0.8  # 交叉概率上限
    adga = AGABP(net, input_number, hidden_number, output_number, popsize, iter_max, PM, PC, None,
                 PM1, PM2, PC1, PC2)
    bestChrom_ga, bestValue_ga, tracematga = adga.adgafun(x_test_scaled, y_test, target_sc)
    aga_net = adga.recover_net(bestChrom_ga)
    plot_net_predict_result("AGA ", aga_net, x_test_scaled, target_sc, y_test)

    gaObject = GA(net, input_number, hidden_number, output_number, popsize, iter_max, PM, PC)
    bestChrom_ga, bestValue_ga, tracematga = gaObject.gafun(x_train_scaled, y_train_scaled, target_sc)
    ga_net = gaObject.recover_net(bestChrom_ga)
    plot_net_predict_result("GA ", ga_net, x_test_scaled, target_sc, y_test)