Я пытаюсь написать код, который будет генерировать значения для таблицы выше. Эти значения будут сгенерированы при соблюдении набора правил:
Текущее состояние и следующее состояние не могут быть одинаковыми. Таким образом, если A является текущим состоянием, то следующее состояние должно быть либо B, либо C.
Значения будут генерироваться случайным образом при каждом запуске программы.
Все строки с одинаковым текущим состоянием, действием и следующим состоянием должны иметь одинаковые награды.
Проблема в том, что, хотя я создал списки для случайного выбора значений, я все еще не могу генерировать значения, которые не будут нарушать правило 1. Каждый раз, когда я запускаю код, правило 1 нарушается. Код для программы можно увидеть ниже.
import random
# Creating lists of variables that will be used to pick a next state at
random while standing on a current state
NSA = ['B', 'C'] # List of states reachable from A. We use this list to pick
a next state at random
NSB = ['A', 'C'] # List of states reachable from B. We use this list to pick
a next state at random
NSC = ['A', 'B'] # List of states reachable from B. We use this list to pick
a next state at random
Current = ['A', 'B', 'C'] # List of possible current states
RandomList = [-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0] # List
of possible rewards for actions taken
Action = ["Clockwise", "Counterclockwise"] # List of possible actions taken
from a current state
# We need to make sure we know what the current state is, what the action
will be and what would be the next state and have a reward for it
# Since there are 2 possible states reachable from A after a clockwise
action, we create and declare 2 variables. These variables will be used to
pick the next
# state at random. Having 2 variables ensures that all the entries are not
populated by a single state and thus makins sure we have both states
populating the column for the action. We then do this for counterclockwise
action and repeat it for everystate
# Since python doesnt allow to declare variables without instantiation,
hence we are assigning a value to each state. This wont matter as we will
later be using the random function to choose a value from the respected
lists declared above.
# Variables as mentioned above for state A. Note cw = clockwise and ccw =
counterclockwise
AnScw1 = "B"
AnScw2 = "C"
AnSccw1 = "B"
AnSccw2 = "C"
# Variables as mentioned above for state B. Note cw = clockwise and ccw =
counterclockwise
BnScw1 = "A"
BnScw2 = "C"
BnSccw1 = "A"
BnSccw2 = "C"
# Variables as mentioned above for state C. Note cw = clockwise and ccw =
counterclockwise
AnScw1 = "A"
AnScw2 = "B"
AnSccw1 = "A"
AnSccw2 = "B"
# creating variables to pick rewards randomly
a_cw_r1 = random.choice(RandomList)
a_cw_r2 = random.choice(RandomList)
a_ccw_r1 = random.choice(RandomList)
a_ccw_r2 = random.choice(RandomList)
b_cw_r1 = random.choice(RandomList)
b_cw_r2 = random.choice(RandomList)
b_ccw_r1 = random.choice(RandomList)
b_ccw_r2 = random.choice(RandomList)
c_cw_r1 = random.choice(RandomList)
c_cw_r2 = random.choice(RandomList)
c_ccw_r1 = random.choice(RandomList)
c_ccw_r2 = random.choice(RandomList)
current = random.choice(Current) # Pick at randomly a current state from the list Current
if (current=="A") :
action = random.choice(Action) # pick a action at random, from the list of actions
if (action=="Clockwise") : # if clockwise action picked
AnScw1 = random.choice(NSA) # randomly choose a next state
AnScw2 = random.choice(NSA) # randomly choose a next state
# if the two states are the same, then change the values. So if any one of two has B as next state, the other is
# is assigned C as the next state.
if (AnScw1 == "B" or AnScw2 == "B") :
if (AnScw1 == AnScw2 and AnScw1 == "B") :
AnScw2 = "C"
a_cw_b_r1 = random.choice(RandomList) # pick a reward from list at random
a_cw_b_r2 = random.choice(RandomList) # pick a reward from list at random
if (AnScw1 == "C" or AnScw2 == "C") :
if (AnScw1 == AnScw2 and AnScw1 == "C") :
AnScw2 = "B"
a_cw_r1 = random.choice(RandomList)
a_cw_r2 = random.choice(RandomList)
if (action=="Counterclockwise") :
AnSccw1 = random.choice(NSA)
AnSccw2 = random.choice(NSA)
if (AnSccw1 == "B" or AnSccw2 == "B") :
if (AnSccw1 == AnSccw2 and AnSccw1 == "B") :
AnSccw2 = "C"
a_ccw_r1 = random.choice(RandomList)
a_ccw_r2 = random.choice(RandomList)
if (AnSccw1 == "C" or AnSccw2 == "C") :
if (AnSccw1 == AnSccw2 and AnSccw1 == "C") :
AnSccw2 = "B"
a_ccw_r1 = random.choice(RandomList)
a_ccw_r2 = random.choice(RandomList)
if (current=="B") :
action = random.choice(Action)
if (action=="Clockwise") :
BnScw1 = random.choice(NSB)
BnScw2 = random.choice(NSB)
if (BnScw1 == "A" or BnScw2 == "A") :
if (BnScw1 == BnScw2 and BnScw1 == "A") :
BnScw2 = "C"
b_cw_b_r1 = random.choice(RandomList)
b_cw_b_r2 = random.choice(RandomList)
if (BnScw1 == "C" or BnScw2 == "C") :
if (BnScw1 == BnScw2 and BnScw1 == "C") :
BnScw2 = "B"
b_cw_r1 = random.choice(RandomList)
b_cw_r2 = random.choice(RandomList)
if (action=="Counterlockwise") :
BnSccw1 = random.choice(NSB)
BnSccw2 = random.choice(NSB)
if (BnSccw1 == "A" or BnSccw2 == "A") :
if (BnSccw1 == BnSccw2 and BnSccw1 == "A") :
BnSccw2 = "C"
b_ccw_r1 = random.choice(RandomList)
b_ccw_r2 = random.choice(RandomList)
if (BnSccw1 == "C" or BnSccw2 == "C") :
if (BnSccw1 == BnSccw2 and BnSccw1 == "C") :
BnSccw2 = "B"
b_ccw_r1 = random.choice(RandomList)
b_ccw_r2 = random.choice(RandomList)
if (current=="C") :
action = random.choice(Action)
if (action=="Clockwise") :
CnScw1 = random.choice(NSC)
CnScw2 = random.choice(NSC)
if (CnScw1 == "A" or CnScw2 == "A") :
if (CnScw1 == CnScw2 and CnScw1 == "A") :
CnScw2 = "B"
c_cw_r1 = random.choice(RandomList)
c_cw_r2 = random.choice(RandomList)
if (CnScw1 == "B" or CnScw2 == "B") :
if (CnScw1 == CnScw2 and CnScw1 == "B") :
CnScw2 = "A"
c_cw_r1 = random.choice(RandomList)
c_cw_r2 = random.choice(RandomList)
if (action=="Counterclockwise") :
CnSccw1 = random.choice(NSC)
CnSccw2 = random.choice(NSC)
if (CnSccw1 == "A" or CnSccw2 == "A") :
if (CnSccw1 == CnSccw2 and CnSccw1 == "A") :
CnSccw2 = "B"
c_ccw_r1 = random.choice(RandomList)
c_ccw_r2 = random.choice(RandomList)
if (CnSccw1 == "B" or CnSccw2 == "B") :
if (CnSccw1 == CnSccw2 and CnSccw1 == "B") :
CnSccw2 = "A"
c_ccw_r1 = random.choice(RandomList)
c_ccw_r2 = random.choice(RandomList)
Как видите, я создал 3 списка NSA, NSB и NSC. Я использую эти 3 списка для случайного выбора значения из них, но получаю элемент, которого нет даже в списке. Так, например, посмотрите на следующую часть кода
NSA = ['B', 'C']
AnScw1 = "B"
AnScw2 = "C"
AnSccw1 = "B"
AnSccw2 = "C"
#calling then later like this
AnScw1 = random.choice(NSA)
AnScw2 = random.choice(NSA)
AnSccw1 = random.choice(NSA)
AnSccw2 = random.choice(NSA)
Я должен получить либо B, либо C от AnScw1, AnScw2, AnSccw1, AnSccw2, но вместо этого я также получаю A, который не входит в список.
Желаемый результат должен выглядеть следующим образом:
===========================================================================
|| Current State || || Action Taken || || Next State || Reward ||
===========================================================================
|| A || || Clockwise || || C || -2.0 ||
|| C || || Counterclockwise || || A || 4.0 ||
|| B || || Counterclockwise || || C || 4.0 ||
|| A || || Counterclockwise || || C || 4.0 ||
Желаемый выход