• 使用STL处理分支限界法处理最优装载问题


    使用STL处理分支限界法处理最优装载问题

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <time.h>
    #define MAX_SIZE 100
    int SIZE;
    using namespace std;
    float Object_Weight[MAX_SIZE];
    float SUM;
    class Node{
    public:
    float total_weight;
    int level;
    Node(){
    total_weight = 0;
    level = 0;
    for(int i=0;i<SIZE;i++)
    result[i] = false;
    }
    Node(const Node& obj){
    total_weight = obj.total_weight;
    level = obj.level;
    for(int i=0;i<SIZE;i++)
    result[i] = obj.result[i];
    }
    Node& operator = (const Node &obj){
    total_weight = obj.total_weight;
    level = obj.level;
    for(int i=0;i<SIZE;i++)
    result[i] = obj.result[i];
    return *this;
    }
    void set(bool value){
    result[level-1] = value;
    total_weight = getWeight();
    }
    float returnWeight(){return total_weight;}
    float maxEstWeight();

    void CopyResult(bool* re);
    private:
    float getWeight();
    bool result[MAX_SIZE];
    };
    struct cmp{
    bool operator()(Node& obj1, Node& obj2){
    return obj1.total_weight<obj2.total_weight;
    }
    };
    void Node::CopyResult(bool* re){
    for(int i=0;i<SIZE;i++)
    re[i] = result[i];
    }
    float Node::getWeight(){
    float sum = 0;
    for(int i=0;i<level;i++)
    {
    if(result[i])
    sum += Object_Weight[i];
    }
    return sum;
    }

    float Node::maxEstWeight(){
    float sum = total_weight;
    for(int i=level;i<SIZE;i++)
    sum += Object_Weight[i];
    return sum;
    }
    void naiveMethod(float c1,float c2){
    float bestWeight = 0;
    int counter = 0;
    bool* bestResult = new bool(SIZE);
    vector<Node> Queue;
    Node *a = new Node();
    Queue.push_back(*a);
    while(Queue.size() != 0){
    Node temp(Queue[0]);
    Queue.erase(Queue.begin());
    if(temp.level != SIZE){
    Node left(temp);
    Node right(temp);
    left.level++;
    left.set(false);
    right.level++;
    right.set(true);
    Queue.push_back(left);
    Queue.push_back(right);
    counter += 2;
    }
    if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
    bestWeight = temp.returnWeight();
    temp.CopyResult(bestResult);
    }
    }//while
    cout<<"c1 loading result:"<<bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<< bestResult[i]<<" ";
    cout<<endl;
    cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<<! bestResult[i]<<" ";
    cout<<endl;
    cout<<"Total counter: "<<counter<<endl;
    }

    void queueMethod(int c1, int c2){
    float bestWeight = 0;
    int counter = 0;
    bool* bestResult = new bool(SIZE);
    vector<Node> Queue;
    Node *a = new Node();
    Queue.push_back(*a);
    while(Queue.size() != 0){
    Node temp(Queue[0]);
    Queue.erase(Queue.begin());
    if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
    Node left(temp);
    Node right(temp);
    left.level++;
    left.set(false);
    right.level++;
    right.set(true);
    Queue.push_back(left);
    Queue.push_back(right);
    counter += 2;
    }
    if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
    bestWeight = temp.returnWeight();
    temp.CopyResult(bestResult);
    }
    }//while
    cout<<"c1 loading result:"<<bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<< bestResult[i]<<" ";
    cout<<endl;
    cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<<! bestResult[i]<<" ";
    cout<<endl;
    cout<<"Total counter: "<<counter<<endl;
    }


    void priority_QueueMethod(int c1, int c2){
    float bestWeight = 0;
    int counter = 0;
    bool* bestResult = new bool(SIZE);
    priority_queue<Node, vector<Node>, cmp> Queue;
    Node *a = new Node();
    Queue.push(*a);
    while(Queue.size() != 0){
    Node temp(Queue.top());
    Queue.pop();
    if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
    Node left(temp);
    Node right(temp);
    left.level++;
    left.set(false);
    right.level++;
    right.set(true);
    Queue.push(left);
    Queue.push(right);
    counter += 2;
    }
    if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
    bestWeight = temp.returnWeight();
    temp.CopyResult(bestResult);
    }
    }//while
    cout<<"c1 loading result:"<<bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<< bestResult[i]<<" ";
    cout<<endl;
    cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    for(int i=0;i<SIZE;i++)
    cout<<! bestResult[i]<<" ";
    cout<<endl;
    cout<<"Total counter: "<<counter<<endl;
    }

    int main(){
    float c1,c2;
    SUM= 0;
    cout<<"SIZE:"<<endl;
    cin>>SIZE;
    cout<<"WEIGHT:"<<endl;
    for(int i=0;i<SIZE;i++){
    cin>>Object_Weight[i];
    SUM += Object_Weight[i];
    }
    cout<<"C1:"<<endl;
    cin>>c1;
    cout<<"C2:"<<endl;
    cin>>c2;
    if(c1+c2<SUM)
    {
    cout<<"No solution!"<<endl;
    return EXIT_SUCCESS;
    }
    if(SUM<c1 || SUM<c2)
    {
    cout<<"Need only one ship!"<<endl;
    return EXIT_SUCCESS;
    }
    time_t start ,end ;
    double cost;
    start = clock();
    naiveMethod(c1, c2);
    end = clock();
    cost=difftime(end,start);
    cout<<"/////////////// Naive method time: "<<cost<<" ///////////////"<<endl;
    start = clock();
    queueMethod(c1,c2);
    end = clock();
    cost=difftime(end,start);
    cout<<"/////////////// Queue method time: "<<cost<<" ///////////////"<<endl;
    start = clock();
    priority_QueueMethod(c1,c2);
    end = clock();
    cost=difftime(end,start);
    cout<<"/////////////// Priority queue method time: "<<cost<<" ///////////////"<<endl;
    return EXIT_SUCCESS;
    }

  • 相关阅读:
    文章标题
    文章标题
    数据结构实验之查找一:二叉排序树
    数据结构实验之查找一:二叉排序树
    数据结构实验之排序三:bucket sort
    数据结构实验之排序三:bucket sort
    数据结构实验之排序二:交换排序
    数据结构实验之排序二:交换排序
    数据结构实验之排序一:一趟快排
    数据结构实验之排序一:一趟快排
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3312705.html
Copyright © 2020-2023  润新知