OrderedSet86 / gtnh-flow

Factory Optimization Flowcharts for Gregtech: New Horizons
MIT License
84 stars 24 forks source link

Removal of back edges prevents some designs from resolving recycling correctly #14

Closed Sopel97 closed 6 months ago

Sopel97 commented 1 year ago

This is the diagram I want, without [recycle] annotations. It does not work, crashes.

Diagram (1):

- m: mixer
  tier: HV
  I:
    diesel: 1000
    tetranitromethane: 20
  O:
    cetane-boosted diesel: 1000
  eut: 480
  dur: 1
  target:
    cetane-boosted diesel: 1000
- m: chemical reactor
  tier: MV
  I:
    nitric acid: 8000
    ethenone: 1000
  O:
    tetranitromethane: 2000
  eut: 120
  dur: 24
- m: chemical reactor
  tier: LV
  I:
    sulfur trioxide: 1000
    water: 1000
  O:
    sulfuric acid: 1000
  eut: 7
  dur: 16
- m: chemical reactor
  tier: MV
  I:
    acetic acid: 1000
    sulfuric acid: 1000
  O:
    ethenone: 1000
    diluted sulfuric acid: 1000
  eut: 120
  dur: 8
- m: distillery
  tier: LV
  I:
    diluted sulfuric acid: 75
  O:
    sulfuric acid: 50
  eut: 30
  dur: 1.5
- m: chemical reactor
  tier: LV
  I:
    methanol: 1000
    carbon monoxide: 1000
  O:
    acetic acid: 1000
  eut: 30
  dur: 15
- m: chemical reactor
  tier: MV
  I:
    hydrogen: 4000
    carbon monoxide: 1000
  O:
    methanol: 1000
  eut: 96
  dur: 6
- m: chemical reactor
  tier: LV
  I:
    charcoal: 1
    oxygen: 1000
  O:
    carbon monoxide: 1000
  eut: 7
  dur: 4
- m: chemical reactor
  tier: LV
  I:
    sulfur dioxide: 1000
    oxygen: 1000
  O:
    sulfur trioxide: 1000
  eut: 30
  dur: 10
- m: chemical reactor
  tier: LV
  I:
    sulfur: 1
    oxygen: 2000
  O:
    sulfur dioxide: 1000
  eut: 7
  dur: 3
- m: chemical reactor
  tier: LV
  I:
    nitrogen dioxide: 2000
    water: 1000
  O:
    nitric acid: 2000
    nitric oxide: 1000
  eut: 30
  dur: 12
- m: chemical reactor
  tier: LV
  I:
    oxygen: 1000
    nitric oxide: 1000
  O:
    nitrogen dioxide: 1000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: LV
  I:
    oxygen: 5000
    ammonia: 2000
  O:
    nitric oxide: 2000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: HV
  I:
    nitrogen: 1000
    hydrogen: 3000
  O:
    ammonia: 1000
  eut: 384
  dur: 16

This is the diagram (1) modified to actually compile, needed 2 [recycle] outputs. Notice, however, that the output is not ideal. It resolves to a positive production of sulfuric acid and nitric oxide. Diagram (2):

- m: mixer
  tier: HV
  I:
    diesel: 1000
    tetranitromethane: 20
  O:
    cetane-boosted diesel: 1000
  eut: 480
  dur: 1
  target:
    cetane-boosted diesel: 1000
- m: chemical reactor
  tier: MV
  I:
    nitric acid: 8000
    ethenone: 1000
  O:
    tetranitromethane: 2000
  eut: 120
  dur: 24
- m: chemical reactor
  tier: LV
  I:
    sulfur trioxide: 1000
    water: 1000
  O:
    sulfuric acid: 1000
  eut: 7
  dur: 16
- m: chemical reactor
  tier: MV
  I:
    acetic acid: 1000
    sulfuric acid: 1000
  O:
    ethenone: 1000
    diluted sulfuric acid: 1000
  eut: 120
  dur: 8
- m: distillery
  tier: LV
  I:
    diluted sulfuric acid: 75
  O:
    '[recycle] sulfuric acid': 50
  eut: 30
  dur: 1.5
- m: chemical reactor
  tier: LV
  I:
    methanol: 1000
    carbon monoxide: 1000
  O:
    acetic acid: 1000
  eut: 30
  dur: 15
- m: chemical reactor
  tier: MV
  I:
    hydrogen: 4000
    carbon monoxide: 1000
  O:
    methanol: 1000
  eut: 96
  dur: 6
- m: chemical reactor
  tier: LV
  I:
    charcoal: 1
    oxygen: 1000
  O:
    carbon monoxide: 1000
  eut: 7
  dur: 4
- m: chemical reactor
  tier: LV
  I:
    sulfur dioxide: 1000
    oxygen: 1000
  O:
    sulfur trioxide: 1000
  eut: 30
  dur: 10
- m: chemical reactor
  tier: LV
  I:
    sulfur: 1
    oxygen: 2000
  O:
    sulfur dioxide: 1000
  eut: 7
  dur: 3
- m: chemical reactor
  tier: LV
  I:
    nitrogen dioxide: 2000
    water: 1000
  O:
    nitric acid: 2000
    '[recycle] nitric oxide': 1000
  eut: 30
  dur: 12
- m: chemical reactor
  tier: LV
  I:
    oxygen: 1000
    nitric oxide: 1000
  O:
    nitrogen dioxide: 1000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: LV
  I:
    oxygen: 5000
    ammonia: 2000
  O:
    nitric oxide: 2000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: HV
  I:
    nitrogen: 1000
    hydrogen: 3000
  O:
    ammonia: 1000
  eut: 384
  dur: 16

Now. I don't know the reason why the first one crashes, nor the reason why the second doesn't solve it completely. What I know is that not removing the back edges fixes the issue, and actually shows where the reuse happens in the diagram. I think it's all more natural to include back edges in intrinsically cyclic processes; I don't understand why the backedges are being removed at all.

I think it should at least be an option to keep back edges, so that a larger set of diagrams can be resolved.

OrderedSet86 commented 1 year ago

The enforced removal of back edges was due to the old algorithm, which could not handle them properly. I think it is fine to add a config option, but it should be default KEEP_BACK_EDGES: false for reasons I'll explain below.

The new LP-based one ""can"" handle them, with the caveat that it can have a confusing choice of external product in the cycle (it can pick anything in a positive or negative recycling loop to sink/source) and this has downstream ramifications for the rest of the calculated recycling. You can read more about this here: https://github.com/OrderedSet86/flow2/blob/main/math.md

I'll take a look at your YAML files in a bit.

OrderedSet86 commented 1 year ago

Actually, it looks like this version of gtnh-flow doesn't even handle adding source/sink at all in cycles. For example, I took your non-[recycle] chart and removed the replenishing source of sulfuric acid. The solver just spits out "hey there's an unresolved variable" and quits.

- m: mixer
  tier: HV
  I:
    diesel: 1000
    tetranitromethane: 20
  O:
    cetane-boosted diesel: 1000
  eut: 480
  dur: 1
  target:
    cetane-boosted diesel: 1000
- m: chemical reactor
  tier: MV
  I:
    nitric acid: 8000
    ethenone: 1000
  O:
    tetranitromethane: 2000
  eut: 120
  dur: 24
- m: chemical reactor
  tier: MV
  I:
    acetic acid: 1000
    sulfuric acid: 1000
  O:
    ethenone: 1000
    diluted sulfuric acid: 1000
  eut: 120
  dur: 8
- m: distillery
  tier: LV
  I:
    diluted sulfuric acid: 75
  O:
    sulfuric acid: 50
  eut: 30
  dur: 1.5
- m: chemical reactor
  tier: LV
  I:
    methanol: 1000
    carbon monoxide: 1000
  O:
    acetic acid: 1000
  eut: 30
  dur: 15
- m: chemical reactor
  tier: MV
  I:
    hydrogen: 4000
    carbon monoxide: 1000
  O:
    methanol: 1000
  eut: 96
  dur: 6
- m: chemical reactor
  tier: LV
  I:
    charcoal: 1
    oxygen: 1000
  O:
    carbon monoxide: 1000
  eut: 7
  dur: 4
- m: chemical reactor
  tier: LV
  I:
    nitrogen dioxide: 2000
    water: 1000
  O:
    nitric acid: 2000
    nitric oxide: 1000
  eut: 30
  dur: 12
- m: chemical reactor
  tier: LV
  I:
    oxygen: 1000
    nitric oxide: 1000
  O:
    nitrogen dioxide: 1000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: LV
  I:
    oxygen: 5000
    ammonia: 2000
  O:
    nitric oxide: 2000
  eut: 30
  dur: 8
- m: chemical reactor
  tier: HV
  I:
    nitrogen: 1000
    hydrogen: 3000
  O:
    ammonia: 1000
  eut: 384
  dur: 16
OrderedSet86 commented 1 year ago

The first crash is probably a logic error with the implementation of the variable code, but I'm probably just going to ignore it in favor of development on flowv2...

OrderedSet86 commented 6 months ago

The IndexError issue is fixed now, and there is the KEEP_BACK_EDGES: false flag from earlier. Flow V2 dev is out of the scope of this issue so closing