`
xitonga
  • 浏览: 588035 次
文章分类
社区版块
存档分类
最新评论

DP34 流水线调度问题 Assembly Line Scheduling @geeksforgeeks

 
阅读更多

A car factory has two assembly lines, each with n stations. A station is denoted by Si,jwhere i is either 1 or 2 and indicates the assembly line the station is on, and j indicates the number of the station.The time taken per station is denoted by ai,j. Each station is dedicated to some sort of work like engine fitting, body fitting, painting and so on. So, a car chassis must pass through each of the n stations in order before exiting the factory. The parallel stations of the two assembly lines perform the same task. After it passes through station Si,j, it will continue to station Si,j+1unless it decides to transfer to the other line. Continuing on the same line incurs no extra cost, but transferring from line i at station j – 1 to station j on the other line takes time ti,j. Each assembly line takes an entry time eiand exit time xiwhich may be different for the two lines. Give an algorithm for computing the minimum time it will take to build a car chassis.

The below figure presents the problem in a clear picture:
Figure-1

The following information can be extracted from the problem statement to make it simpler:

  • Two assembly lines, 1 and 2, each with stations from 1 to n.
  • A car chassis must pass through all stations from 1 to n in order(in any of the two assembly lines). i.e. it cannot jump from station i to station j if they are not at one move distance.
  • The car chassis can move one station forward in the same line, or one station diagonally in the other line. It incurs an extra cost ti, j to move to station j from line i. No cost is incurred for movement in same line.
  • The time taken in station j on line i is ai, j.
  • Si, jrepresents a station j on line i.

Breaking the problem into smaller sub-problems:
We can easily find the ith factorial if (i-1)th factorial is known. Can we apply the similar funda here?
If the minimum time taken by the chassis to leave station Si, j-1is known, the minimum time taken to leave station Si, jcan be calculated quickly by combining ai, jand ti, j.

T1(j)indicates the minimum time taken by the car chassis to leave station j on assembly line 1.

T2(j)indicates the minimum time taken by the car chassis to leave station j on assembly line 2.

Base cases:
The entry time eicomes into picture only when the car chassis enters the car factory.

Time taken to leave first station in line 1 is given by:
T1(1) = Entry time in Line 1 + Time spent in station S1,1
T1(1) = e1+ a1,1
Similarly, time taken to leave first station in line 2 is given by:
T2(1) = e2+ a2,1

Recursive Relations:
If we look at the problem statement, it quickly boils down to the below observations:
The car chassis at station S1,jcan come either from station S1, j-1or station S2, j-1.

Case #1: Its previous station is S1, j-1
The minimum time to leave station S1,jis given by:
T1(j) = Minimum time taken to leave station S1, j-1+ Time spent in station S1, j
T1(j) = T1(j-1) + a1, j

Case #2: Its previous station is S2, j-1
The minimum time to leave station S1, j is given by:
T1(j) = Minimum time taken to leave station S2, j-1+ Extra cost incurred to change the assembly line + Time spent in station S1, j
T1(j) = T2(j-1) + t2, j+ a1, j

The minimum time T1(j) is given by the minimum of the two obtained in cases #1 and #2.
T1(j) = min((T1(j-1) + a1, j), (T2(j-1) + t2, j+ a1, j))
Similarly the minimum time to reach station S2, j is given by:
T2(j) = min((T2(j-1) + a2, j), (T1(j-1) + t1, j+ a2, j))

The total minimum time taken by the car chassis to come out of the factory is given by:
Tmin = min(Time taken to leave station Si,n+ Time taken to exit the car factory)
Tmin = min(T1(n) + x1, T2(n) + x2)

Why dynamic programming?
The above recursion exhibits overlapping sub-problems. There are two ways to reach station S1, j:

  1. From station S1, j-1
  2. From station S2, j-1

So, to find the minimum time to leave station S1, jthe minimum time to leave the previous two stations must be calculated(as explained in above recursion).
Similarly, there are two ways to reach station S2, j:

  1. From station S2, j-1
  2. From station S1, j-1

Please note that the minimum times to leave stations S1, j-1and S2, j-1have already been calculated.

So, we need two tables to store the partial results calculated for each station in an assembly line. The table will be filled in bottom-up fashion.

Note:
In this post, the word “leave” has been used in place of “reach” to avoid the confusion. Since the car chassis must spend a fixed time in each station, the word leave suits better.


流水线调度问题,DP的经典例子。到达某一装配点的货物既可以由本流水线过来,也可以由另一支流水线过来,取决于哪一个更合算。

以下例子答案:

Output:

35

Figure-2
The bold line shows the path covered by the car chassis for given input values.

Exercise:
Extend the above algorithm to print the path covered by the car chassis in the factory.


package DP;

public class AssemblyLineScheduling {

	public static void main(String[] args) {
		int[][] a = {{4, 5, 3, 2},
                		 {2, 10, 1, 4}};
		int[][] t = {{0, 7, 4, 5},
                		 {0, 9, 2, 8}};
		int[] e = {10, 12};
		int[] x = {18, 7};
		System.out.println(carAssemblyDP(a, t, e, x));
		System.out.println(carAssembly(a, t, e, x));
	}
	
	public static int carAssembly(int[][] a, int[][] t, int[] e, int[] x){
		int n = a[0].length-1;
		return Math.min(carAssemblyRec(a,t, e, x, n, 0) + x[0], 
								carAssemblyRec(a,t, e, x, n, 1) + x[1]);
	}
	
	public static int carAssemblyRec(int[][] a, int[][] t, int[] e, int[] x, int n, int line){
		if(n == 0){
			return e[line] + a[line][0];
		}
		
		int T0 = Integer.MAX_VALUE;
		int T1 = Integer.MAX_VALUE;
		if(line == 0){		// 以下只适用于line0
			T0 = Math.min(carAssemblyRec(a, t, e, x, n-1, 0) + a[0][n],				// 从本线路过来的
								carAssemblyRec(a, t, e, x, n-1, 1) + t[1][n] + a[0][n]);	// 从另一条线路过来的
		}else if(line == 1){		// 以下只适用于line1
			T1 = Math.min(carAssemblyRec(a, t, e, x, n-1, 1) + a[1][n],				// 从本线路过来的
								 carAssemblyRec(a, t, e, x, n-1, 0) + t[0][n] + a[1][n]);	// 从另一条线路过来的
		}
		
		return Math.min(T0, T1);
	}

	public static int carAssemblyDP(int[][] a, int[][] t, int[] e, int[] x){
		int n = a[0].length;
		int[] T1 = new int[n];
		int[] T2 = new int[n];
		
		T1[0] = e[0] + a[0][0];
		T2[0] = e[1] + a[1][0];
		
		for(int i=1; i<n; i++){
			T1[i] = Math.min(T1[i-1]+a[0][i], T2[i-1]+t[1][i]+a[0][i]);
			T2[i] = Math.min(T2[i-1]+a[1][i], T1[i-1]+t[0][i]+a[1][i]);
		}
		
		return Math.min(T1[n-1]+x[0], T2[n-1]+x[1]);
	}
}


吐槽一下这题的递归,一开始我是这样写的:

	public static int carAssemblyRec(int[][] a, int[][] t, int[] e, int[] x, int n, int line){
		if(n == 0){
			return e[line] + a[line][0];
		}
		
		int T0 = Math.min(carAssemblyRec(a, t, e, x, n-1, 0) + a[0][n],
								 carAssemblyRec(a, t, e, x, n-1, 1) + t[1][n] + a[0][n]);
		int T1 = Math.min(carAssemblyRec(a, t, e, x, n-1, 1) + a[1][n],
								 carAssemblyRec(a, t, e, x, n-1, 0) + t[0][n] + a[1][n]);
		
		return Math.min(T0, T1);
	}

结果和DP的不一样,于是在哪里想了半天哪里出问题,费了好大功夫,用debugger单步跟踪才发现原来是忘记加限制条件了!!!

正确写法应该是:

	public static int carAssemblyRec(int[][] a, int[][] t, int[] e, int[] x, int n, int line){
		if(n == 0){
			return e[line] + a[line][0];
		}
		
		int T0 = Integer.MAX_VALUE;
		int T1 = Integer.MAX_VALUE;
		if(line == 0){		// 以下只适用于line0
			T0 = Math.min(carAssemblyRec(a, t, e, x, n-1, 0) + a[0][n],				// 从本线路过来的
								carAssemblyRec(a, t, e, x, n-1, 1) + t[1][n] + a[0][n]);	// 从另一条线路过来的
		}else if(line == 1){		// 以下只适用于line1
			T1 = Math.min(carAssemblyRec(a, t, e, x, n-1, 1) + a[1][n],				// 从本线路过来的
								 carAssemblyRec(a, t, e, x, n-1, 0) + t[0][n] + a[1][n]);	// 从另一条线路过来的
		}
		
		return Math.min(T0, T1);
	}



http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/


分享到:
评论

相关推荐

    node-v5.11.0-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    41532698775097Facebook Lite_405.0.0.8.113_apkcombo.com.armeabi-v7a.apk

    41532698775097Facebook Lite_405.0.0.8.113_apkcombo.com.armeabi-v7a.apk

    2024-2030中国RDF制粒机市场现状研究分析与发展前景预测报告.docx

    2024-2030中国RDF制粒机市场现状研究分析与发展前景预测报告

    财务困境-RLPM模型.xlsx

    详细介绍及样例数据:https://blog.csdn.net/li514006030/article/details/138549562

    基于python实现的Socket服务器与传感器通讯手段实现的家庭能源管理系统

    【作品名称】:基于Socket作为服务器与传感器通讯手段实现的家庭能源管理系统 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。

    node-v9.10.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    信息素养大赛的模拟题目

    内容概要: 此资源为信息素养大赛的算法创意实践挑战赛的模拟题目,包含:选择题、填空题、编程题。内容完全为自己整合,请各位同学及家长放心使用。

    什么是TJA和ICA.pdf

    什么是TJA和ICA

    2024-2030中国POS射频识别系统市场现状研究分析与发展前景预测报告.docx

    2024-2030中国POS射频识别系统市场现状研究分析与发展前景预测报告

    PCI & PCIe图像采集卡专精特新“小巨人”企业最新调研报告.docx

    PCI & PCIe图像采集卡专精特新“小巨人”企业最新调研报告

    基础运维技能(下)md格式笔记

    基础运维技能(下)md格式笔记

    ISO 18115-3-2022 表面化学分析词汇第3部分:光学界面分析术语.pdf

    ISO 18115-3-2022 表面化学分析词汇第3部分:光学界面分析术语.pdf

    node-v17.2.0-headers.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    WordPress原创插件:当日24小时发布文章标题变红.zip

    WordPress原创插件:当日24小时发布文章标题变红

    微博热搜数据可视化分析系统,技术框架python + flask web + mysql + pycharm

    微博热搜数据可视化分析系统 技术框架 python + flask web + mysql + pycharm 角色介绍 普通用户 qqq 123456 模块分析 登录注册 数据爬取 数据清洗 数据可视化模块 热门话题排行 热词榜单 话题热度趋势和分布 话题情感指数和趋势 词云 NLP情感分析 相关话题推送 分词主题数据提取 舆情分析 退出模块 数据库weibo_nlp_system 分析原理 我的最爱是动漫,你喜欢什么呢? 我 的 最爱 是 动漫 你 喜欢 什么 呢

    node-v8.5.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v9.3.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    QQIP查询,IP指针文件,IP查询

    这是一个能够获取 IP 访问者信息的文件。当有用户访问时,它可以精准地获取到访问者的 IP 地址。该文件具有高效、准确的特点,能够为相关应用提供有力的数据支持。通过使用这个文件,我们可以更好地了解访问者的来源和分布情况,有助于进行数据分析和应用优化。将其上传至 CSDN,希望能与更多开发者分享交流,共同探索其在不同领域的应用价值,为互联网技术的发展贡献一份力量。需注意,在使用过程中要遵循相关法律法规,确保数据的合法使用和隐私保护。

    node-v9.1.0-darwin-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    基于 Amazon AWS EC2 部署 Ghost 博客内含源码以及说明书可以自己运行复现.zip

    基于 Amazon AWS EC2 部署 Ghost 博客内含源码以及说明书可以自己运行复现.zip

Global site tag (gtag.js) - Google Analytics