当前位置: 首页>>代码示例>>Python>>正文


Python Group.parseFile方法代码示例

本文整理汇总了Python中pyparsing.Group.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python Group.parseFile方法的具体用法?Python Group.parseFile怎么用?Python Group.parseFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyparsing.Group的用法示例。


在下文中一共展示了Group.parseFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: solve

# 需要导入模块: from pyparsing import Group [as 别名]
# 或者: from pyparsing.Group import parseFile [as 别名]

#.........这里部分代码省略.........

	CondPrecedences = list()
	for P in S.precs_cond() :
		CondPrecedences.append(( task_to_id[P.left],task_to_id[P.right],P.offset))

	LowerBounds = list()
	for P in S.precs_low() :
		LowerBounds.append((task_to_id[P.left],P.right))

	UpperBounds = list()
	for P in S.precs_up() :
		UpperBounds.append((task_to_id[P.left],P.right))

	FixBounds = list()
	for T in S.tasks() :
		if T.start is not None :
			FixBounds.append((task_to_id[T],T.start))

	Precedences = sorted(Precedences)
	CondPrecedences = sorted(CondPrecedences)

	# add objective
	Objectives = [ (task_to_id[key],S.objective[key])  for key in S.objective if key != 1 ]
	#for key in S.objective :
	#	Objectives.append( (task_to_id[key],S.objective[key]) )
	Objectives = sorted(Objectives)

	# function to convert table into opl type typle lists
	def to_str(l) :
		return '\n'.join([ '<'+' '.join([ str(x) for x in row ])+'>'  \
                                  for row in l ])

	if not os.path.exists(solvers_path+'/tmp') :
		os.makedirs(solvers_path+'/tmp')

	# write .dat-file
	f = open(solvers_path+'/tmp/cpoptimizer.dat','w')
	f.write('Objectives={\n'+to_str(Objectives)+'\n};\n\n')
	f.write('Tasks={\n'+to_str(Tasks)+'\n};\n\n')
	f.write('Resources={\n'+to_str(Resources)+'\n};\n\n')
	f.write('PulseResources={\n'+to_str(PulseResources)+'\n};\n\n')
	f.write('TaskResources={\n'+to_str(TaskResources)+'\n};\n\n')
	f.write('TaskResourceGroups={\n'+to_str(TaskResourceGroups)+'\n};\n\n')
	f.write('TaskPulseResources={\n'+to_str(TaskPulsResources)+'\n};\n\n')
	f.write('Precedences={\n'+to_str(Precedences)+'\n};\n\n')
	f.write('TightPrecedences={\n'+to_str(TightPrecedences)+'\n};\n\n')
	f.write('CondPrecedences={\n'+to_str(CondPrecedences)+'\n};\n\n')
	f.write('UpperBounds={\n'+to_str(UpperBounds)+'\n};\n\n')
	f.write('LowerBounds={\n'+to_str(LowerBounds)+'\n};\n\n')
	f.write('FixBounds={\n'+to_str(FixBounds)+'\n};\n\n')
	f.close()

	# run cp-optimizer
	start_time = time.time()
	os.system('oplrun '+solvers_path+'/cpoptimizer.mod '+solvers_path+'/tmp/cpoptimizer.dat')
	if msg : print('INFO: execution time (sec) = '+str(time.time()-start_time))

	# parse output 
	from pyparsing import Keyword,Literal,Word,alphas,nums,printables,OneOrMore,ZeroOrMore,dblQuotedString,Group
	
	INT = Word( nums )

	int_row = Group( INT + \
                         Literal("<").suppress() + \
	                 INT + INT + INT + INT + \
                         Literal(">").suppress() )

	rint_row = Group( INT + INT +\
                         Literal("<").suppress() + \
	                 INT + INT + INT + INT + \
                         Literal(">").suppress() )

	res_seq_row = Group(Literal("<").suppress()  + dblQuotedString + \
	                    INT + INT + INT + \
	                    INT + INT + INT + \
                            Literal(">").suppress() )

	plan = Group( Keyword("INTERVALS").suppress() + Group( ZeroOrMore(int_row) ) +
                      Keyword("RINTERVALS").suppress() + Group( ZeroOrMore(rint_row) ) )#+

	opl_plan = plan.parseFile(solvers_path+'/tmp/cpoptimizer.out')

	int_plan = opl_plan[0][0]
	rint_plan = opl_plan[0][1]
	#res_seq_plan = opl_plan[0][2][0]

	# get starts of tasks #TODO: merge with resource assignment
	starts = { i : int(int_plan[i][2]) for i in range(len(int_plan)) }

	# get resource assignment
	assign = { i : [ int(rint_plan[j][1]) for j in range(len(rint_plan)) \
                         if int(rint_plan[j][0]) == i and int(rint_plan[j][2]) ] \
                         for i in starts.keys() }

	# add to scenario
	for T in S.tasks() :
		T.start = starts[task_to_id[T]] #second column is start
		if T.resources is None :
			T.resources = list()
		T.resources += [ id_to_resource[j] for j in assign[task_to_id[T]] ]
开发者ID:MiguelPeralvo,项目名称:pyschedule,代码行数:104,代码来源:cpoptimizer.py


注:本文中的pyparsing.Group.parseFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。