Hard Constraint
unknown
python
2 years ago
3.5 kB
11
Indexable
################# Decode function ##################
def decode_schedate(chromosome):
pass
def convert_schedate_to_string(sche_date):
pass
def decode_number_of_battery(chromosome):
pass
def manday_chromosome(chromosome): # fitness function
MANDAY = dict()
HC_score = 0
# SC_score = 0
# violate_child = dict()
for child in chromosome:
# print(chromosome)
# take bit and convert to component
component = child.split(
'-') # convert: H13807098-01/12/0001-09/03/0002-10110000110 to ['H13807098', '01/12/0001', '09/03/0002', '10110000110']
# take component
supply_id = component[0] # id
start_day = component[1] # start date
end_day = component[2] # end date
schedate_y_m_d_routine_type_numbat = component[3] # contain |10001|0100|01| 1 000 1010 ==|year|month|date evening, type, number of battery
HC_SCORE = 0
BATDAY = dict()
###Decode Setup###
battery_type = access_row_by_wonum(supply_id)['battery_type']
d_estdur = access_row_by_wonum(supply_id)['d_estdur']
device = access_row_by_wonum(supply_id)['device']
sched_date = decode_schedate(schedate_y_m_d_routine_type_numbat)
date = convert_schedate_to_string(sched_date)
#######################Hard Constraint 1#######################
#Resource set R is Catesian product of B,D and T
#Năng lưg cung cấp tại 1 thời điểm => định nghĩa năng lượng cung cấp
BATDAY[(battery_type, date)] = BATDAY.get((battery_type, date), 0) + d_estdur #Tao dict voi key la tuple ( type va date ), + khoi tao = 0 va cong them d_estdur
for (battery_type,date), demand in BATDAY.items(): # Start constraint
total_capacity = get_resource_quang(battery_type,date,device) #Theo paper là có device,
if total_capacity == -1: # gen date with date not in resouce
HC_score += 1
elif total_capacity < demand:
HC_score += 1
#######################Hard Constraint 1#######################
#######################Hard Constraint 2#######################
#1 Device => 1 Pin
device_battery_mapping = dict()
number_of_battery = decode_number_of_battery(schedate_y_m_d_routine_type_numbat)
if device in device_battery_mapping:
if device_battery_mapping[device] != battery_type
HC_SCORE += 1
else:
device_battery_mapping[device] = battery_type
#######################Hard Constraint 2#######################
#######################Hard Constraint 3#######################
#1 Pin khong cung cap 2 thiet bi
battery_device_mapping = dict()
if battery_type in battery_device_mapping:
if battery_device_mapping[battery_type] != device:
HC_score += 1
else:
battery_device_mapping[battery_type] = device
#######################Hard Constraint 3#######################
def cal_pop_fitness(pop):
fitness = []
for chromosome in pop:
HC = manday_chromosome(chromosome)
fitness.append(1 / (10 * HC + 1))
fitness = np.array(fitness)
return fitness
def get_resource_quang(battery_type, date):
resource_data = load_data(PATHS["resource"])
date_unique = np.unique(resource_data.date.to_list()).astype(list)
if date not in date_unique:
return -1
total_capacity = resource_data[
(resource_data["battery_type"] == battery_type)
& (resource_data["date"] == date)
]["capacity_ah"].sum() #tong capacity
return total_capacityEditor is loading...
Leave a Comment