コード例 #1
0
 def get_solution_info(self, solution):
     """Get information about the cost, service availability and latency of a solution
 """
     latency_full = helpers.get_solution_global_latency(solution, True)
     latency_nwk = helpers.get_solution_global_latency(solution)
     return {
         "cost": helpers.get_solution_cost(solution),
         "availability": helpers.get_solution_availability(solution),
         "latency": {
             "processing": latency_full - latency_nwk,
             "network": latency_nwk,
             "total": latency_full
         }
     }
コード例 #2
0
    def fitness(self, C):
        """Calculate the fitness of chromosome C.
    
    The fitness of a solution is its cost. First convert C to the full representation.
    Then calculate its cost. (The lower the better). 
    """

        solution = copy.deepcopy(self.scenario)
        helpers.from_chromosome(solution, C)
        if self.optimize_for == "availability":
            return helpers.get_solution_availability(solution)
        elif self.optimize_for == "latency":
            return helpers.get_solution_global_latency(solution,
                                                       self.processing_latency)
        else:  #cost
            return helpers.get_solution_cost(solution)
コード例 #3
0
    def init_solution_pool(self):
        """Initialize solution pool.
    
    Generate S feasible solutions/placements as follows: For each VNF, 
    select a random host. If it has enough capacity, place VNF there.
    Otherwise look for another host.
    """
        self.solution_pool = []
        solutions_to_generate = self.solution_pool_size

        while solutions_to_generate > 0:
            # TODO: Remove the deepcopy, just clear "place_at" fields
            solution = copy.deepcopy(self.scenario)

            reset = False
            for v in solution['vnfs']:
                if reset is True:
                    # Solution infeasible. Try another one...
                    logging.debug("Infeasible solution. Resetting")
                    break

                vnf_placed = False
                while not vnf_placed:
                    if not helpers.check_if_there_is_space(solution, v):
                        # reset solution and start from scratch
                        reset = True
                        break

                    h = choice(solution["hosts"])
                    v["place_at"] = [h["host_name"]]

                    # First check if it is allowed to place v at h
                    # If not, try another one
                    if not helpers.check_if_placement_allowed(
                            solution, h["host_name"], v["vnf_name"]):
                        logging.debug(
                            "init_solution_pool: Not allowed, trying another host"
                        )
                        v["place_at"] = None
                        continue

                    # check if h has the available resources to host v
                    # If capacity will be exceeded, try another host
                    if helpers.check_host_capacity_constraint(solution, h):
                        vnf_placed = True
                        logging.debug("init_solution_pool: VNF " +
                                      v["vnf_name"] + " placed at " +
                                      h["host_name"])
                    else:
                        v["place_at"] = None

            # check if the solution violates any MEC constraints
            mec_constraints_ok = helpers.check_mec_constraints(solution)
            if not mec_constraints_ok:
                logging.debug("init_solution_pool: Mec constraint violated")
                continue
            # check if we're violating location constraints
            location_constraints_ok = helpers.check_location_constraints(
                solution)
            if not location_constraints_ok:
                logging.debug(
                    "init_solution_pool: Location constraint violated")
                continue
            # Check if the solution violates any link capacities
            link_constraints_ok = helpers.check_link_capacity_constraints(
                solution)
            if not link_constraints_ok:
                logging.debug(
                    "init_solution_pool: Link capacity constraint violated")
                continue
            delay_constraints_ok = helpers.check_delay_constraints(solution)
            if not delay_constraints_ok:
                logging.debug("init_solution_pool: Delay constraint violated")
                continue
            #reachability_ok = helpers.check_reachability(solution)
            #if not reachability_ok:
            #  continue
            if reset is False:
                logging.debug(helpers.show_host_link_status(solution))
                logging.debug(
                    "Solution cost: " +
                    str(helpers.get_solution_cost(solution)) +
                    ", availability: " +
                    str(helpers.get_solution_availability(solution)) +
                    ", latency: " +
                    str(helpers.get_solution_global_latency(solution)))
                # Store the simplified "chromosome" representation of the solution
                C = helpers.to_chromosome(solution)
                self.solution_pool.append(C)
                solutions_to_generate -= 1