def modified_evolution(self, max_eval=100, mutation_rate=0.01, log_path='./'):
    with open(os.path.join(log_path, 'child.txt'), 'a') as fw_c:
        writer_c = csv.writer(fw_c, lineterminator='\n')
        start_time = time.time()
        eval_flag = np.empty(self.lam)
        active_num = self.pop[0].count_active_node()
        (_, pool_num) = self.pop[0].check_pool()
        if self.init:
            pass
        else:
            while ((active_num < self.pop[0].net_info.min_active_num) or (pool_num > self.max_pool_num)):
                self.pop[0].mutation(1.0)
                active_num = self.pop[0].count_active_node()
                (_, pool_num) = self.pop[0].check_pool()
        if (self.pop[0].eval is None):
            self._evaluation([self.pop[0]], np.array([True]))
        print(self._log_data(net_info_type='active_only', start_time=start_time))
        while (self.num_gen < max_eval):
            self.num_gen += 1
            for i in range(self.lam):
                eval_flag[i] = False
                self.pop[(i + 1)].copy(self.pop[0])
                active_num = self.pop[(i + 1)].count_active_node()
                (_, pool_num) = self.pop[(i + 1)].check_pool()
                while ((not eval_flag[i]) or (active_num < self.pop[(i + 1)].net_info.min_active_num) or (pool_num > self.max_pool_num)):
                    self.pop[(i + 1)].copy(self.pop[0])
                    eval_flag[i] = self.pop[(i + 1)].mutation(mutation_rate)
                    active_num = self.pop[(i + 1)].count_active_node()
                    (_, pool_num) = self.pop[(i + 1)].check_pool()
            (evaluations_acc, evaluations_size) = self._evaluation(self.pop[1:], eval_flag=eval_flag)
            evaluations_argsort = np.argsort((- evaluations_acc))
            print(evaluations_acc, evaluations_argsort)
            best_arg = evaluations_argsort[0]
            f = open(os.path.join(log_path, 'arch_child.txt'), 'a')
            writer_f = csv.writer(f, lineterminator='\n')
            for c in range((1 + self.lam)):
                writer_c.writerow(self._log_data_children(net_info_type='full', start_time=start_time, pop=self.pop[c]))
                writer_f.writerow(self._log_data_children(net_info_type='active_only', start_time=start_time, pop=self.pop[c]))
            f.close()
            print('Comparing children with parent...')
            print(f"Best Child's Accuracy {evaluations_acc[best_arg]}, Parent Accuracy: {self.pop[0].eval}")
            if (evaluations_acc[best_arg] > self.pop[0].eval):
                self.pop[0].copy(self.pop[(best_arg + 1)])
                print('Replacing parent with best child')
            elif (self.bias > 0):
                found = False
                print(f'Parent: Accuracy: {self.pop[0].eval}, Size: {self.pop[0].size}')
                for (i, idx) in enumerate(evaluations_argsort):
                    print(f'Child {(i + 1)}: Accuracy: {evaluations_acc[idx]}, Size: {evaluations_size[idx]}')
                    if ((evaluations_acc[idx] > (self.pop[0].eval - self.bias)) and (evaluations_size[idx] < self.pop[0].size)):
                        print('Replacing parent with child')
                        self.pop[0].copy(self.pop[(idx + 1)])
                        found = True
                        break
                if (not found):
                    self.pop[0].neutral_mutation(mutation_rate)
            else:
                self.pop[0].neutral_mutation(mutation_rate)
            print(self._log_data(net_info_type='active_only', start_time=start_time))
            fw = open(os.path.join(log_path, 'log_cgp.txt'), 'a')
            writer = csv.writer(fw, lineterminator='\n')
            writer.writerow(self._log_data(net_info_type='full', start_time=start_time))
            fa = open(os.path.join(log_path, 'arch.txt'), 'a')
            writer_a = csv.writer(fa, lineterminator='\n')
            writer_a.writerow(self._log_data(net_info_type='active_only', start_time=start_time))
            fw.close()
            fa.close()
def modified_evolution(self, max_eval=100, mutation_rate=0.01, log_path='./'):
    with open(os.path.join(log_path, 'child.txt'), 'a') as fw_c:
        writer_c = csv.writer(fw_c, lineterminator='\n')
        start_time = time.time()
        eval_flag = np.empty(self.lam)
        active_num = self.pop[0].count_active_node()
        (_, pool_num) = self.pop[0].check_pool()
        if self.init:
            pass
        else:
            while ((active_num < self.pop[0].net_info.min_active_num) or (pool_num > self.max_pool_num)):
                self.pop[0].mutation(1.0)
                active_num = self.pop[0].count_active_node()
                (_, pool_num) = self.pop[0].check_pool()
        if (self.pop[0].eval is None):
            self._evaluation([self.pop[0]], np.array([True]))
        print(self._log_data(net_info_type='active_only', start_time=start_time))
        while (self.num_gen < max_eval):
            self.num_gen += 1
            for i in range(self.lam):
                eval_flag[i] = False
                self.pop[(i + 1)].copy(self.pop[0])
                active_num = self.pop[(i + 1)].count_active_node()
                (_, pool_num) = self.pop[(i + 1)].check_pool()
                while ((not eval_flag[i]) or (active_num < self.pop[(i + 1)].net_info.min_active_num) or (pool_num > self.max_pool_num)):
                    self.pop[(i + 1)].copy(self.pop[0])
                    eval_flag[i] = self.pop[(i + 1)].mutation(mutation_rate)
                    active_num = self.pop[(i + 1)].count_active_node()
                    (_, pool_num) = self.pop[(i + 1)].check_pool()
            (evaluations_acc, evaluations_size) = self._evaluation(self.pop[1:], eval_flag=eval_flag)
            evaluations_argsort = np.argsort((- evaluations_acc))
            print(evaluations_acc, evaluations_argsort)
            best_arg = evaluations_argsort[0]
            f = open(os.path.join(log_path, 'arch_child.txt'), 'a')
            writer_f = csv.writer(f, lineterminator='\n')
            for c in range((1 + self.lam)):
                writer_c.writerow(self._log_data_children(net_info_type='full', start_time=start_time, pop=self.pop[c]))
                writer_f.writerow(self._log_data_children(net_info_type='active_only', start_time=start_time, pop=self.pop[c]))
            f.close()
            print('Comparing children with parent...')
            print(f"Best Child's Accuracy {evaluations_acc[best_arg]}, Parent Accuracy: {self.pop[0].eval}")
            if (evaluations_acc[best_arg] > self.pop[0].eval):
                self.pop[0].copy(self.pop[(best_arg + 1)])
                print('Replacing parent with best child')
            elif (self.bias > 0):
                found = False
                print(f'Parent: Accuracy: {self.pop[0].eval}, Size: {self.pop[0].size}')
                for (i, idx) in enumerate(evaluations_argsort):
                    print(f'Child {(i + 1)}: Accuracy: {evaluations_acc[idx]}, Size: {evaluations_size[idx]}')
                    if ((evaluations_acc[idx] > (self.pop[0].eval - self.bias)) and (evaluations_size[idx] < self.pop[0].size)):
                        print('Replacing parent with child')
                        self.pop[0].copy(self.pop[(idx + 1)])
                        found = True
                        break
                if (not found):
                    self.pop[0].neutral_mutation(mutation_rate)
            else:
                self.pop[0].neutral_mutation(mutation_rate)
            print(self._log_data(net_info_type='active_only', start_time=start_time))
            fw = open(os.path.join(log_path, 'log_cgp.txt'), 'a')
            writer = csv.writer(fw, lineterminator='\n')
            writer.writerow(self._log_data(net_info_type='full', start_time=start_time))
            fa = open(os.path.join(log_path, 'arch.txt'), 'a')
            writer_a = csv.writer(fa, lineterminator='\n')
            writer_a.writerow(self._log_data(net_info_type='active_only', start_time=start_time))
            fw.close()
            fa.close()