Skip to content Skip to sidebar Skip to footer

How To Get Python GUI To Call A Genetic Algorithm Written In C

I'm new to Stack Overflow. I have a genetic algorithm written in C that accepts user input in the form of a number 0-100, and outputs an array of numbers. The C code is a full, s

Solution 1:

I assume that you are able to store the text that the user enters in a variable? If not, this question explains it pretty nicely. Anyway, once you get that, call subprocess.check_output like this:

 result = subprocess.check_output(["./cexecutable", inputValue])

(replace "cexecutable" with the name of the executable of your genetic algorithm program and inputValue with whatever variable you're storing the input in)

This should store the output of the genetic algorithm in result. It will be all one string, so if there are multiple lines of output you'll probably want to call result.split("/n") to get a list of lines. You can then parse them and put them into the array as you see fit, based on how they're formatted.

Assuming that you have some sort of "enter" button associated with the text box, and you're doing this all as an event that occurs when the button is clicked, this will happen every time the user enters new text and clicks the button.

EDIT (in response to comment): To keep the program running in the background, you'll need to use subprocess.Popen and Popen.communicate. Rather than just returning the output of your program, this will create a Popen object that you can continue to interact with. Without testing this on your code, I can't guarantee that the code below will do exactly what you want, but I think it should be close:

 genAlg = subprocess.Popen(["./executable"])
 ...
 #user enters data
 ...
 result = genAlg.communicate(inputValue)[0] 
 #communicate sends the given argument to stdin, and returns a 
 #tuple (stdout, stderr) - you want stdout, hence the "[0]"

EDIT 2: Turns out that's not actually a workable solution - see J.F. Sebastian's comments below.


Post a Comment for "How To Get Python GUI To Call A Genetic Algorithm Written In C"