#๐Ÿ”’ need help with bfs algorithm for balancing signal strengths

66 messages ยท Page 1 of 1 (latest)

brisk rivet
#
def find_path(data, instructions, num_outputs):
    queue = [QueueEntry(data, [])]
    while queue:
        queue_entry = queue.pop()
    
        if len(queue_entry.data) == num_outputs and all(c == queue_entry.data[0] for c in queue_entry.data):
            return queue_entry.data
        for instruction in instructions:
            if instruction not in queue_entry.used: # need to replace this line with something
                queue.append(QueueEntry(
                        [*instruction(*queue_entry.data)], 
                        queue_entry.used + [instruction]
                    )
                )

signals = [Signal({'A': 0.5, 'B': 0.5}), Signal({'B': 1})]
instructions = [Splitter.split,]

print(find_path(signals, instructions, 2))

so this works but only for len(signals) == 2 because of Splitter.split only able to receive 1-2 inputs

need help making this work for any len(signals) and instructions being able to be used more than once without it spiraling into an infinite loop pithink

magic flaxBOT
#

@brisk rivet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

brisk rivet
iron hazel
#

Sily question, but why not just make the inital queue have an item for each item in data?

#

@brisk rivet ^^

#

Assuming you're trying to write split_all()?

iron hazel
#

You were talking about any number of things instead of 2.

brisk rivet
#

if i split 3 items into 3 entries

#

problem still there

#

how to partition that to make sure its split correctly

#

since i can only receive 2 at a time with Splitter.split

iron hazel
#

Maybe you should provide an example. What an item?

brisk rivet
#

by item i mean signal

#

signals i gave in original post has 2

#

signals = [Signal({'A': 0.5, 'B': 0.5}), Signal({'B': 1}), Signal({'B': 1}), Signal({'B': 1})] this one has 4 but it gives TypeError: Splitter.split() takes 2 positional arguments but 4 were given

iron hazel
#

What about

def split(signals): # accepts a list?
brisk rivet
#

which i expected but i also dont know how to properly fix

brisk rivet
#

if thats what u asking

#

reason i dont is cause i need to use the instructions given

iron hazel
#

I am. So why not accept a list of singals?

brisk rivet
iron hazel
#

Splitter.split doesn't accept any instructions? What are they, what do they mean?

brisk rivet
#

think of instructions like chess moves, i can only modify signals that way pithink

#

Splitter.split currently only one since it should be possible to do any problem just with that

iron hazel
#

Oh, so splitter.split is an instruction, needs to accept any number of positional parameters.

brisk rivet
#

nah it can only accept 1 or 2 arguments

iron hazel
#

So:

@staticmethod
def split(*signals):
    .....
brisk rivet
#

so question is how to partition the current queue entry data into it

iron hazel
#

But you're passing *data ? Might be any number.

#

Ah

#

Well, how do you want to partition it?

#

Halve it until there's only 1 or 2?

#

Skim along it in pairs, then combine the pairs?

#

Throw out all the Nones first?

brisk rivet
#

im asking how its done cause i dont know how to do it without it spiraling to an infinite loop or it just not balancing it

#

last night i had 2 python processes taking up 9gb of ram cause of this problem

iron hazel
#

If you don't have a spec, make your own.

  • discard all Nones
  • partition data into pairs, spit them and put the split back?
#

But really, you need to know what is meant to happen, then implement that.

brisk rivet
iron hazel
#

Supposing you got a:0.5,b:0.25,c:0.10 what should the final result be?

brisk rivet
#

multiples of 3 are definetly the ugliest

iron hazel
#

So 3 identials Signals each Signal({'a':0.5/3, 'b':0.25/3, 'c':0.10/3}) ?

brisk rivet
#

ye

iron hazel
#

Sounds like Splitter.split wants more parameters. Tell me again why it may only accept 2?

#

Why can't it accept all the items from data?

brisk rivet
#

thats just the instruction pithink

#

if u want to know where instruction came from

#

uh

#

factorio

#

it takes 1-2 inputs and has 1-2 outputs

#

do i just need to look into how chess engines are made for this kind of thing .-.

iron hazel
#

I don't know. Your problem has little to do with graphs and everything to do with splitting your division across 1-tuples or 2-tuples.

How about you: count all the nonNones in data. Call that n, the number of input signals.
Walk data in pairs (using range() with a step of 2 maybe):
if you've got a pair of numbers (a,b) pass in a/n*2, b/n*2, which will get you a/n,b/n out the end.
Adjust.

molten geyser
#

The processing order doesn't matter, right? You just need to compose enough split operations that you can approximate the numbers you want as binary fractions, it seems to me.

iron hazel
#

That would be my assumption. The math above should be exact.

#

I think.

molten geyser
#

Yeah but if I understand the problem statement, he wants to do it entirely in terms of that binary split operation, so giving exact answers is 'cheating'

iron hazel
#

But the above does use binary operations.

molten geyser
#

Only when n is divisible by 2 though right?

#

Oh, no, I'm just tired I guess. Yeah.

magic flaxBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.