Job Processing

题意:一条流水线上有M1个A机器,M2个B机器,以及N个待加工的零件。每个零件均需要经过A然后再经过B加工才能成行。每个机器加工一个零件的时间不一定是相同的,在输入中给出。求对所有零件进行A处理最短需要多久,加工完所有的零件最短需要多久?

解题思路

  1. 参考NOCOW上的算法(http://www.nocow.cn/index.php/USACO/job
  2. 设time[i][j] = k为第j个i种类机器(0为A,1为B)加工一个零件所需要的时间为k。delay[i][j] = k为第j个i种类机器(0为A,1为B)加工一个零件所需要等待的时间为k。cost[i][j] = k为i种类机器(0为A,1为B)加工第j个零件所需要的时间为k
  3. 对于A机器,首先将delay[0][0...M1-1]设为0,然后将所有N个零件一个一个加入其中,对每个零件j,取delay[0][0...M1-1] + times[0][0...M1-1] 中最小的值(假设为delay[0][k] + times[0][k]),并将delay[0][k]设为这个最小值,然后用cost[0][j] 来保存这个最小值
  4. 对于B机器,与3中的操作相同,用delay[1][0...M2-1],times[1][0...M2-1],cost[1][0...N-1]来替换其中的数组
  5. 通过3、4的操作我们得到了cost[0..1][0...N-1],然后取cost[0][0...N-1]中的最大值就是对所有零件进行A处理所需要的最短时间。cost[0][i] + cost[1][N-1-i](其中i取[0, N-1])中的最大值就是加工完所有零件所需要的最短时间

代码

/*
ID: zc.rene1
LANG: C
PROG: job
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 99999

int N;
int M[2];
int times[2][30];
int delay[2][30];
int cost[2][1000];

int GetMin(int index)
{
    int min = MAX;
    int min_i = -1;
    int i;

    for (i=0; i<M[index]; i++)
    {
	if (delay[index][i] + times[index][i] < min)
	{
	    min = delay[index][i] + times[index][i];
	    min_i = i;
	}
    }

    delay[index][min_i] += times[index][min_i];

    return min;
}

int main(void)
{
    FILE *fin, *fout;
    int i, j, temp, max = -1;

    fin = fopen("job.in", "r");
    fout = fopen("job.out", "w");

    fscanf(fin, "%d %d %d", &N, &M[0], &M[1]);

    for (i=0; i<M[0]; i++)
    {
	fscanf(fin, "%d", ×[0][i]);
    }
    for (i=0; i<M[1]; i++)
    {
	fscanf(fin, "%d", ×[1][i]);
    }


    memset(cost, 0, 2 * 1000 * sizeof(int));
    memset(delay, 0, 2 * 30 * sizeof(int));

    for (i=0; i<2; i++)
    {
	for (j=0; j<N; j++)
	{
	    temp = GetMin(i);
	    cost[i][j] = temp;
	}
    }

    for (i=0; i<N; i++)
    {
	if (cost[0][i] > max)
	{
	    max = cost[0][i];
	}
    }
    fprintf(fout, "%d ", max);
    max = -1;
    for (i=0; i<N; i++)
    {
	if (cost[0][i] + cost[1][N-i-1] > max)
	{
	    max = cost[0][i] + cost[1][N-i-1];
	}
    }
    fprintf(fout, "%d\n", max);

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import numpy as np from platypus import NSGAII, Problem, Real, Integer # 定义问题 class JobShopProblem(Problem): def __init__(self, jobs, machines, processing_times): num_jobs = len(jobs) num_machines = len(machines[0]) super().__init__(num_jobs, 1, 1) self.jobs = jobs self.machines = machines self.processing_times = processing_times self.types[:] = Integer(0, num_jobs - 1) self.constraints[:] = [lambda x: x[0] == 1] def evaluate(self, solution): job_order = np.argsort(np.array(solution.variables[:], dtype=int)) machine_available_time = np.zeros(len(self.machines)) job_completion_time = np.zeros(len(self.jobs)) for job_idx in job_order: job = self.jobs[job_idx] for machine_idx, processing_time in zip(job, self.processing_times[job_idx]): machine_available_time[machine_idx] = max(machine_available_time[machine_idx], job_completion_time[job_idx]) job_completion_time[job_idx] = machine_available_time[machine_idx] + processing_time solution.objectives[:] = [np.max(job_completion_time)] # 定义问题参数 jobs = [[0, 1], [2, 0], [1, 2]] machines = [[0, 1, 2], [1, 2, 0], [2, 0, 1]] processing_times = [[5, 4], [3, 5], [1, 3]] # 创建算法实例 problem = JobShopProblem(jobs, machines, processing_times) algorithm = NSGAII(problem) algorithm.population_size = 100 # 设置优化目标 problem.directions[:] = Problem.MINIMIZE # 定义算法参数 algorithm.population_size = 100 max_generations = 100 mutation_probability = 0.1 # 设置算法参数 algorithm.max_iterations = max_generations algorithm.mutation_probability = mutation_probability # 运行算法 algorithm.run(max_generations) # 输出结果 print("最小化的最大完工时间:", algorithm.result[0].objectives[0]) print("工件加工顺序和机器安排方案:", algorithm.result[0].variables[:]) 请检查上述代码
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值