zoukankan      html  css  js  c++  java
  • 使用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;
    }

  • 相关阅读:
    BeanUtils.copyProperties的用法
    Eclipse中GitLab的配置和使用入门
    认识与入门 Markdown
    mybatis基础配置
    动态规划-最长公共子串
    查找
    Entity Framework Code First ---EF Power Tool 和MySql一起使用遇到的问题
    使用TortoiseSVN碰到的几个问题(2)-冲突解决, 图标重载
    使用TortoiseSVN碰到的几个问题(1)-导入,提交,更新
    Asp.net MVC4 Step By Step(5)-使用Web API
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3312705.html
Copyright © 2011-2022 走看看