예제 #1
0
void initBoxCapacitated(std::vector<Box*> &vectorBox, Data &data, std::vector<int> & sorted_fac)
{
    // sorted_fac is sorted by decreasing capacities
    std::sort( sorted_fac.begin(), sorted_fac.end(), compare_capacity(data) );

    // Compute the total demand
    double dtotal( 0 );
    for ( unsigned int i = 0; i < data.getnbCustomer(); ++i )
    {
        dtotal += data.getCustomer(i).getDemand();
    }

    // Compute the minimum number of facilities to open (left).
    double qminsum( 0 );
    unsigned int minfac( 0 );
    while ( minfac < sorted_fac.size() && qminsum < dtotal )
    {
        qminsum += data.getFacility(sorted_fac[minfac]).getCapacity();
        ++minfac;
    }

    // Check if the problem can be feasible
    if ( qminsum < dtotal )
    {
        std::cerr << "The problem is infeasible !" << std::endl;
        return;
    }

    // Compute the minimum number of facilities to open (right).
    double qmaxsum( 0 );
    unsigned int maxfac( sorted_fac.size() );
    while ( maxfac >= 0 && qmaxsum < dtotal )
    {
        --maxfac;
        qmaxsum += data.getFacility(sorted_fac[maxfac]).getCapacity();
    }

    for ( int it = 0; it < Argument::paving_directions; ++it )
    {
        // GRASP
        std::vector<bool> facilityOpen(data.getnbFacility(), false);
        std::vector<int> not_open( sorted_fac );
        double qsum( 0 ), alpha( 0.5 ),
               dir( (double)it / (double)(Argument::paving_directions - 1.) );

        while ( qsum < dtotal )
        {
            std::vector<double> u( not_open.size() );
            std::vector<int> RCL;
            double umin( std::numeric_limits<double>::infinity() ),
                   umax( -std::numeric_limits<double>::infinity() ),
                   ulimit;

            // Build utility function
            for ( unsigned int j = 0; j < not_open.size(); ++j )
            {
                double cost( 0 );
                if ( data.getNbObjective() > 2 )
                {
                    for ( int k = 0; k < data.getNbObjective(); ++k )
                    {
                        cost += data.getFacility( not_open[j] ).getLocationObjCost( k );
                    }
                }
                else
                {
                    cost = dir * data.getFacility( not_open[j] ).getLocationObjCost( 0 )
                           + ( 1. - dir ) * data.getFacility( not_open[j] ).getLocationObjCost( 1 );
                }

                u[j] = data.getFacility( not_open[j] ).getCapacity() / cost;
                if ( u[j] < umin ) umin = u[j];
                if ( u[j] > umax ) umax = u[j];
            }
            ulimit = umin + alpha * ( umax - umin );

            // Build RCL
            for ( unsigned int j = 0; j < u.size(); ++j )
            {
                if ( u[j] >= ulimit )
                {
                    RCL.push_back( j );
                }
            }

            // Select a random facility
            int r = std::rand() % RCL.size();

            // Open it
            facilityOpen[not_open[RCL[r]]] = true;
            qsum += data.getFacility( not_open[RCL[r]] ).getCapacity();
            not_open.erase( not_open.begin() + RCL[r] );
        }
        vectorBox.push_back( new Box(data, facilityOpen) );
    }
}
예제 #2
0
void
process_utilization(resource_t * rsc, node_t ** prefer, pe_working_set_t * data_set)
{
    int alloc_details = scores_log_level + 1;

    if (safe_str_neq(data_set->placement_strategy, "default")) {
        GListPtr gIter = NULL;
        GListPtr colocated_rscs = NULL;
        gboolean any_capable = FALSE;

        colocated_rscs = find_colocated_rscs(colocated_rscs, rsc, rsc);
        if (colocated_rscs) {
            GHashTable *unallocated_utilization = NULL;
            char *rscs_id = crm_concat(rsc->id, "and its colocated resources", ' ');
            node_t *most_capable_node = NULL;

            unallocated_utilization = sum_unallocated_utilization(rsc, colocated_rscs);

            for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
                node_t *node = (node_t *) gIter->data;

                if (have_enough_capacity(node, rscs_id, unallocated_utilization)) {
                    any_capable = TRUE;
                }

                if (most_capable_node == NULL ||
                    compare_capacity(node, most_capable_node) < 0) {
                    /* < 0 means 'node' is more capable */
                    most_capable_node = node;
                }
            }

            if (any_capable) {
                for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
                    node_t *node = (node_t *) gIter->data;

                    if (have_enough_capacity(node, rscs_id, unallocated_utilization) == FALSE) {
                        pe_rsc_debug(rsc, "Resource %s and its colocated resources cannot be allocated to node %s: no enough capacity",
                                     rsc->id, node->details->uname);
                        resource_location(rsc, node, -INFINITY, "__limit_utilization__", data_set);
                    }
                }

            } else if (*prefer == NULL) {
                *prefer = most_capable_node;
            }

            if (unallocated_utilization) {
                g_hash_table_destroy(unallocated_utilization);
            }

            g_list_free(colocated_rscs);
            free(rscs_id);
        }

        if (any_capable == FALSE) {
            for (gIter = data_set->nodes; gIter != NULL; gIter = gIter->next) {
                node_t *node = (node_t *) gIter->data;

                if (have_enough_capacity(node, rsc->id, rsc->utilization) == FALSE) {
                    pe_rsc_debug(rsc, "Resource %s cannot be allocated to node %s: no enough capacity",
                                 rsc->id, node->details->uname);
                    resource_location(rsc, node, -INFINITY, "__limit_utilization__", data_set);
                }
            }
        }
        dump_node_scores(alloc_details, rsc, "Post-utilization", rsc->allowed_nodes);
    }
}