Weighted Random Selection

This commit is contained in:
Maurane
2026-01-22 15:31:25 +01:00
parent b7c329bbde
commit e4a7d6beac
13 changed files with 365 additions and 204 deletions
+29 -5
View File
@@ -57,7 +57,9 @@ func new_patient():
var file_path: String
#Generating patient_id
var patient_id = Globals.patient_list.pick_random()
var floor_index = -Globals.floor_count -1
print("Floor Index: " + str(floor_index))
var patient_id = weighted_random(patient_list[floor_index])
Globals.patient_id = patient_id
Globals.patient_list.erase(patient_id) #Line to be reenabled when there is more than one patient in the list
Globals.patients_seen.append(patient_id)
@@ -122,15 +124,15 @@ func sort_patients():
var symptom_matched : bool
var symptom_list: Array
for i in range(45,52):
for patient_id in Globals.patient_list:
patient_symptoms.clear()
file_path = "res://Dialogues/Patients/patient_" + str(i) + "_file.cfg"
file_path = "res://Dialogues/Patients/patient_" + patient_id + "_file.cfg"
config.load(file_path)
symptom_list = config.get_value("", "psychiatric_assessment").split("\n", false)
patient_symptoms = symptom_list
symptom_list = config.get_value("", "abnormal_symptoms").split("\n", false)
patient_symptoms.append_array(symptom_list)
print("Patient #" + str(i) + " symptom list: " + str(patient_symptoms))
print("Patient #" + patient_id + " symptom list: " + str(patient_symptoms))
#Goes through the symptoms_arrays from the last to the first,
#If a symptom matches with the patient symptoms,
#Add that patient to the floors from that floor to the last one
@@ -143,9 +145,31 @@ func sort_patients():
break
if symptom_matched:
for k in range(j, symptoms_arrays.size()):
patient_list[k].append(str(i))
patient_list[k].append({"patient_id": patient_id, "weight": j+1})
symptom_matched = false
break
for i in range(len(patient_list)):
print("Floor " + str(i) + " possible patients: " + str(patient_list[i]))
#Weighted random selection from an Array
func weighted_random(array: Array):
#Calculating total weight
var total_weight : int
for item in array:
total_weight += item.weight
print("Total weight: " + str(total_weight))
var r = randf() * total_weight
print("r :" + str(r))
var weight_sum : int
for item in array:
weight_sum += item.weight
if weight_sum >= r:
print("Patient selected: " + item.patient_id + ", Weight: " + str(item.weight))
return item.patient_id
return null