private static void TestTheadManager() { System.Collections.Concurrent.ConcurrentBag <string> bag = new System.Collections.Concurrent.ConcurrentBag <string>(); object locko = new object(); var vals = Enumerable.Range(1, 17).Select(m => m.ToString()).ToArray(); Devmasters.Batch.ThreadManager.DoActionForAll <string>(vals, i => { System.Threading.Thread.Sleep(Devmasters.Core.Rnd.Next(50, 140)); Console.WriteLine($"{i}"); lock (locko) { bag.Add(i); } return(new Devmasters.Batch.ActionOutputData()); }, true, 50, Devmasters.Batch.Manager.DefaultOutputWriter, new Devmasters.Batch.ActionProgressWriter(0.1f).Writer ); System.IO.File.WriteAllLines(@"c:\!\!xx.txt", bag.OrderBy(m => m).Select(m => m.ToString())); return; }
/// <summary> /// Find nearest clear spot (might be the given one) /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="target"></param> /// <returns></returns> private Tuple <int, int> FindClear(int x, int y, Actor target) { var r = 0; var toExamine = new System.Collections.Concurrent.ConcurrentBag <Tuple <int, int> >(); // Keep going till found while (true) { var items = new List <Tuple <int, int> >(); for (var ix = x - r; ix <= x + r; ++ix) { for (var iy = y - r; iy <= y + r; ++iy) { items.Add(new Tuple <int, int>(ix, iy)); } } Parallel.ForEach(items, j => { var ix = j.Item1; var iy = j.Item2; // If in bounds if ((ix < 0 || iy < 0 || ix >= currentMap.Width || iy >= currentMap.Height)) { return; } // Check if walkable if (!currentMap.map[ix, iy].walkable) { return; } // If both, check if monsters on it if (currentMap.Actors.Any(i => i != target && i.x == ix && i.y == iy && !i.dead)) { return; } // Check if borders another part in a cardinal direction bool borders = characterParts.Any(i => (i.x == ix + 1 && i.y == iy) || (i.x == ix - 1 && i.y == iy) || (i.x == ix && i.y + 1 == iy) || (i.x == ix && i.y - 1 == iy)); // Check if immediately borders parent - in case not above bool bordersParent = ((ix == this.x - 1 && iy == this.y) || (ix == this.x + 1 && iy == this.y) || (ix == this.x && iy == this.y - 1) || (ix == this.x && iy == this.y + 1)); // Has to border a bit if (!(borders || bordersParent)) { return; } // Got a candidate toExamine.Add(new Tuple <int, int>(ix, iy)); }); if (toExamine.Count > 0) { // Got one // Randomize order so not to always bias var randomizedOrder = toExamine.OrderBy(i => rng.Next()).Where(i => i != null); // And get the lowers var bitsByFarAway = randomizedOrder.OrderBy(i => Math.Sqrt(Math.Pow((i.Item1 - x), 2) + Math.Pow((i.Item2 - y), 2))); foreach (var i in bitsByFarAway) { return(bitsByFarAway.First()); } } else { // If too big, return ++r; if (r > 30) { return(null); } } } }
protected override void SolveInstance(IGH_DataAccess DA) { this.Message = string.Empty; List <string> code = new List <string>(); Grasshopper.Kernel.Data.GH_Structure <GH_Number> data; List <Plane> planes = new List <Plane>(); List <List <double> > angles = new List <List <double> >(); List <Speed> speeds = new List <Speed>(); List <Zone> zones = new List <Zone>(); List <Tool> tools = new List <Tool>(); List <CSystem> wobjs = new List <CSystem>(); // Store the input lists of external axis values to synchronise with the targets. List <double> eRotVals = new List <double>(); List <double> eLinVals = new List <double>(); DA.GetDataTree(0, out data); if (!DA.GetDataList(1, speeds)) { speeds.Add(Speed.Default); } if (!DA.GetDataList(2, zones)) { zones.Add(Zone.Default); } if (!DA.GetDataList(3, tools)) { tools.Add(ABBTool.Default); } //Convert GH_Structure<GH_Number> to List<List<double>> foreach (List <GH_Number> list in data.Branches) { var tempList = new List <double>(); foreach (var number in list) { double num; Grasshopper.Kernel.GH_Convert.ToDouble(number, out num, GH_Conversion.Both); tempList.Add(num); } angles.Add(tempList); } // If the inputs are present, get the external axis values. if (extRotAxisCheck.Checked) { if (!DA.GetDataList("Rotary", eRotVals)) { return; } } if (extLinAxisCheck.Checked) { if (!DA.GetDataList("Linear", eLinVals)) { return; } } //Compute targets System.Collections.Concurrent.ConcurrentBag <TargetOrderPair> bag = new System.Collections.Concurrent.ConcurrentBag <TargetOrderPair>(); Parallel.For(0, angles.Count, index => ComputeJointTargets(bag, index, angles, speeds, zones, tools, eRotVals, eLinVals)); c_targets = bag.OrderBy(b => b.Order).Select(b => b.Value).ToList(); // Set output data if (c_targets != null) { DA.SetDataList("Targets", c_targets); } if (outputCode.Checked) { this.Message = Manufacturer.ToString(); code = c_targets.Select(t => t.RobStr(Manufacturer)).ToList(); DA.SetDataList("Code", code); } }
protected override void SolveInstance(IGH_DataAccess DA) { this.Message = string.Empty; List <string> code = new List <string>(); List <Plane> planes = new List <Plane>(); List <List <double> > angles = new List <List <double> >(); List <Speed> speeds = new List <Speed>(); List <Zone> zones = new List <Zone>(); List <Tool> tools = new List <Tool>(); List <CSystem> wobjs = new List <CSystem>(); List <int> methods = new List <int>() { 0 }; // Store the input lists of external axis values to synchronise with the targets. List <double> eRotVals = new List <double>(); List <double> eLinVals = new List <double>(); DA.GetDataList(0, planes); if (!DA.GetDataList(1, speeds)) { speeds.Add(Speed.Default); } if (!DA.GetDataList(2, zones)) { zones.Add(Zone.Default); } if (!DA.GetDataList(3, tools)) { tools.Add(ABBTool.Default); } if (!DA.GetDataList(4, wobjs)) { wobjs.Add(CSystem.Default); } if (interpolation.Checked) { if (DA.GetDataList("*Method", methods)) { return; } } // If the inputs are present, get the external axis values. if (extRotAxisCheck.Checked) { if (!DA.GetDataList("Rotary", eRotVals)) { return; } } if (extLinAxisCheck.Checked) { if (!DA.GetDataList("Linear", eLinVals)) { return; } } System.Collections.Concurrent.ConcurrentBag <TargetOrderPair> bag = new System.Collections.Concurrent.ConcurrentBag <TargetOrderPair>(); // Compute results on given data Parallel.For(0, planes.Count, index => ComputeTargets(bag, index, planes, speeds, zones, tools, wobjs, eRotVals, eLinVals, methods)); //Compute bounding box for visualisation c_bBox = new BoundingBox(c_targets.Select(t => t.Plane.Origin).ToList()); //<--- Since Joint Targets so far don't have a spacial refference c_targets = bag.OrderBy(b => b.Order).Select(b => b.Value).ToList(); // Set output data if (c_targets != null) { DA.SetDataList("Targets", c_targets); } if (outputCode.Checked) { this.Message = Manufacturer.ToString(); code = c_targets.Select(t => t.RobStr(Manufacturer)).ToList(); DA.SetDataList("Code", code); } }
public TranslationComponents ParseXmlFiles(List <FileInfo> allStringTablePaths) { var lstHeader = new System.Collections.Concurrent.ConcurrentBag <string>(); var allModInfos = new System.Collections.Concurrent.ConcurrentBag <ModInfoContainer>(); var transComp = new TranslationComponents(); Parallel.ForEach(allStringTablePaths, (currentFile) => { var modInfo = new ModInfoContainer { FileInfoStringTable = currentFile, Name = currentFile.Directory.Name }; XDocument xdoc; try { xdoc = XDocument.Load(currentFile.FullName); } catch (XmlException xmlException) { throw new GenericXmlException("", currentFile.FullName, xmlException.Message); } IEnumerable <XElement> keys = xdoc.Descendants().Where(x => x.Name == KEY_NAME); var dicKeyWithTranslations = new Dictionary <string, Dictionary <string, string> >(); // all keys foreach (XElement key in keys) { string currentKeyId = key.Attribute(ID_NAME).Value; var dicTranslations = new Dictionary <string, string>(); // all languages of a key foreach (XElement language in key.Descendants()) { string languageName = language.Name.ToString(); if (dicTranslations.ContainsKey(languageName)) { throw new DuplicateKeyException(languageName, currentFile.FullName, currentKeyId); } dicTranslations.Add(languageName, language.Value); // save all the languages if (lstHeader.Contains(languageName) == false) { lstHeader.Add(languageName); } } if (dicKeyWithTranslations.ContainsKey(currentKeyId)) { throw new DuplicateKeyException(currentKeyId, currentFile.FullName, currentKeyId); } dicKeyWithTranslations.Add(currentKeyId, dicTranslations); } modInfo.Values = dicKeyWithTranslations; allModInfos.Add(modInfo); }); transComp.AllModInfo = allModInfos.OrderBy(mic => mic.Name).ToList(); transComp.Headers = lstHeader.OrderBy(h => h).ToList(); return(transComp); }