-1

I actually develop an AI that use reinforcement learning, and i am searching for a way to modify a variable (that stay at the value after use, for exemple if i have a value 'pos' at 10, and then i change the value of 'pos' to 11, 'pos' needs to stay at 11, even after the closing of the main program.) in another program that store progress of the AI (call it 'training' for exemple), all that using code in my main script. I am not using any modules except random, i try to make an AI without, it's part of the challenge. More precisely, i want to update values tables of 'training' with my main script, to make the AI progress in its choices. I've seen someone done it in Java, but i can't figure out how to do it in Python.

Code:

file 'main':

import training_board as tb

def Main():
    def AI():

    # do AI things
    # need to change value of 'pos' in 'training_board'
    # code that i need, who will change the value of 'pos' in 'training_board' PERMANENTLY
    # go back at doing other AI things

Thanks for answearing ! If you need more specific info, please write them in the comments, im kinda new to this ^^'

Ps: English isn't my first language, so i apologize for grammar errors and stuff ^^'

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
BaronHeal
  • 3
  • 2
  • 1
    you are going to need to ad more details, your description is too vague. Are these two independent processes? If so, do they have to be? Because generally, you should write your programs to be modular, and you import the functionality into a main driver program. If they have to be two independent processes for whatever reason, then you are going to have to use some form of interprocess communication. This is too vague and unspecified as it stands. You ened to elaborate. – juanpa.arrivillaga Apr 17 '20 at 08:32
  • what exactly are you looking for? If you are using tensorflow (which I would recommend if you are using python...) you can just export and import models (https://www.tensorflow.org/tfx/serving/serving_basic) otherwise you can just write out the matrices representing your model to a .csv or other file using pandas or numpy... – Chris Apr 17 '20 at 08:34
  • 1
    Now it sounds like you want some form of *data persistence*. The data doesn't exist in your file, certainly you should not think of it that way. Your python file contains *source code*, which will essentially create objects in main memory in the python runtime process that is executing your script. If you want data to be maintained between different runs of a process, you need some form of data persistence. The simplest method would be something like saving your data to a file and reading it when needed. Again, this is all to vague to give any specific advice – juanpa.arrivillaga Apr 17 '20 at 10:05
  • yes, thaks you very much ! I'll search deeply before asking next time :) – BaronHeal Apr 17 '20 at 10:56

2 Answers2

0

It is actually quite simple - you can modify the variable in another module by just changing the variable in the module's scope. For example if you have two programs main.py and config.py and you want to change the value of the variable x that is defined in config.py, you can write config.x = new_value in main.py.

As @juanpa.arrivillaga correctly pointed out though, it is questionable whether that is really a good way to go - heavily depending on your specific problem.

sempersmile
  • 481
  • 2
  • 9
  • Edit: oh, actually, it's not what im looking for. I try to modify a variable witch, after being modified, stays, like a value of 10 for the variable 'pos1', that i increment by 1, and who then stay at 11 in the file, even after the closing of the main program. Im not sure if i explain correctly... – BaronHeal Apr 17 '20 at 08:58
  • @BaronHeal you must provide a [mcve] your explanations are too vague. Provide the code you are actually using and a description of how you are running it – juanpa.arrivillaga Apr 17 '20 at 09:30
  • @juanpa.arrivillaga the code i'm using is a little bit messy... I just want to permanently change a value in a python file, using another python file. – BaronHeal Apr 17 '20 at 09:50
  • 1
    @BaronHeal **again** that is to vague. Please add a [mcve]. Please add a full description of **exactly** what you are trying to accomplish. What do you mean change a value "in a python file"??? You want to change the source code? I doubt it.. so what exactly do you mean? What exactly do you mean by "using another python file"?? You never answered my initial questions about what exactly you are doing. Is this a single python process? Are you importing a module? Or is this two separate processes? If so, does it have to be? – juanpa.arrivillaga Apr 17 '20 at 10:00
  • @juanpa.arrivillaga it's two files, one is the module, the other the main. i try to, with the main file (my python source code), change values in the module, that stays forever in the module, and can be reused after by the main file. – BaronHeal Apr 17 '20 at 10:49
0

Welcome to Stackoverflow! Cheers to great learning ahead. As pointed out by others, this is very much possible, but it is not suggested to do so.

Also, If the variable is bound to be changed, then the better solution will be to store this value somewhere on disk and read it every time from the disk whenever you need it.

There are 2 possible scenarios that I can see:

Scenario 1: training board is written by someone else and you are using it. In this case it is a big "NO" to change the pos value in tiger_board as it might be being used by tiger_board's own functioning as well.

Scenario 2: training_board is written by you. If you are the one coding it, then store the pos variable in a separate file, and read it during initialization.

Hope this helps! Thanks.

Ikhurana
  • 154
  • 1
  • 7
  • thanks, it helps very much, and i am the one that coded training_board, sorry for not mentioning it. But how can i do for multiple variables ? I have 30 to test actually, and i've heard that a file re-write itself for every modifications, so how can i separate the values in distincts blocks that i can reuse, without creating 30 files ? – BaronHeal Apr 17 '20 at 10:46
  • @BaronHeal 30 is nothing, just read the whole file, then write the new data. You can use something like JSON as a serialization format, or just `pickle` too – juanpa.arrivillaga Apr 17 '20 at 10:51
  • @juanpa.arrivillaga Ok, thanks, i think i see where i am going now. Thanks you all for your kindness, despite i am a newbie in stack and cannot explain properly ^^' – BaronHeal Apr 17 '20 at 10:53
  • @BaronHeal thank you for responding to our requests for clarification. The more you engage, the more you learn. Half the battle is learning how to describe your problems effectively! – juanpa.arrivillaga Apr 17 '20 at 10:55
  • yes, thanks again ! – BaronHeal Apr 17 '20 at 10:58
  • BaronHeal: your welcome! and I agree with @jaunpa.arrivillaga. All your engagement helped us to understand your problem better and provide correct suggestions. So, Thank you for that and Happy Learning! – Ikhurana Apr 17 '20 at 11:24