并行高效的foreach孩子评价 [英] Efficient foreach child evaluation in parallel

查看:95
本文介绍了并行高效的foreach孩子评价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的列表,每一个布尔ShouldRun()对他们的方法。

I have a list of objects, each with a bool ShouldRun() method on them.

我目前遍历对象的列表,并检查ShouldRun( )每个对象,并在第一次调用运行()返回true

I am currently iterating over the list of objects, and checking ShouldRun() on each object, and calling Run() on the first one to return true

foreach (child in Children)
{
   if (child.ShouldRun())
   {
      child.Run();
      break;
    }
 }



我想这样做是平行的,因为评估shouldRun可以采取时间像样的数目,这有利于让集合中后面的元素早就开始自己的评价。

I would like to do this in parallel, because evaluating shouldRun can take a decent amount of time, and it is advantageous to let later elements in the collection start their evaluation early.

不过,我不能想办法做这将满足这些条件:

However, I cannot think of a way to do this that will satisfy these conditions :

1只运行一个项目

2,不要运行,如果以后项目较早的项目是真实的,还是尚未完成评估尚未

2 Do not run a later item if an earlier item is true, or has not finished evaluating yet

3如果所有的早项目已返回false,和中间产品返回true,不伺候后来项目,因为你知道它不能覆盖较早完成任何评估。

3 If all "earlier" items have returned false, and a middle item returns true, do not wait on a later item to finish evaluating because you know it can't override anything earlier.

我觉得做一个平行的,其中LINQ查询检索所有项目的shouldRun( ),然后排序,但这将违反条件#3

I thought of doing a parallel "where" linq query to retrieve all the items that shouldRun() and then sorting, but this will violate condition #3

创意

背景资料:

的系统,为广义机器人的AI系统

The system is for a generalized robotics AI system.

一些较高优先级的任务可以通过立即已知传感器被触发变量如:林摔倒,解决这个问题。

Some of the higher priority tasks can be triggered by immediately known sensor variables eg : Im falling over, fix it!

其他的任务可能是计算密集型(做从相机图像识别和方法可见目标)

other tasks might be computationally intensive (do image recognition from the camera, and approach visible objectives)

其他的任务可能是数据库或远程驱动(查询从数据库中可能的目标位置的列表,然后导航那里看看,如果你能进入其中的一个可视范围)

other tasks might be database or remotely driven (look up a list of probable objective locations from a database, and then navigate there to see if you can get into visible range of one of them)

某些任务本身有子任务,基本上是将在任务的一个子集通过递归来启动该过程,并且孙子任务将通过链条传递向上

Some of the tasks themselves have child tasks, which is essentially going to start this process over recursively at a subset of tasks, and the grandchild task will be passed up through the chain

推荐答案

我不是一个天才PLINQ,但不会简单的这个答案就够了?

I'm not a PLINQ genius, but wouldn't this simpler answer suffice?

var childToRun = Children.AsParallel().AsOrdered()
    .Where(x => x.ShouldRun()).FirstOrDefault();
childToRun.Run();  

这篇关于并行高效的foreach孩子评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆