Java中的作业调度问题

发布于 2024-10-07 06:07:45 字数 795 浏览 0 评论 0原文

我正在写一个问题来解决工作计划,但我很难理解如何解决。

The Wood Shop 的世界著名摇椅积压了订单(每个订单 1 张椅子)。有几个 制作手工巴伯摇椅的步骤(例如切割木片、组装、打磨、涂抹污渍、 并涂清漆)。

制作一把椅子总共需要1周的时间。然而,由于椅子的销售方式不同 地区和市场不同,每个订单的利润金额可能会有所不同。此外,还有一个相关的截止日期 每个订单。只有在截止日期前完成任务,公司才会盈利;否则,利润为 0。

编写一个程序来确定订单的最佳计划,从而使利润最大化。输入文件将 包含一个或多个测试用例。测试用例中的第一行将包含一个整数 n (0 n 1000),表示 待处理的订单数量。

n 值为 0 表示输入文件结尾。 接下来的 n 行每行包含 3 个正整数。第一个整数 i 是订单号。

给定的所有订单号 测试用例是唯一的。第二个整数表示从现在到 i 的截止日期的周数 th 命令。这 第三个整数表示如果在第 i 个截止日期之前完成,公司将获得的利润金额 th 命令。

我要求的是一种应该如何解决这个问题的算法。

对于输入文件中的每个测试用例,输出文件应该输出一行报告由此产生的利润金额 以最佳顺序完成订单。

Example Input File (sched.in)
7
1 3 40
2 1 35
3 1 30
4 3 25
5 1 20
6 3 15
7 2 10
4
3054 2 30
4099 1 35
3059 2 25
2098 1 40
0
Example Output File (sched.out)
100
70

I am writing a problem to solve Job schedules but I am having a hard time understanding how.

The Wood Shop has a backlog of orders for its world famous rocking chair (1 chair per order). There are several
steps involved in making a handmade Baber Rocking chair (eg. cutting wood pieces, assembly, sanding, applying a stain,
and applying varnish).

The total time required to make a chair is 1 week. However, since the chairs are sold in different
regions and various markets, the amount of profit for each order may differ. In addition, there is a deadline associated
with each order. The company will only earn a profit if they meet the deadline; otherwise, the profit is 0.

Write a program that will determine an optimal schedule for the orders that will maximize profit. The input file will
contain one or more test cases. The first line in a test case will contain an integer, n (0 n 1000), that represents the
number of orders that are pending.

A value of 0 for n indicates the end of the input file.
The next n lines contain 3 positive integers each. The first integer, i, is an order number.

All order numbers for a given
test case are unique. The second integer represents the number of weeks from now until the deadline for i
th
order. The
third integer represents the amount of profit that the company will earn if the deadline is met for the i
th
order.

What I am asking for is an algorithm of how I should go about solving this problem.

For each test case in the input file, the output file should output a line that reports the amount of profit that results from
completing the orders in an optimal order.

Example Input File (sched.in)
7
1 3 40
2 1 35
3 1 30
4 3 25
5 1 20
6 3 15
7 2 10
4
3054 2 30
4099 1 35
3059 2 25
2098 1 40
0
Example Output File (sched.out)
100
70

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走野 2024-10-14 06:07:45

有很多方法可以解决车间问题。首先阅读维基百科条目,然后拿起一本关于算法设计的好书。你的教授可能会推荐一个。我怀疑动态编程将是解决这个问题的好方法,但也会有其他方法。

这是一个困难的问题,所以不要指望有一个简单的答案。许多人仍在研究有效解决这个问题的方法。

There are a bunch of ways to solve the job shop problem. Start by reading the wikipedia entry, then pick up a good book on algorithm design. Your professor can probably recommend one. I suspect dynamic programming would be a good way to approach this but there will be other approaches too.

This is a difficult problem so don't expect an easy answer. Many people are still researching ways to solve this problem efficiently.

乱世争霸 2024-10-14 06:07:45

你的问题的假设是不完整的。需要知道您每周可以制作多少张椅子。也许你可以一次性全部做好。但我们假设你只能制作一个。解决办法是这样的。

根据卡梅伦·斯金纳的非常聪明的评论,我改变了我的答案:

public class tarea
{         
    List<input> datas = new ArrayList<input>();

     class input
     {
         public int profit;
         public int deadline;
         public int index1;
         public int index2;
         public int sum() {return index1+index2;}
        /**
         * @param profit
         * @param deadline
         */
        public input(int deadline, int profit)
        {
            super();
            this.profit = profit;
            this.deadline = deadline;
        } 

     }


     public void readData1()
     {
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
     }


     public void readData2()
     {/*
         3 40
         2 1 35
         3 1 30
         4 3 25
         5 1 20
         6 3 15
         7 2 10 */

         this.datas.add(new input(3,40));
         this.datas.add(new input(1,35));
         this.datas.add(new input(1,30));
         this.datas.add(new input(3,25));
         this.datas.add(new input(1,20));
         this.datas.add(new input(3,15));
         this.datas.add(new input(2,10));
     }

     public void readData3()
     {/*
     2 30
     4099 1 35
     3059 2 25
     2098 1 40*/

         this.datas.add(new input(2,30));
         this.datas.add(new input(1,35));
         this.datas.add(new input(2,25));
         this.datas.add(new input(1,40));
     }



     @SuppressWarnings("unchecked")
    public void sortbyprofit(List<input> datas)
     {
         Collections.sort(datas, new Comparator() {

            public int compare(Object o1, Object o2)
            {
                if(((input)(o1)).profit < ((input)(o2)).profit)
                    return 1;
                else if(((input)(o1)).profit == ((input)(o2)).profit)
                    return 0;
                else return -1;
            }});
     }

     @SuppressWarnings("unchecked")
     public void sortbydeadline(List<input> datas)
      {
          Collections.sort(datas, new Comparator() {

             public int compare(Object o1, Object o2)
             {
                 if(((input)(o1)).deadline > ((input)(o2)).deadline)
                     return 1;
                 else if(((input)(o1)).deadline == ((input)(o2)).deadline)
                     return 0;
                 else return -1;
             }});
      }


     @SuppressWarnings("unchecked")
     public void sortbySum(List<input> datas)
      {
          Collections.sort(datas, new Comparator() {

             public int compare(Object o1, Object o2)
             {
                 if(((input)(o1)).sum() > ((input)(o2)).sum())
                     return 1;
                 else if(((input)(o1)).sum() == ((input)(o2)).sum())
                     return 0;
                 else return -1;
             }});
      }


    @SuppressWarnings("unchecked")
    public static void main(String[] args)
    {
        tarea tsk = new tarea();
        //tsk.readData1();
        //tsk.readData2();
        tsk.readData3();


        while (tsk.datas.size() > 0)
        {
            //sort by profit
            tsk.sortbyprofit(tsk.datas);
            int idx0 = 1;
            //assign index
            for (input data : tsk.datas)
            {
                data.index1 = idx0;
                idx0++;
            }

            //sort by deadline
            tsk.sortbydeadline(tsk.datas);
            int idx2 = 1;
            for (input data : tsk.datas)
            {
                data.index2 = idx2;
                idx2++;
            }

            //sort by sum and profit
            tsk.sortbySum(tsk.datas);

            List<input> tmpdatas = new ArrayList<input>();
            int valsum = tsk.datas.get(0).sum();
            for (input data : tsk.datas)
            {
                if (data.sum() == valsum)
                    tmpdatas.add(data);
            }            
            tsk.sortbyprofit(tmpdatas);

            //get the first one as result
            input thedata = tmpdatas.get(0);

            System.out.println("result ===> " + thedata.profit);

            tsk.datas.remove(thedata);
            tmpdatas = new ArrayList<input>();
            for (input data : tsk.datas)
            {
                data.deadline--;
                if (data.deadline > 0)
                    tmpdatas.add(data);
            }
            tsk.datas = tmpdatas;
        }


    }


}

The postulate of your problem is incomplete. It is required to know ho many chairs can you make per week. Maybe you can make all at once. But let's assume you can make only one. The solution is like this.

based on the very smart comments of Cameron Skinner I change my answer to this:

public class tarea
{         
    List<input> datas = new ArrayList<input>();

     class input
     {
         public int profit;
         public int deadline;
         public int index1;
         public int index2;
         public int sum() {return index1+index2;}
        /**
         * @param profit
         * @param deadline
         */
        public input(int deadline, int profit)
        {
            super();
            this.profit = profit;
            this.deadline = deadline;
        } 

     }


     public void readData1()
     {
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(1,1));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
         this.datas.add(new input(10,1000));
     }


     public void readData2()
     {/*
         3 40
         2 1 35
         3 1 30
         4 3 25
         5 1 20
         6 3 15
         7 2 10 */

         this.datas.add(new input(3,40));
         this.datas.add(new input(1,35));
         this.datas.add(new input(1,30));
         this.datas.add(new input(3,25));
         this.datas.add(new input(1,20));
         this.datas.add(new input(3,15));
         this.datas.add(new input(2,10));
     }

     public void readData3()
     {/*
     2 30
     4099 1 35
     3059 2 25
     2098 1 40*/

         this.datas.add(new input(2,30));
         this.datas.add(new input(1,35));
         this.datas.add(new input(2,25));
         this.datas.add(new input(1,40));
     }



     @SuppressWarnings("unchecked")
    public void sortbyprofit(List<input> datas)
     {
         Collections.sort(datas, new Comparator() {

            public int compare(Object o1, Object o2)
            {
                if(((input)(o1)).profit < ((input)(o2)).profit)
                    return 1;
                else if(((input)(o1)).profit == ((input)(o2)).profit)
                    return 0;
                else return -1;
            }});
     }

     @SuppressWarnings("unchecked")
     public void sortbydeadline(List<input> datas)
      {
          Collections.sort(datas, new Comparator() {

             public int compare(Object o1, Object o2)
             {
                 if(((input)(o1)).deadline > ((input)(o2)).deadline)
                     return 1;
                 else if(((input)(o1)).deadline == ((input)(o2)).deadline)
                     return 0;
                 else return -1;
             }});
      }


     @SuppressWarnings("unchecked")
     public void sortbySum(List<input> datas)
      {
          Collections.sort(datas, new Comparator() {

             public int compare(Object o1, Object o2)
             {
                 if(((input)(o1)).sum() > ((input)(o2)).sum())
                     return 1;
                 else if(((input)(o1)).sum() == ((input)(o2)).sum())
                     return 0;
                 else return -1;
             }});
      }


    @SuppressWarnings("unchecked")
    public static void main(String[] args)
    {
        tarea tsk = new tarea();
        //tsk.readData1();
        //tsk.readData2();
        tsk.readData3();


        while (tsk.datas.size() > 0)
        {
            //sort by profit
            tsk.sortbyprofit(tsk.datas);
            int idx0 = 1;
            //assign index
            for (input data : tsk.datas)
            {
                data.index1 = idx0;
                idx0++;
            }

            //sort by deadline
            tsk.sortbydeadline(tsk.datas);
            int idx2 = 1;
            for (input data : tsk.datas)
            {
                data.index2 = idx2;
                idx2++;
            }

            //sort by sum and profit
            tsk.sortbySum(tsk.datas);

            List<input> tmpdatas = new ArrayList<input>();
            int valsum = tsk.datas.get(0).sum();
            for (input data : tsk.datas)
            {
                if (data.sum() == valsum)
                    tmpdatas.add(data);
            }            
            tsk.sortbyprofit(tmpdatas);

            //get the first one as result
            input thedata = tmpdatas.get(0);

            System.out.println("result ===> " + thedata.profit);

            tsk.datas.remove(thedata);
            tmpdatas = new ArrayList<input>();
            for (input data : tsk.datas)
            {
                data.deadline--;
                if (data.deadline > 0)
                    tmpdatas.add(data);
            }
            tsk.datas = tmpdatas;
        }


    }


}
忆离笙 2024-10-14 06:07:45

欢迎来到 NP 完整规划问题的奇妙世界。一旦横向扩展,我们一生中就不可能找到最佳解决方案。但这并不重要,你只需要在给定时间内找到最佳解决方案(击败人类规划者和其他软件程序)。

不要自己编写这些算法(除非您是拥有多年经验的专家)。使用专门解决此类问题的现成库,例如:

Welcome to the wonderful world of NP complete planning problems. Once you scale out, it's impossible to find the optimal solution in our lifetimes. That doesn't matter though, you just have to find the best solution in the given time (beating human planners and other software programs).

Don't write these algorithms yourself (unless you're an expert with years of experience). Use an off the shelf library which specialize in these kind of problems, such as:

却一份温柔 2024-10-14 06:07:45

好吧,Java 不是这里的问题。不要专注于语言,而要考虑算法。

这“尝起来”就像有一个动态 编程解决方案,但据此我推断您是初学者,因此详尽的搜索可能更容易只要订单数量合理。在详尽的搜索中,您只需列出每个可能的订单并保存最有利可图的订单。

Okay, Java is not the problem here. Instead of concentrating on the language, think about the algorithm.

This "tastes" like something where there is a dynamic programming solution, but from this I'm kind of inferring you're a beginner, so an exhaustive search is probably easier as long as the number of orders is reasonable. In an exhaustive search, you'd simply lay out each possible order and save the most profitable one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文