Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 利润最大化,作业调度_Java_Algorithm_Graph_Greedy - Fatal编程技术网

Java 利润最大化,作业调度

Java 利润最大化,作业调度,java,algorithm,graph,greedy,Java,Algorithm,Graph,Greedy,我有一个场景,在这个场景中,每个时隙都有利润和多个工作可供选择。我需要在每个时间段选择工作,以便获得整体最大利润。我需要获得的最大利润和时间表 我唯一能想到的就是用暴力来对付每一个共产主义。我怎样才能有效地解决这个问题。有没有什么方法可以通过使用特定的算法或数据结构来做得更好 在下面的示例中,可以为timeslot1选择任何作业J1、J2、J4。类似地,对于其他时间段,可以选择任何一个作业或不选择任何作业。对于特定时间段,只能选择一个作业。如果某项工作在一个时间段内完成,则无法再次完成 例如,如

我有一个场景,在这个场景中,每个时隙都有利润和多个工作可供选择。我需要在每个时间段选择工作,以便获得整体最大利润。我需要获得的最大利润和时间表

我唯一能想到的就是用暴力来对付每一个共产主义。我怎样才能有效地解决这个问题。有没有什么方法可以通过使用特定的算法或数据结构来做得更好

在下面的示例中,可以为timeslot1选择任何作业J1、J2、J4。类似地,对于其他时间段,可以选择任何一个作业或不选择任何作业。对于特定时间段,只能选择一个作业。如果某项工作在一个时间段内完成,则无法再次完成

例如,如果j1在TS1中完成,则不能在TS2中再次拾取

+----------+--------+----------------------+
| TimeSlot | Profit | Possible Job         |
+----------+--------+----------------------+
|        1 |     50 | J1 or J2 or J4       |
|        2 |    100 | J1                   |
|        3 |     20 | J2                   |
|        4 |     60 | J5 or J4             |
|        5 |     15 | J1 or J2 or J3 or J6 |
+----------+--------+----------------------+

这可以通过以下方法得到最佳解决

在这里,您的图形是
G=(U,V,E)
,其中:

U = {1, 2, ...., n} // time slots
V = {J1, J2, ..., J_m} // jobs
E = { (i,J) | if job J can be done in time i }
w(i,J) = profit(i)

通过在时间段
i
中执行任务
J
,当最大匹配节点
J
与节点
i
匹配时,将上图中的最大匹配直接转换为最优解{

private int numberOfJobs;
private Job[] jobs;
private int maxProfit;

public class JobComparator implements Comparator<Job> {

    @Override
    public int compare(Job arg0, Job arg1) {
        if (arg0.end <= arg1.end)
            return -1;
        else
            return 1;
    }
}

public JobProfitMaximizer() {
    numberOfJobs = 0;
    maxProfit = 0;
}

private void printJobProfiles() {
    for (Job j : jobs) {
        System.out.println(j.start + " " + j.end + " " + j.profit);
    }
}

private void createJobProfiles() {
    jobs = new Job[numberOfJobs];
    File inputFile = new File("***Filepath***********");
    Scanner sc = null;
    int jobCounter = 0;
    try {
        sc = new Scanner(inputFile);
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            String[] profileOfJob = s.split(" ");
            int start = Integer.parseInt(profileOfJob[1]);
            int end = Integer.parseInt(profileOfJob[2]);
            int profit = Integer.parseInt(profileOfJob[3]);
            jobs[jobCounter] = new Job(start, end, profit);
            jobCounter = jobCounter + 1;
        }
    } catch (FileNotFoundException e) {
        System.out.println("The file is not present");
    } finally {
        try {
            if (sc != null)
                sc.close();
        } catch (Exception f) {
            System.out.println(f.getMessage());
        }
    }
}

private void setNumberOfJobs() {
    File inputFile = new File("***Filepath***********");
    Scanner sc = null;
    int countOfJobs = 0;
    try {
        sc = new Scanner(inputFile);
        while (sc.hasNextLine()) {
            countOfJobs = countOfJobs + 1;
            sc.nextLine();
        }
        numberOfJobs = countOfJobs;
        System.out.println(numberOfJobs);
    } catch (FileNotFoundException e) {
        System.out.println("The file is not present");
    } finally {
        try {
            if (sc != null)
                sc.close();
        } catch (Exception f) {
            System.out.println(f.getMessage());
        }
    }
}

private void sortJobsOnFinishTimes() {
    JobComparator jc = new JobComparator();
    Arrays.sort(jobs, jc);
}

private void calculateMaximumProfit() {
    int[] T = new int[numberOfJobs];
    T[0] = jobs[0].profit;

    for (int i = 1; i < numberOfJobs; i++) {
        T[i] = Math.max(T[i - 1], jobs[i].profit);
        for (int j = i - 1; j >= 0; j--) {
            if (jobs[j].end <= jobs[i].start) {
                T[i] = Math.max(T[i], T[j] + jobs[i].profit);
                break;
            }
        }
    }

    int currentMaxProfitValue = T[0];

    for (int m : T) {
        if (m > currentMaxProfitValue) {
            currentMaxProfitValue = m;
        }
    }
    maxProfit = currentMaxProfitValue;
}

public static void main(String args[]) {
    JobProfitMaximizer j = new JobProfitMaximizer();
    j.setNumberOfJobs();
    j.createJobProfiles();
    j.sortJobsOnFinishTimes();
    j.calculateMaximumProfit();
    System.out.println("The maximum profit is " + j.maxProfit);
}
private int numberOfJobs;
私人工作[]工作;
私营企业;
公共类JobComparator实现Comparator{
@凌驾
公共整数比较(作业arg0、作业arg1){
如果(arg0.end=0;j--){
if(作业[j].结束currentMaxProfitValue){
currentMaxProfitValue=m;
}
}
maxProfit=当前MaxProfitValue;
}
公共静态void main(字符串参数[]){
JobProfitMaximizer j=新的JobProfitMaximizer();
j、 setNumberOfJobs();
j、 createJobProfiles();
j、 sortJobsOnFinishTimes();
j、 计算最大利润();
System.out.println(“最大利润为”+j.maxProfit);
}

}

这个例子你能更精确一点吗?TS1=>J4,TS2=>J1,TS3=>J2,TS4=>J5,TS5=>J3或J6?我认为这是线性规划,不是动态规划。有很多线性规划库和优化库,但你没有在这方面标记语言,所以我不能建议你说什么,应该有她可能是一项工作的相关成本,或者是一个时间段内工作数量的限制,否则答案显然是将所有工作放在时间段2中,因为它产生的利润最大。@criket_007。在任何时间段,最多只能选择一项工作。谢谢。我从来没有想过这个问题可以用图表来建模。还有,有没有我还有其他方法可以解决这个问题,即使它没有最大加工效率那么高?维护一个数组T[]以存储最大利润并不断更新。根据作业的完成时间对作业进行排序。排序后,如果两个作业作业(I)和作业(j)没有重叠,则设置T[I]=max(T[I],T[j]+利润[j])。最后,在T[]中选择最大值这将是最终的最大利润值。这是自下而上的动态规划,因为递归展开