Logic network long(ish) expressions

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Roaders
Inserter
Inserter
Posts: 28
Joined: Mon Jan 15, 2024 1:33 pm
Contact:

Logic network long(ish) expressions

Post by Roaders »

Hi All

I am trying to control the unloading of a train at one of my stations. The expression I want to evaluate is something like this (where T = train count and B = base count)

(Bammo > 500 || Tammo == 0 ) && (BOil > 20000 || TOil == 0) && (BRobot > 50 || TRobot == 0) && (BRepairPack > 50 || TRepairPack = 0)

so it seems to me that I need a whole load of combinators for this...
  • I need 8 combinators to evaluate each of the expressions (Bammo > 500)
  • I need 4 combaintors to OR the base / train expressions
  • I need 3 combinators to AND the 4 expressions in a tree structure (2 combinators AND 2 goods each, third combinator then ANDs those 2 combinators)
Is there an easier way than this? (it will actually be more complex than this as I have more goods I want to unload)

For ANDing all the individual goods the EVERY operator is pretty useful... but not quite. As each decider outpus 1 or nothing I can't use Every as if a combinator evaluates to false it just doesn't output anything. Is there a way around this?

Many Thanks

NineNine
Fast Inserter
Fast Inserter
Posts: 140
Joined: Mon Oct 24, 2022 11:20 pm
Contact:

Re: Logic network long(ish) expressions

Post by NineNine »

It's not any easier than this.

9/10 times, there's a way to do what you're trying to do without circuits. I built a vanilla megabase with no combinators or deciders at all, for example. Of course, people do crazy stuff, writing programs that do all sorts of stuff with the combinators and deciders, but you usually don't need them to play and enjoy the game.

computeraddict
Fast Inserter
Fast Inserter
Posts: 176
Joined: Sat Oct 07, 2023 6:44 am
Contact:

Re: Logic network long(ish) expressions

Post by computeraddict »

It can be done in 10.

Try leveraging the fact that wires are a free addition as much as possible. I think you'll need 8 decider combinators for the threshold expressions.

After that, though, you'll only need two deciders.

For each threshold condition have them output 1 of a signal unique to its OR group. Sum the output of all of them with a wire, so you should have 0-2 of four different signals. Run them through another decider with "each > 0, 1 whatever", then a final decider of "whatever >= 4, 1 success".

Pi-C
Smart Inserter
Smart Inserter
Posts: 1720
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Logic network long(ish) expressions

Post by Pi-C »

Roaders wrote:
Mon Jan 22, 2024 12:42 pm
Hi All

I am trying to control the unloading of a train at one of my stations. The expression I want to evaluate is something like this (where T = train count and B = base count)

(Bammo > 500 || Tammo == 0 ) && (BOil > 20000 || TOil == 0) && (BRobot > 50 || TRobot == 0) && (BRepairPack > 50 || TRepairPack = 0)

so it seems to me that I need a whole load of combinators for this...
  • I need 8 combinators to evaluate each of the expressions (Bammo > 500)
Correct, for each thing you need two decider combinators: Signal>value --> output B(1), Signal==0 --> output T(1).
  • I need 4 combaintors to OR the base / train expressions
Correct. For each pair of deciders, you feed B and T into one arithmetic combinator: B | T --> output X
  • I need 3 combinators to AND the 4 expressions in a tree structure (2 combinators AND 2 goods each, third combinator then ANDs those 2 combinators)
Too complicated! Feed the output from all arithmetic combinators into another decider: X=4 --> output GREEN(1)
Is there an easier way than this? (it will actually be more complex than this as I have more goods I want to unload)
You'll always need 2 deciders + 1 arithmetic combinator for each of the goods, but no matter how many goods you have, they'll all feed into the last decider. All you have to do is adjust the value of X required to output GREEN to the count of goods.


In this blueprint, I've used two constant combinators to simulate input from Base (left) and Train (right):

A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Roaders
Inserter
Inserter
Posts: 28
Joined: Mon Jan 15, 2024 1:33 pm
Contact:

Re: Logic network long(ish) expressions

Post by Roaders »

Thanks for the replies. I'd already figured out the summing up to whatever total I want but I was hoping that I could use the Every trick somehow. I wish that you could specify an else value in combinators so taht we output a value to network for both true and false evaluations...

mmmPI
Smart Inserter
Smart Inserter
Posts: 3560
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Logic network long(ish) expressions

Post by mmmPI »



Does that work ?

1 constant for the threshold/request necessary for the build

1 constant to simulate train storage , 1 constant to simulte base storage, using the same value as in the previous example

Edit: sorry when removing the train station i plugged wrongly the upper left constant to simulate it and updated the blueprint
Last edited by mmmPI on Mon Jan 22, 2024 3:36 pm, edited 1 time in total.

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

Roaders wrote:
Mon Jan 22, 2024 12:42 pm

(Bammo > 500 || Tammo == 0 ) && (BOil > 20000 || TOil == 0) && (BRobot > 50 || TRobot == 0) && (BRepairPack > 50 || TRepairPack = 0)
It seems you have 4 items to handle: ammo, oil, robots, repair packs.
The expression should evaluate to true, if each of the materials either has 0 in the train or above some threshold in some warehouse (chests).

Read the train and wire it to one decider. Additionally, wire a constant combinator with ammo, oil, robot, repairpack all set to -1. Condition in the constant combinator: EACH = -1, output EACH=1. This will output a 1 for every of the 4 material that is empty in the train.

Now to the chests and storage tanks. Wire them all together and read content. In a constant combinator, set values ammo=-500, oil=-20000, robot=-50, repairpack=-50. These are our thresholds in negative. Use a decider combinator and wire the chests/tanks as well as the constant combinator to the input of that decider combinator. We "normalized" the values, so whatever value was above the threshold is now > 0, if the condition should be true.

Wire the output of both decider combinators together. For every signal, we get 0 if both conditions for one of the materials were false or > 0 if at least one of the inner conditions were true. So connect these to the input of a 3rd decider combinator. Condition EACH > 0, output O=1. This will count how many of the inner OR conditions are true and output the amount to O. If the result is O=4, our whole statement is true. So either directly use O=4 as condition in the device you intend to control, or add another decider combinator to further use O=4 or O <> 4.

This setup works only, if there are only these 4 items in the warehouse counting circuit. If there are other items, they will count as true with the >0 condition and taint the result. On the other side, the train content is irrelevant, there can be arbitrary other items in the train, that doesn't matter.

In my example, I simulated the warehouse content on the left with the top left constant combinator, and I simulated the train content with the top right constant combinator. I put some arbitrary values into it, 2 materials in the warehouse above threshold and 2 materials in the train being 0.
In combination, all 4 sub conditions are true, so the lamp lights up. If you switch off the left constant combinator to simulate an empty warehouse (everything below threshold), the lamp goes off, since only the 2 sub conditions from the train remain true.

The thresholds are saved as negative values in the lower left constant combinator, and the 4x -1 in the lower right combinator are required to check for something being empty in the train.
Screenshot 2024-01-22 162911.png
Screenshot 2024-01-22 162911.png (278.16 KiB) Viewed 2202 times
Last edited by Tertius on Mon Jan 22, 2024 3:48 pm, edited 1 time in total.

mmmPI
Smart Inserter
Smart Inserter
Posts: 3560
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Logic network long(ish) expressions

Post by mmmPI »

Tertius wrote:
Mon Jan 22, 2024 3:34 pm
The expression should evaluate to true, if each of the materials either has 0 in the train or above some threshold in some warehouse (chests).
I don't understand this part, i had it the other around for most of the time i tried fiddling, it was checking if some materials where under some threshold. Supposedly in my mind, if the request is still pending, and the train has no more of it, then output 1, to trigger its departure, i thought it was similar to using inactivity condition somehow.

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

mmmPI wrote:
Mon Jan 22, 2024 3:47 pm
I don't understand this part, i had it the other around for most of the time i tried fiddling, it was checking if some materials where under some threshold.
The 4 inner OR conditions are always "Bsomething>value OR Tsomething=0". I interpret the Bsomething>value term as "something above value" or better "something above threshold". Since B refers to the base, I assume this is referring to some chest/tank storage. T refers to the train, so I assume this is referring to stuff carried in the train.

The question is about unloading trains, but the whole expression makes only sense for me if it is checking if I should NOT unload from a train. You cannot unload from a train if its content is 0, and you don't want to unload from a train if you already have more than enough (above threshold).

However, for an unloading station, you want to handle each material independently. If you have below threshold for some material, you unload this, but only if you have some in the train. If you have above threshold for some material, you skip that material but still check all other material - and this is what the condition in the OP isn't saying. It just gives one result for all 4 materials checked. It cannot say if only one material needs unloading.

If you actually want to dynamically unload material you don't have enough, and skip material where there is none in the train, you have to as the question differently and involve actual loading/filtering with inserters and filter inserters. I only answered the given question. A really nice question, if you ask me :)

mmmPI
Smart Inserter
Smart Inserter
Posts: 3560
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Logic network long(ish) expressions

Post by mmmPI »

Tertius wrote:
Mon Jan 22, 2024 3:59 pm
for me it is checking if I should NOT unload from a train. You cannot unload from a train if its content is 0, and you don't want to unload from a train if you already have more than enough (above threshold).
That make sense now.
Tertius wrote:
Mon Jan 22, 2024 3:59 pm
However, for an unloading station, you want to handle each material independently. If you have below threshold for some material, you unload this, but only if you have some in the train. If you have above threshold for some material, you skip that material but still check all other material - and this is what the condition in the OP isn't saying. It just gives one result for all 4 materials checked. It cannot say if only one material needs unloading.
I realize now why the last decider is setup differently in your blueprint, i changed it so that it shows 4 if 4 signals are above 0.



It can be refined using overflow i suppose, as such it is doing the same thing as yours with more combinators because i didn't use the 4x -1 constant, because that is annoying to setup if you have to do it for many component and you already have to setup the threshold so it recreate a 4x -1 from the threshold.

Also this will change in 2.0 !

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

@Roaders
How will the complete picture of your unloading station will look like? You asked one isolated question, but as long as we don't see the complete picture, we might be unable to give answers that are directly helpful for you solving your problem. We might even try to solve an X-Y problem instead of the problem at hand.

On the other hand, if you describe too much, we might be attempted to provide a solution you want to create yourself.

It may be you're trying to create a station similar to what I created for my base. I designed a train that carries a fixed assortment of stuff in each wagon. At unloading stations, constant combinators define the stuff to unload from each wagon. Unloading is done by using one stack filter inserter per wagon into one chest per wagon.
If you have 40 of some material and want 50, it unloads 10. If you have 60 of some material and want 50, it unloads none.

If the train only has 2 and you want 10, it unloads 2 and doesn't stall the filter inserter. This is the crucial thing: don't make the inserter stall on a thing it cannot grab, because it doesn't exist.
I thought about that problem a few hours, and the solution is surprisingly simple.
Compute the actual demand (subtract items already present from demand). Compare this actual demand and train content and find the minimum of both values. Unload the minimum. This way you're not trying to unload stuff that's not actually in the train.

You can also word this solution differently. This is how I implemented it.
Compute the total amount of items: train content plus items already stored in the station.
Subtract the demand from the total amount. Filter out negative values. This gets rid of items we cannot unload.
From the result, subtract the items already stored in the station. This restores the number of items in the train, but items filtered out we cannot unload.
From what is left, unload from the train.

This requires three global combinators and 2 additional combinators per wagon, and one constant combinator per wagon to define what to unload.

Roaders
Inserter
Inserter
Posts: 28
Joined: Mon Jan 15, 2024 1:33 pm
Contact:

Re: Logic network long(ish) expressions

Post by Roaders »

Wow guys. Thanks very much for your long replies. They obviously took some time to compose. I now need some time to understand them!!!

@Tertius

I have a train that I want to load a bunch of stuff onto. At the unloading station I have an inserter per type of good to unload the goods. I want to keep unloading the item until the value in the constant combinator is reached BUT - I don't want the train to wait there forever if it runs out of that good.

Using the negative values in the combinators and combining with the real values and seeing if it's above 0 is a clever way to think about it. Without being to check for null values this might be the next best thing!

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

Roaders wrote:
Mon Jan 22, 2024 5:16 pm
Wow guys. Thanks very much for your long replies. They obviously took some time to compose.
It's much fun to work out a solution for an interesting question. Especially your question to boil down your long expression was much fun.

Using negative values one way or the other is an important tool for sorting out certain items from a list of random items.

Sometimes, it's also useful to "mark" items by making them negative, do something with marked or unmarked items, then remove the mark again, or filter out marked or unmarked items. There are 3 ways to make things negative: you can subtract a large value, you can multiply it with -1, or you can OR it with a value of 2^31 (enter 2147483648 as value). The last operation is special, it just sets the most significant bit, and since this is the bit where the 32-bit integer arithmetic of the combinators stores the sign, arithmetic detects the new value as negative, although the actual value became invalid. You can reverse that operation with the AND operation with value 2147483647. This is all bits 1 but the sign bit.
What method you prefer is up to you. Most often, multiplying with -1 is best, because you can directly use also the negative value and reverse it with multiplying with -1 again.
Using the OR operation can be useful for filtering and is more a special case.

Roaders
Inserter
Inserter
Posts: 28
Joined: Mon Jan 15, 2024 1:33 pm
Contact:

Re: Logic network long(ish) expressions

Post by Roaders »

I did have an issue where exactly the right number of items were in the storage chests. If I want 50 items (set to -50 in constant combinator) and I had 50 items then the values cancelled out, not to produce 0 but to produce no value at all (I really hate this feature).

To solve this I added combinator to add 1 to each constant value. -50 becomes -49, when I combine them it becomes 1.

Is this a sensible solution?

mmmPI
Smart Inserter
Smart Inserter
Posts: 3560
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Logic network long(ish) expressions

Post by mmmPI »

adding an offset of 1 is a sensible solution to me, since numbers are integers and representing quantity, the smallest quantity is the one that will turn -1 into 0 or 0 into 1, if you can't detect signal = 0 then if you add 1, you have to detect signal = 1

That's very similar to how i checked if the train still has some useful material in my mind, from one constant combinator holding the positive threshold value , they are transformed into just "1"s for all signal that are requested, then multiplied by -1 for all -1's, then additionned with the quantity inside the train.

This means removing 1 to all train's quantity instead of adding 1 like you do, but it is used for the same reason. Because then it's easier to filter all negative signal, instead of "0", if the train is empty, it will show "-1", if the train has not nothing but just a little left, the smallest amount, 1, then it is going to be detected, because it will yield 0, and 0 is not negative, so it is a way to only show the interesting signals that are "0"and not every single signal in the game that are 0 because not used.

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

If you want 50 items and have 50 items, why is canceling out both signals an issue? You don't need to do anything with that material, you have neither demand nor surplus. If you want to control an inserter, material > 0 as activation condition is false if material = 0.

Having to add +1 or -1 to have a value instead of no value is a sign of something not done right.

I agree the handling of zero is ... interesting ... but if you get used to it, it has some interesting properties.
Sometimes you can use the ANYTHING or EVERYTHING wildcard to get the right thing out of your values. These are for handling multiple signals, but for a single signal these have their value as well.

The EVERYTHING wildcard has the interesting property that it can convert the absence of signals to true. Usually, it's only true if every signal matches the condition. But if there is no signal at all, so the condition isn't evaluated at all, it results in true as well. Even if the condition is EVERYTHING > 0, which should theoretically result in false, the result is actually true.
This can get on your nerves, but it can save your butt as well - depending on what your need.

The ANYTING wildcard, on the other hand, is the opposite. It will ignore 0. If it gets zero input, it results in false - even if the condition is ANYTHING = 0, which should theoretically result in true.

You can use this behavior for your benefit. Create lookup tables, if you don't know what input values will result in what output values.

mmmPI
Smart Inserter
Smart Inserter
Posts: 3560
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: Logic network long(ish) expressions

Post by mmmPI »

Tertius wrote:
Mon Jan 22, 2024 7:26 pm
Having to add +1 or -1 to have a value instead of no value is a sign of something not done right.
Isn't that what you are doing with the constant combinator that adds 4x -1 on the right side ?

Roaders
Inserter
Inserter
Posts: 28
Joined: Mon Jan 15, 2024 1:33 pm
Contact:

Re: Logic network long(ish) expressions

Post by Roaders »

Tertius wrote:
Mon Jan 22, 2024 7:26 pm
If you want 50 items and have 50 items, why is canceling out both signals an issue? You don't need to do anything with that material, you have neither demand nor surplus. If you want to control an inserter, material > 0 as activation condition is false if material = 0.

Having to add +1 or -1 to have a value instead of no value is a sign of something not done right.

I agree the handling of zero is ... interesting ... but if you get used to it, it has some interesting properties.
Sometimes you can use the ANYTHING or EVERYTHING wildcard to get the right thing out of your values. These are for handling multiple signals, but for a single signal these have their value as well.

The EVERYTHING wildcard has the interesting property that it can convert the absence of signals to true. Usually, it's only true if every signal matches the condition. But if there is no signal at all, so the condition isn't evaluated at all, it results in true as well. Even if the condition is EVERYTHING > 0, which should theoretically result in false, the result is actually true.
This can get on your nerves, but it can save your butt as well - depending on what your need.

The ANYTING wildcard, on the other hand, is the opposite. It will ignore 0. If it gets zero input, it results in false - even if the condition is ANYTHING = 0, which should theoretically result in true.

You can use this behavior for your benefit. Create lookup tables, if you don't know what input values will result in what output values.
The issue is that I no longer get a value of 4 to signal that the train should leave because one of the values cancels out and is not included. I think I can use the everything wild card though. If I say everything must be greater than 0 then if the value doesn't exist the train can still leave. Thanks again

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

mmmPI wrote:
Mon Jan 22, 2024 8:01 pm
Tertius wrote:
Mon Jan 22, 2024 7:26 pm
Having to add +1 or -1 to have a value instead of no value is a sign of something not done right.
Isn't that what you are doing with the constant combinator that adds 4x -1 on the right side ?
Yes and no. The -1 is used to mark a signal, not to increase or decrease a value so it isn't zero any more.

Both halves of the condition have to have compatible operands: 0 for false and 1 for true in both halves. So I convert zero to negative to mark the zeroes. Positive values destroy the mark. Then filter all surviving negatives and create 1 for each match. This also filters out any other material that might be on that train.

Tertius
Filter Inserter
Filter Inserter
Posts: 891
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Logic network long(ish) expressions

Post by Tertius »

Roaders wrote:
Mon Jan 22, 2024 8:12 pm
The issue is that I no longer get a value of 4 to signal that the train should leave because one of the values cancels out and is not included. I think I can use the everything wild card though. If I say everything must be greater than 0 then if the value doesn't exist the train can still leave. Thanks again
I developed a lengthy and elaborated answer and wanted to show how to do things without that ugly +/-1. After some time with no success, I looked into my existing station to see how I solved it in there.
Well, I didn't solve it. Sorry.

Instead, I used "5 seconds inactivity" as waiting condition in the train schedule. This means, if there is no inserter or fluid pumping activity for more than 5 seconds, the train will leave. But as long as there is activity, the train will stay.

Post Reply

Return to “Gameplay Help”