| import init
|
| import config
|
| import random
|
| import math
|
|
|
| action = [0] * config.ACTION_DIMENSION
|
| big_state = [0] * 19
|
|
|
|
|
| def single_step(action_combo):
|
|
|
| scoreValue = 0
|
|
|
| action_value = action_combo[0]
|
|
|
| big_state = action_combo[1]
|
|
|
|
|
|
|
|
|
| for a in range(config.ACTION_DIMENSION):
|
| action[a] = action_value % 3
|
| if action[a] > 1:
|
| action[a] = -1
|
| action_value = action_value // 3
|
|
|
|
|
|
|
| for i in range(3, 6):
|
| if action[i] < 0:
|
| action[i] = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| score_value = 0
|
| score_card = [0] * init.BIG_STATE_DIMENSION
|
|
|
| local_step = [0] * config.ACTION_DIMENSION
|
| annex = [0] * (config.ACTION_DIMENSION - 13 - 1)
|
| over = False
|
|
|
|
|
|
|
|
|
| def new_volume(big_state):
|
|
|
| volume = big_state[18] + big_state[15] - big_state[17]
|
| if volume < init.VOLUME_MINIMUM_ML:
|
| volume = init.VOLUME_MINIMUM_ML
|
| return volume
|
|
|
| def new_hematocrit(bigState, preStepVolumemL):
|
|
|
| hemoglobin_milliliter = preStepVolumemL * bigState[12] / 100
|
| hematocrit = hemoglobin_milliliter * 100 / bigState[18]
|
|
|
| hematocrit = hematocrit - init.HEMATOCRIT_CHANGE_PER_HOUR * init.HOURS_PER_STEP
|
| if hematocrit < 0:
|
| hematocrit = 0
|
| return hematocrit
|
|
|
| def glucose_millimole_consumed(bigState):
|
| glucose_mmole_consumed = (
|
| init.GLUCOSE_CONSUMPTION_MMOLE_PER_GRAM_HOUR
|
| * init.GRAFT_GRAMS
|
| * init.HOURS_PER_STEP
|
| * pow(2, (bigState[0] - 37) /10)
|
| )
|
| return glucose_mmole_consumed
|
|
|
| def new_glucose(bigState, localStep, preStepVolumemL):
|
| glucose_millimole = 0
|
|
|
| try:
|
| glucose_millimole = (bigState[9] * preStepVolumemL / 1000) + localStep[3]
|
| except: glucose_millimole = .001
|
|
|
| glucose_millimole = glucose_millimole - glucose_millimole_consumed(bigState)
|
| gluc_mM = glucose_millimole / (bigState[18] / 1000 )
|
| return gluc_mM
|
|
|
| def new_insulin(bigState, localStep):
|
|
|
| insulinmIU = bigState[10] + localStep[4] - glucose_millimole_consumed(bigState) * init.INSULIN_mUNIT_PER_mMOLE_GLUCOSE
|
| return insulinmIU
|
|
|
| def new_lactate(bigState, preStepVolumemL):
|
| lactate_millimole = (bigState[11] * preStepVolumemL / 1000) + glucose_millimole_consumed(bigState) * init.LACTATES_PER_GLUCOSE
|
| return lactate_millimole / (bigState[18] / 1000 )
|
|
|
| def new_VR(bigState,vasod):
|
| specificVR = (.0107 - .0002 * big_state[0]) * init.VR_ORGAN_FACTOR
|
|
|
|
|
| VR = specificVR * init.GRAFT_GRAMS * (1 - .1 * vasod) * random.uniform(1, init.VR_STOCHASTIC_FACTOR)
|
| return VR
|
|
|
| def new_flow(bigState):
|
|
|
| try:
|
| return bigState[1]/big_state[3]
|
| except:
|
| return 99
|
|
|
| def new_bicarb(bigState, localStep, preStepVolumemL):
|
|
|
| flow_weighted_mM = (init.BICARB_IN_KREBS_mM * bigState[15] + bigState[13] * bigState[2]) / (bigState[15] + bigState[2])
|
| bicarb_millimole = (flow_weighted_mM * preStepVolumemL / 1000) + localStep[5]
|
| bicarb_millimolar = bicarb_millimole / (bigState[18] / 1000 )
|
| return bicarb_millimolar
|
|
|
| def new_pH(bigState):
|
|
|
| localpH = init.H_H_PK + math.log((bigState[13]/(init.CO2_SOLUBILITY_mM_PER_MMHG * bigState[8])),10)
|
| return localpH
|
|
|
| def new_svO2(bigState):
|
|
|
|
|
| saO2 = (pow((.13534 * bigState[5]),2.62)) / ((pow((.13534 * bigState[5]),2.62)) + 27.4)
|
| Hgb_g_per_dL = 0.34 * bigState[12]
|
| CaO2_mL_per_dL = (saO2 * Hgb_g_per_dL * 1.36) + (.0031 * bigState[5])
|
| CaO2_mL_per_L = CaO2_mL_per_dL * 10
|
| CaO2_mM = CaO2_mL_per_L / init.MOLAR_VOLUME_STP_GAS_L
|
| O2_rate_in_millimole_per_min = CaO2_mM * bigState[2] / 1000
|
| O2_millimole_consumed_per_minute = (glucose_millimole_consumed(bigState) / init.MINUTES_PER_STEP) * (init.AEROBIC_FRACTION * init.OXYGENS_PER_GLUCOSE)
|
| O2_rate_out_millimole_per_min = O2_rate_in_millimole_per_min - O2_millimole_consumed_per_minute
|
| CvO2_mM = (O2_rate_out_millimole_per_min / bigState[2]) * 1000
|
| CvO2_mL_per_dL = CvO2_mM * init.MOLAR_VOLUME_STP_GAS_L / 10
|
| v_Hgb_g_per_dL = 0.34 * bigState[12]
|
| svO2 = (CvO2_mL_per_dL / ((v_Hgb_g_per_dL * 1.34)+ (.0031 / 2)))
|
| if svO2 > 1:
|
| svO2 = 1
|
| return svO2, CvO2_mL_per_dL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| pre_step_volume_mL = big_state[18]
|
|
|
|
|
| for x in range(0, config.ACTION_DIMENSION):
|
| local_step[x] = action[x] * config.action_step[x]
|
|
|
|
|
|
|
| temperature_C = big_state[0] + local_step[0]
|
| if temperature_C > config.action_limit_max[0]:
|
| temperature_C = config.action_limit_max[0]
|
| if temperature_C < config.action_limit_min[0]:
|
| temperature_C = config.action_limit_min[0]
|
|
|
| pressure_mmHg = big_state[1] + local_step[1]
|
| if pressure_mmHg > config.action_limit_max[1]:
|
| pressure_mmHg = config.action_limit_max[1]
|
| if pressure_mmHg < config.action_limit_min[1]:
|
| pressure_mmHg = config.action_limit_min[1]
|
|
|
| fiO2 = big_state[14] + local_step[2]
|
| if fiO2 > config.action_limit_max[2]:
|
| fiO2 = config.action_limit_max[2]
|
| if fiO2 < config.action_limit_min[2]:
|
| fiO2 = config.action_limit_min[2]
|
|
|
| dialysis_in_mL_per_min = big_state[15] + local_step[7]
|
| if dialysis_in_mL_per_min > config.action_limit_max[7]:
|
| dialysis_in_mL_per_min = config.action_limit_max[7]
|
| if dialysis_in_mL_per_min < config.action_limit_min[7]:
|
| dialysis_in_mL_per_min = config.action_limit_min[7]
|
|
|
| dialysis_out_mL_per_min = big_state[17] + local_step[8]
|
| if dialysis_out_mL_per_min > config.action_limit_max[8]:
|
| dialysis_out_mL_per_min = config.action_limit_max[8]
|
| if dialysis_out_mL_per_min < config.action_limit_min[8]:
|
| dialysis_out_mL_per_min = config.action_limit_min[8]
|
|
|
|
|
| pre_step_volume_mL = big_state[18]
|
|
|
|
|
| big_state[0] = temperature_C
|
| big_state[1] = pressure_mmHg
|
| big_state[14] = fiO2
|
| big_state[15] = dialysis_in_mL_per_min
|
| big_state[17] = dialysis_out_mL_per_min
|
|
|
|
|
|
|
| big_state[18] = new_volume(big_state)
|
|
|
|
|
| big_state[9] = new_glucose(big_state, local_step, pre_step_volume_mL)
|
| glucose_mM = big_state[9]
|
|
|
|
|
| big_state[11] = new_lactate(big_state, pre_step_volume_mL)
|
| lactate_mM = big_state[11]
|
|
|
|
|
| big_state[10] = new_insulin(big_state, local_step)
|
| insulin_mU = big_state[10]
|
|
|
|
|
| vasodilator = local_step[6]
|
|
|
|
|
| big_state[12] = new_hematocrit(big_state, pre_step_volume_mL)
|
| hematocrit = big_state[12]
|
|
|
|
|
| big_state[16] = big_state[16] + init.HOURS_PER_STEP
|
| hours = big_state[16]
|
|
|
|
|
| big_state[5] = fiO2 * init.ATMOSPHERIC_PRESSURE_MMHG
|
| pO2_mmHg = big_state[5]
|
|
|
|
|
| big_state[8] = pO2_mmHg * init.CARBOGEN_CO2_TO_O2_FRACTION
|
| pCO2_mmHg = big_state[8]
|
|
|
|
|
| big_state[3] = new_VR(big_state,vasodilator)
|
| VR = big_state[3]
|
|
|
|
|
| big_state[2] = new_flow(big_state)
|
| flow_mL_per_min = big_state[2]
|
|
|
|
|
| big_state[13] = new_bicarb(big_state, local_step, pre_step_volume_mL)
|
| bicarb_mM = big_state[13]
|
|
|
|
|
| big_state[4] = new_pH(big_state)
|
| pH = big_state[4]
|
|
|
|
|
| big_state[7], cvO2_mL_per_dL = new_svO2(big_state)
|
| svO2 = big_state[7]
|
|
|
|
|
| big_state[6] = (cvO2_mL_per_dL - (svO2 * hematocrit * 0.34 * 1.36)) / .0031
|
| if big_state[6] < 0:
|
| big_state[6] = 0
|
| pvO2_mmHg = big_state[6]
|
|
|
|
|
| if pvO2_mmHg > 100:
|
| svO2 = 1
|
| elif pvO2_mmHg > 60:
|
| svO2 = .9 + .25 * (pvO2_mmHg - 60)/ 100
|
| elif pvO2_mmHg > 40:
|
| svO2 = .8 + .5 * (pvO2_mmHg - 40) / 100
|
| elif pvO2_mmHg > 0:
|
| svO2 = pvO2_mmHg * 2 / 100
|
| else:
|
| svO2 = 0
|
| if svO2 < 0:
|
| svO2 = 0
|
| big_state[7] = svO2
|
|
|
|
|
|
|
|
|
|
|
|
|
| scalarSum = 0
|
| newScoreCard = [0] * 6
|
| scoreCard = [0] * 14
|
|
|
| if config.STATE_TYPE == "discrete":
|
| for y in range(0, 14):
|
| if big_state[y] < config.criticalDepletion[y]:
|
| scoreCard[y] = -2
|
| elif big_state[y] < config.depletion[y]:
|
| scoreCard[y] = -1
|
| elif big_state[y] > config.criticalExcess[y]:
|
| scoreCard[y] = 2
|
| elif big_state[y] > config.excess[y]:
|
| scoreCard[y] = 1
|
| else:
|
| scoreCard[y] = 0
|
|
|
|
|
| for z in range (0,6):
|
| newScoreCard[0] = scoreCard[0]
|
| newScoreCard[1] = scoreCard[3]
|
| newScoreCard[2] = scoreCard[4]
|
| newScoreCard[3] = scoreCard[6]
|
| newScoreCard[4] = scoreCard[9]
|
| newScoreCard[5] = scoreCard[10]
|
|
|
| scalarSum = scalarSum + pow(newScoreCard[z],2)
|
|
|
| scoreValue = scalarSum
|
| scoreValue = (127 - 10 * scoreValue)
|
|
|
|
|
| if hours >= 24:
|
| scoreValue = 255
|
|
|
|
|
| bad_score_flag = 0
|
| for x in range (0,6):
|
| if abs(newScoreCard[x]) == 2:
|
| bad_score_flag = 1
|
| if bad_score_flag == 1:
|
| scoreValue = -255
|
|
|
|
|
|
|
|
|
|
|
| actionString = [str(i) for i in action]
|
| scoreString = [str(j) for j in newScoreCard]
|
| stateString = [str(round(k,2)) for k in big_state]
|
|
|
|
|
| if hours == 1:
|
| outstring = "\n" + str(hours) + "\t " + ", ".join(actionString) + "; " + ", ".join(scoreString) + "; " + str(round(scoreValue,2)) + "; " + ", ".join(stateString) + "\n"
|
|
|
| else:
|
| outstring = str(hours) + "\t " + ", ".join(actionString) + "; " + ", ".join(scoreString) + "; " + str(round(scoreValue,2)) + "; " + ", ".join(stateString) + "\n"
|
|
|
| with open (config.filePath + str(config.myuuid) + " " + config.SCORE_TYPE + ".txt",'a') as myfile:
|
| myfile.write (outstring)
|
|
|
| out24List = outstring
|
|
|
| if (hours == 24) and (scoreValue > -255):
|
| with open (config.filePath + str(config.myuuid) + " " + config.SCORE_TYPE + "24s.txt",'a') as my24file:
|
| for x in range (0,25):
|
| my24file.write (out24List[x])
|
|
|
|
|
|
|
|
|
| roundState = [ round(elem, 2) for elem in big_state ]
|
| big_state = roundState
|
|
|
| answer = [big_state, newScoreCard, scoreValue]
|
|
|
| return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|