How to order this list? - algorithm

I have a list of applications. I need to order them in a specific way and install in that order.
Things to consider:
Some applications have as a requirement, another application.
Some applications need a reboot before install next application, we want this applications to stay at bottom of the list but some of them may require an application that doesn't need a reboot, so, it can happen that some application that doesn't have any requirement neither a reboot, goes after an application that needs a reboot.
An example:
P1 (Reboot)
P2 (Needs P3)
P3
P4 (Needs P1)
P5 (Reboot and needs P3)
P6 (Reboot)
P7
So, if we have the apps in that order:
P1 - P2 - P3 - P4 - P5 - P6 - P7
The correct order would be (for example):
P3 - P7 - P2 - P1 - P4 - P5 - P6
If theres a non reboot app that has as requirement an app that needs a reboot (like P4) would be better if they stay upper on list than the others reboot apps (P5 - P6)

You need a topological sorting algorithm.

Related

TwinCAT 3 - Manage I/Os via GCode / program M Commands

What I'm trying to achieve is to switch on and off specific I/Os via gcode call.
In some motion controllers I worked before, that could be done with an M-command (i.e. M10Pn, where n indicates the pin address).
For example:
Move to position X10 Y10
Turn on cooling
Move to position X0 Y0
Turn off cooling
Assuming that the cooling is linked to a dedicated ethercat module, how do I address it?
I browsed the beckhoff infosys, and it looks like M commands are the starting point, but I'm not sure how to program them.
Does anyone know where to find an example?

RS485 Communication Problem with Specific Hardware

I have a communication problem with RS485 that I am not sure which component or thing is problematic. I will define some cases, which some works and others not.
I have four different components,
USB - RS 232 Converter
RS232 - RS485 Converter(1)
RS232 - RS485 Converter(2)
USB - RS485
Raspberry
Ubuntu Computer
Specific Hardware gets direct RS485
My test cases are like fallowing (I will indicate components like C5, which means Raspberry)(I am using the first component as input point, and using minicom),
(C5 - C1 - C2 - C4 - C6)(Baud: 115200) Working
(C6 - C1 - C2 - C4 - C5)(Baud: 115200) Working
(C5 - C4 - C2 - C1 - C6)(Baud: 115200) Working with no hard flow
(C6 - C4 - C2 - C1 - C5)(Baud: 115200) Working with no hard flow
They seams working but here is the tricky part
(C6 - C4 - C7)(Baud: 115200) Not working
(C5 - C4 - C7)(Baud: 115200) Not working
(C6 - C1 - C2 - C7)(Baud: 115200) Not working
(C5 - C1 - C2 - C7)(Baud: 115200) Not working
(C6 - C1 - C3 - C7)(Baud: 115200) Working
(C5 - C1 - C3 - C7)(Baud: 115200) Working
(C5 - C1 - C3 - C4 - C6)(Baud: 115200) Working
(C6 - C1 - C3 - C4 - C5)(Baud: 115200) Working
My problem is that my C2 and C4 are working correctly with each other but not working with C7 at all. But C3 works with everyone, who is the problematic one? Which one I need to change or examine. I am happy to answer any problems to fix this problem. Sorry for my explanation type. I could not find a better way to express this case.
You have wiring issues.
Bottom line: all RS232 ports should (and are) able to talk to each other. Same for RS485.
You should verify the pinout of each adaptor. Since they all use the same DB-9 connector for both sides (232 and 485) and you are combining three of them, it becomes very confusing.
For RS485 just take every pair of devices one by one and make sure you are wiring correctly DATA + on one side to DATA + on the other, same for DATA - and both GNDs together (and do not tie any other cables: +5V or +12 or whatever).
For RS232 just wire both GNDs together, TX on one side to RX on the other and vice versa. You will need more connections only if you use hardware flow control, so I would not use that for a first test.
Having said that, I will quote myself verbatim:
There are some nuances to the general R485 universal compatibility
stated above, like bus biasing or impedance (related to the
terminating resistors) that can be sometimes (mostly when you have to
cover long distances or you are in a noisy industrial environment) a
headache but you should always be able to solve those given enough
time, patience and ingenuity.
I guess you have all devices nearby so the above should not apply to your case. If after reviewing the wiring and pinouts you still have trouble with some combinations, it might be a good idea to use a scope to tap into the bus and see what's really going on. Or, if you have access to it, an even better tool.

Rete network without beta network?

I'm trying to draw a Rete network for a sample rule which has no binding between variables in different patterns. I know that beta network is used to make sure that the bended variable in different patterns are consistent.
(defrule R1
(type1 c1 c2)
(type2 c3)
=>
)
(defrule R2
(type2 c3)
(type3 c4 v1)
(type4 c5 v1)
=>
)
In R1, there is no binded variables between the two patterns, how should I combine their result in the Rete network then?
In R2, two rules have binded variable while the third has not. How to combine the three rules in the network?
I searched for Rete network example for such a situation but didn't find any. I tried to draw the network and below is my network. Is it right?
UPDATE: New network based on Gary's answer
Thanks
Beta nodes store partial matches regardless of whether there are variables specified in the patterns that need to be checked for consistency. The variables bindings just serve to filter the partial matches that are stored in the beta memory. If there are no variables, then all generated partial matches will be stored in the beta memories.
Your diagram should look like this:
a1 a2 a3 a4
\ / \ / /
b1 b2 /
| \ /
r1 b3
|
r2

Chandy/Misra dining philosophers solution

So based on the Chandy/Misra section in this Wikipedia article we've got 5 philosophers numbered P1-P5.
Based on this quote:
For every pair of philosophers contending for a resource, create a fork and give it to the philosopher with the lower ID (n for agent Pn). Each fork can either be dirty or clean. Initially, all forks are dirty
When a philosopher with a fork receives a request message, he keeps the fork if it is clean, but gives it up when it is dirty. If he sends the fork over, he cleans the fork before doing so.
So with the knowledge that all forks are initially dirty, regard the following quote and the image underneath it.
For every pair of Swansons, give the fork to the guy with the smaller id.
My question is if P3 now requests a second fork from his neighbor P2, will P2 give up his single fork because it was dirty, even though he just picked it up?
P3 cannot ask P4 for the fork because he already has the fork (acc. to the image).
The fork P4 is holding can only be shared by P4 and P5 (acc. to the problem, you can only ask your neighbors for the fork, which means P3 can only take the fork between P3 & P2 and the fork between P3 & P4 )
In other words, P4 cannot give P3 the fork which currently lies between P4 and P5
Therefore, P3 will have to wait until P2 gives him the second fork
** EDIT **
Yes P2 will give up the fork since it's dirty

Is this a minimal set-cover problem?

I have the following scenario (preliminary apologies for length, but I wanted to be as descriptive as possible):
I am presented with a list of "recipes" (Ri) that must be fulfilled, in the order presented, to complete a given task. Each recipe consists of a list of the parts (Pj) required to complete it. A recipe typically requires up to 3 or 4 parts, but might require as many as 16. An example recipe list might look like:
R1 = {P1}
R2 = {P4}
R3 = {P2, P3, P4}
R4 = {P1, P4}
R5 = {P1, P2, P2} //Note that more than 1 of a given part may be needed. (Here, P2)
R6 = {P2, P3}
R7 = {P3, P3}
R8 = {P1} //Note that recipes may recur within the list. (Same as R1)
The longest list might consist of a few hundred recipes, but typically contains many recurrences of some recipes, so eliminating identical recipes will generally reduce the list to fewer than 50 unique recipes.
I have a bank of machines (Mk), each of which has been pre-programmed (this happens once, before list processing has begun) to produce some (or all) of the available types of parts.
An iteration of the fulfillment process occurs as follows:
The next recipe in the list is presented to the bank of machines.
On each machine, one of its available programs is selected to produce one of the parts required by this recipe, or, if it is not required for this recipe, it is set "offline."
A "crank" is turned, and each machine (that has not been "offlined") spits out one part.
Combining the parts produced by one turn of the crank fulfills the recipe. Order is irrelevant, e.g., fulfilling recipe {P1, P2, P3} is the same as fulfilling recipe {P1, P3, P2}.
The machines operate instantaneously, in parallel, and have unlimited raw materials, so there are no resource or time/scheduling constraints. The size k of the bank of machines must be at least equal to the number of elements in the longest recipe, and thus has roughly the same range (typically 3-4, possibly up to 16) as the recipe lengths noted above. So, in the example above, k=3 (as determined by the size of R3 and R5) seems a reasonable choice.
The question at hand is how to pre-program the machines so that the bank is capable of fulfilling all of the recipes in a given list. The machine bank shares a common pool of memory, so I'm looking for an algorithm that produces a programming configuration that eliminates (entirely, or as much as possible) redundancy between machines, so as to minimize the amount of total memory load. The machine bank size k is flexible, i.e., if increasing the number of machines beyond the length of the longest recipe in a given list produces a more optimal solution for the list (but keeping a hard limit of 16), that's fine.
For now, I'm considering this a unicost problem, i.e., each program requires the same amount of memory, although I'd like the flexibility to add per-program weighting in the future. In the example above, considering all recipes, P1 occurs at most once, P2 occurs at most twice (in R5), P3 occurs at most twice (in R7), and P4 occurs at most once, so I would ideally like to achieve a configuration that matches this - only one machine configured to produce P1, two machines configured to produce P2, two machines configured to produce P3, and one machine configured to produce P4. One possible minimal configuration for the above example, using machine bank size k=3, would be:
M1 is programmed to produce either P1 or P3
M2 is programmed to produce either P2 or P3
M3 is programmed to produce either P2 or P4
Since there are no job-shop-type constraints here, my intuition tells me that this should reduce to a set-cover problem - something like the minimal unate set-cover problem found in designing digital systems. But I can't seem to adapt my (admittedly limited) knowledge of those algorithms to this scenario. Can someone confirm or deny the feasibility of this approach, and, in either case, point me towards some helpful algorithms? I'm looking for something I can integrate into an existing chunk of code, as opposed to something prepackaged like Berkeley's Espresso.
Thanks!
This reminds me of the graph coloring problem used for register allocation in compilers.
Step 1: if the same part is repeated in a recipe, rename it; e.g., R5 = {P1, P2, P2'}
Step 2: insert all the parts into a graph with edges between parts in the same recipe
Step 3: color the graph so that no two connected nodes (parts) have the same color
The colors are the machine identities to make the parts.
This is sub-optimal because the renamed parts create false constraints in other recipes. You may be able to fix this with "coalescing." See Briggs.

Resources