├── .gitignore ├── Exam ├── AI基础期末考试-2023春v2 - 有答案.docx └── 全(有答案)2023人工智能基础期中考试v4.docx ├── Homework ├── 01-Math │ ├── 01-Math.md │ ├── 01-Math.pdf │ └── HW01.pdf ├── 02-MNIST-Recognition │ ├── .gitignore │ ├── 02-MNIST-Recognition.assets │ │ ├── cnn.png │ │ ├── loss_acc_cnn.png │ │ ├── loss_acc_mlp.png │ │ ├── mlp_adam_dropout.png │ │ ├── np_mnist_mlp.png │ │ └── np_mnist_mlp_monentum.png │ ├── 02-MNIST-Recognition.md │ ├── 02-MNIST-Recognition.pdf │ ├── handout │ │ ├── bp_np.py │ │ ├── examples.py │ │ ├── handout.md │ │ ├── mnist_mlp_template.py │ │ └── np_mnist_template.py │ ├── mnist_cnn.py │ ├── mnist_mlp.py │ ├── np_mnist_mlp.py │ └── np_mnist_mlp_monentum.py ├── 03-CIFAR-Recognition │ ├── 03-CIFAR-Recognition.assets │ │ ├── accuracy.png │ │ └── loss.png │ ├── 03-CIFAR-Recognition.md │ ├── 03-CIFAR-Recognition.pdf │ ├── cifar10_cnn_torch.py │ ├── elephant.jpg │ ├── handout │ │ ├── cifar10_cnn_torch_template.py │ │ ├── examples.py │ │ ├── handout.md │ │ └── imagenet_class_index.json │ ├── imagenet_class_index.json │ └── runs │ │ └── VGG │ │ ├── Accuracy_Test │ │ └── events.out.tfevents.1711864248.Aurora.7423.4 │ │ ├── Accuracy_Train │ │ └── events.out.tfevents.1711864247.Aurora.7423.2 │ │ ├── Loss_Test │ │ └── events.out.tfevents.1711864248.Aurora.7423.3 │ │ ├── Loss_Train │ │ └── events.out.tfevents.1711864247.Aurora.7423.1 │ │ └── events.out.tfevents.1711864242.Aurora.7423.0 ├── 04-IMDB-Emotion-Recognition │ ├── 04-IMDB-Emotion-Recognition.assets │ │ ├── GRU.svg │ │ ├── LSTM.svg │ │ ├── RNN.svg │ │ ├── vs_Accuracy.png │ │ └── vs_Loss.png │ ├── 04-IMDB-Emotion-Recognition.md │ ├── 04-IMDB-Emotion-Recognition.pdf │ ├── handout │ │ ├── README.md │ │ ├── example_imdb_lstm_torch.py │ │ ├── lstm_manual_template.py │ │ └── utils.py │ ├── imdb_arch_comparison_torch.py │ └── lstm_manual.py ├── 05-Global-Search │ ├── c++ │ │ ├── algorithm │ │ │ ├── breadth_first_search.hpp │ │ │ ├── depth_first_search.hpp │ │ │ ├── heuristic_search.hpp │ │ │ └── uniform_cost_search.hpp │ │ ├── interface │ │ │ └── state.hpp │ │ ├── problem │ │ │ ├── directed_graph.hpp │ │ │ └── queens.hpp │ │ ├── queens_bfs_dfs.cpp │ │ ├── short_path_ucs.cpp │ │ └── utils │ │ │ └── show_path.hpp │ ├── handout │ │ ├── c++ │ │ │ ├── algorithm │ │ │ │ ├── breadth_first_search.hpp │ │ │ │ ├── depth_first_search.hpp │ │ │ │ ├── heuristic_search.hpp │ │ │ │ └── uniform_cost_search.hpp │ │ │ ├── interface │ │ │ │ └── state.hpp │ │ │ ├── problem │ │ │ │ ├── directed_graph.hpp │ │ │ │ └── queens.hpp │ │ │ ├── queens_bfs_dfs.cpp │ │ │ ├── short_path_ucs.cpp │ │ │ └── utils │ │ │ │ └── show_path.hpp │ │ ├── python │ │ │ ├── algorithm │ │ │ │ ├── __init__.py │ │ │ │ ├── breadth_first_search.py │ │ │ │ ├── depth_first_search.py │ │ │ │ ├── heuristic_search.py │ │ │ │ └── uniform_cost_search.py │ │ │ ├── interface │ │ │ │ ├── __init__.py │ │ │ │ └── state.py │ │ │ ├── problem │ │ │ │ ├── __init__.py │ │ │ │ ├── directed_graph.py │ │ │ │ └── queens.py │ │ │ ├── queens_bfs_dfs.py │ │ │ ├── short_path_ucs.py │ │ │ └── utils │ │ │ │ ├── __init__.py │ │ │ │ └── show_path.py │ │ ├── 作业要求说明.txt │ │ └── 实验报告1_学号_姓名.docx │ ├── python │ │ ├── algorithm │ │ │ ├── __init__.py │ │ │ ├── breadth_first_search.py │ │ │ ├── depth_first_search.py │ │ │ ├── heuristic_search.py │ │ │ └── uniform_cost_search.py │ │ ├── interface │ │ │ ├── __init__.py │ │ │ └── state.py │ │ ├── problem │ │ │ ├── __init__.py │ │ │ ├── directed_graph.py │ │ │ └── queens.py │ │ ├── queens_bfs_dfs.py │ │ ├── short_path_ucs.py │ │ └── utils │ │ │ ├── __init__.py │ │ │ └── show_path.py │ ├── 全局搜索实验报告_2110306206_Arthals.docx │ └── 全局搜索实验报告_2110306206_Arthals.pdf ├── 06-Local-Search │ ├── c++ │ │ ├── algorithm │ │ │ ├── conflicts_minimize.hpp │ │ │ ├── genetic.hpp │ │ │ ├── hill_climb.hpp │ │ │ └── simulated_anneal.hpp │ │ ├── interface │ │ │ ├── constraint_satisfaction.hpp │ │ │ ├── population.hpp │ │ │ ├── state.hpp │ │ │ └── state_local.hpp │ │ ├── problem │ │ │ ├── queens.hpp │ │ │ ├── queens_constraint.hpp │ │ │ ├── queens_move.hpp │ │ │ └── queens_swap.hpp │ │ ├── queens_conflict_minimize │ │ ├── queens_conflict_minimize.cpp │ │ ├── queens_genetic │ │ ├── queens_genetic.cpp │ │ ├── queens_hill_climb │ │ ├── queens_hill_climb.cpp │ │ ├── queens_simulated_anneal │ │ ├── queens_simulated_anneal.cpp │ │ ├── start.sh │ │ └── utils │ │ │ ├── random_variables.hpp │ │ │ └── selection.hpp │ ├── handout │ │ ├── c++ │ │ │ ├── algorithm │ │ │ │ ├── conflicts_minimize.hpp │ │ │ │ ├── genetic.hpp │ │ │ │ ├── hill_climb.hpp │ │ │ │ └── simulated_anneal.hpp │ │ │ ├── interface │ │ │ │ ├── constraint_satisfaction.hpp │ │ │ │ ├── population.hpp │ │ │ │ ├── state.hpp │ │ │ │ └── state_local.hpp │ │ │ ├── problem │ │ │ │ ├── queens.hpp │ │ │ │ ├── queens_constraint.hpp │ │ │ │ ├── queens_move.hpp │ │ │ │ └── queens_swap.hpp │ │ │ ├── queens_conflict_minimize.cpp │ │ │ ├── queens_genetic.cpp │ │ │ ├── queens_hill_climb.cpp │ │ │ ├── queens_simulated_anneal.cpp │ │ │ └── utils │ │ │ │ ├── random_variables.hpp │ │ │ │ └── selection.hpp │ │ ├── python │ │ │ ├── algorithm │ │ │ │ ├── __init__.py │ │ │ │ ├── conflicts_minimize.py │ │ │ │ ├── genetic.py │ │ │ │ ├── hill_climb.py │ │ │ │ └── simulated_anneal.py │ │ │ ├── interface │ │ │ │ ├── __init__.py │ │ │ │ ├── constraint_satisfaction.py │ │ │ │ ├── population.py │ │ │ │ ├── state.py │ │ │ │ └── state_local.py │ │ │ ├── problem │ │ │ │ ├── __init__.py │ │ │ │ ├── queens.py │ │ │ │ ├── queens_constraint.py │ │ │ │ ├── queens_move.py │ │ │ │ └── queens_swap.py │ │ │ ├── queens_conflict_minimize.py │ │ │ ├── queens_genetic.py │ │ │ ├── queens_hill_climb.py │ │ │ ├── queens_simulated_anneal.py │ │ │ └── utils │ │ │ │ ├── __init__.py │ │ │ │ ├── random_variables.py │ │ │ │ └── selection.py │ │ ├── 作业要求说明.txt │ │ └── 实验报告2_学号_姓名.docx │ ├── python │ │ ├── algorithm │ │ │ ├── __init__.py │ │ │ ├── conflicts_minimize.py │ │ │ ├── genetic.py │ │ │ ├── hill_climb.py │ │ │ └── simulated_anneal.py │ │ ├── interface │ │ │ ├── __init__.py │ │ │ ├── constraint_satisfaction.py │ │ │ ├── population.py │ │ │ ├── state.py │ │ │ └── state_local.py │ │ ├── problem │ │ │ ├── __init__.py │ │ │ ├── queens.py │ │ │ ├── queens_constraint.py │ │ │ ├── queens_move.py │ │ │ └── queens_swap.py │ │ ├── queens_conflict_minimize.py │ │ ├── queens_genetic.py │ │ ├── queens_hill_climb.py │ │ ├── queens_simulated_anneal.py │ │ └── utils │ │ │ ├── __init__.py │ │ │ ├── random_variables.py │ │ │ └── selection.py │ ├── 局部搜索实验报告_2110306206_Arthals.docx │ └── 局部搜索实验报告_2110306206_Arthals.pdf ├── 07-Adversial-Search │ ├── c++ │ │ ├── algorithm │ │ │ ├── alpha_beta_search.hpp │ │ │ ├── general_game_search.hpp │ │ │ └── monte_carlo_tree_search.hpp │ │ ├── hex_main.cpp │ │ ├── hex_mcts_test.cpp │ │ ├── interface │ │ │ ├── game_state.hpp │ │ │ └── state.hpp │ │ ├── problem │ │ │ ├── eight_puzzle.hpp │ │ │ ├── hex.hpp │ │ │ └── tictactoe.hpp │ │ ├── tictactoe_search.cpp │ │ └── utils │ │ │ ├── random_variables.hpp │ │ │ ├── search_tree.hpp │ │ │ ├── selection.hpp │ │ │ ├── show_path.hpp │ │ │ └── union_find_set.hpp │ ├── handout │ │ ├── c++ │ │ │ ├── algorithm │ │ │ │ ├── alpha_beta_search.hpp │ │ │ │ ├── general_game_search.hpp │ │ │ │ └── monte_carlo_tree_search.hpp │ │ │ ├── hex_main.cpp │ │ │ ├── hex_mcts_test.cpp │ │ │ ├── interface │ │ │ │ ├── game_state.hpp │ │ │ │ └── state.hpp │ │ │ ├── problem │ │ │ │ ├── eight_puzzle.hpp │ │ │ │ ├── hex.hpp │ │ │ │ └── tictactoe.hpp │ │ │ ├── tictactoe_search.cpp │ │ │ └── utils │ │ │ │ ├── random_variables.hpp │ │ │ │ ├── search_tree.hpp │ │ │ │ ├── selection.hpp │ │ │ │ ├── show_path.hpp │ │ │ │ └── union_find_set.hpp │ │ ├── python │ │ │ ├── __main__.py │ │ │ ├── algorithm │ │ │ │ ├── __init__.py │ │ │ │ ├── alpha_beta_search.py │ │ │ │ ├── general_game_search.py │ │ │ │ └── monte_carlo_tree_search.py │ │ │ ├── hex_mcts_test.py │ │ │ ├── interface │ │ │ │ ├── __init__.py │ │ │ │ ├── game_state.py │ │ │ │ └── state.py │ │ │ ├── problem │ │ │ │ ├── __init__.py │ │ │ │ ├── hex.py │ │ │ │ └── tictactoe.py │ │ │ ├── tictactoe_search.py │ │ │ └── utils │ │ │ │ ├── __init__.py │ │ │ │ ├── random_variables.py │ │ │ │ ├── search_tree.py │ │ │ │ ├── selection.py │ │ │ │ ├── show_path.py │ │ │ │ └── union_find_set.py │ │ ├── 作业要求说明.txt │ │ └── 实验报告3_学号_姓名.docx │ ├── python │ │ ├── __main__.py │ │ ├── algorithm │ │ │ ├── __init__.py │ │ │ ├── alpha_beta_search.py │ │ │ ├── general_game_search.py │ │ │ ├── monte_carlo_tree_search.py │ │ │ └── test.py │ │ ├── hex_mcts_test.py │ │ ├── interface │ │ │ ├── __init__.py │ │ │ ├── game_state.py │ │ │ └── state.py │ │ ├── problem │ │ │ ├── __init__.py │ │ │ ├── hex.py │ │ │ └── tictactoe.py │ │ ├── tictactoe_search.py │ │ └── utils │ │ │ ├── __init__.py │ │ │ ├── random_variables.py │ │ │ ├── search_tree.py │ │ │ ├── selection.py │ │ │ ├── show_path.py │ │ │ └── union_find_set.py │ ├── 对抗搜索实验报告_2110306206_Arthals.docx │ └── 对抗搜索实验报告_2110306206_Arthals.pdf ├── 08-Reinforcement-Learning │ ├── dynamic_programming.py │ ├── handout │ │ ├── dynamic programming(挖空).py │ │ ├── 动态规划实验报告.docx │ │ └── 第6课-基于动态规划的路径规划-实验指导书(挖空).docx │ ├── 动态规划实验报告_2110306206_Arthals.docx │ ├── 动态规划实验报告_2110306206_Arthals.pdf │ └── 第6课-基于动态规划的路径规划-实验指导书(挖空).docx ├── 09-House-Price-Prediction │ ├── 09-House-Price-Prediction.md │ ├── 09-House-Price-Prediction.pdf │ ├── 2110306206_Arthals.csv │ ├── arthals.ipynb │ └── handout │ │ ├── baselines.ipynb │ │ ├── data │ │ ├── test.csv │ │ ├── test_groundtruth.csv │ │ └── train.csv │ │ ├── 作业说明.txt │ │ └── 数据字典 │ │ ├── Building Classifications 数据字典.pdf │ │ └── 数据字典.pdf └── 10-Mahjong │ ├── 10-Mahjong.assets │ ├── 128_vs_256.png │ ├── Mahjong_CNN.svg │ ├── accuracy.png │ ├── botzone_group.png │ ├── botzone_rank.png │ ├── botzone_rank_score.png │ ├── complex_feature.png │ ├── loss.png │ └── vit_vs_resnet.png │ ├── 10-Mahjong.md │ ├── 10-Mahjong.pdf │ ├── __main__.py │ ├── agent.py │ ├── data │ ├── cannot_enhance_matches.json │ └── data-count-back.json │ ├── dataset.py │ ├── feature.py │ ├── handout │ ├── Mahjong.zip │ ├── README.md │ ├── code │ │ ├── __main__.py │ │ ├── agent.py │ │ ├── dataset.py │ │ ├── feature.py │ │ ├── model.py │ │ ├── preprocess.py │ │ └── supervised.py │ ├── 作业说明.docx │ └── 国标麻将实验报告_学号_姓名.docx │ ├── model.py │ ├── parallel.py │ ├── preprocess.py │ ├── supervised.py │ ├── valid.py │ └── vit.py ├── LICENSE ├── Note ├── 03机器学习介绍与线性模型.assets │ ├── gaussian_distribution.png │ ├── gd.png │ └── nonlinear.png ├── 03机器学习介绍与线性模型.md ├── 04机器学习中的线性回归与分类问题.assets │ ├── dnn.png │ ├── logistic_regression_vs_linear_regression.png │ ├── mlp.png │ ├── softmax.png │ └── xor.png ├── 04机器学习中的线性回归与分类问题.md ├── 05神经网络基础.assets │ ├── L1-vs-L2.png │ ├── backpropagation.png │ ├── bias.png │ ├── dropout.png │ ├── early-stopping.png │ ├── encoder-decoder.png │ ├── gradient_descent.png │ ├── mlp.png │ └── neuron.png ├── 05神经网络基础.md ├── 06卷积神经网络.assets │ ├── deconvolution.png │ ├── depthwise_separable_convolution.png │ ├── dilated_convolution.png │ ├── horizontal_edge.png │ ├── max_pooling.png │ ├── mean_pooling.png │ ├── multi_layer_receptive_field.png │ ├── no_padding.png │ ├── pyramid_pooling.png │ ├── receptive_field.png │ ├── residual_block.png │ ├── stereoscopic_perspective.png │ └── zero_padding.png ├── 06卷积神经网络.md ├── 07卷积神经网络应用.assets │ ├── NMS.png │ ├── YOLO.png │ ├── YOLO_v2.png │ ├── ap.png │ ├── close-set-and-open-set.png │ ├── cpm_arch.png │ ├── depth_estimation.png │ ├── fcn.png │ ├── feature_vector.png │ ├── image-segmentation.png │ ├── image_translation.png │ ├── iou.png │ ├── loss_weighting.png │ ├── mirror_padding.png │ ├── open-pose.png │ ├── open_pose.png │ ├── paf.png │ ├── point-wise.png │ ├── ppn.png │ ├── ppn_arch.png │ ├── rcnn-vs-spp-2.png │ ├── rcnn-vs-spp.png │ ├── roi-align.png │ ├── roi-pooling.png │ ├── rpn-1.png │ ├── rpn-2.png │ ├── segnet.png │ ├── skip-connection.png │ ├── style_transfer.png │ └── super_resolution.png ├── 07卷积神经网络应用.md ├── 08对抗神经网络.assets │ ├── CV_and_CG.png │ ├── CV_and_CG_2.png │ ├── GAN_traning.png │ ├── discriminative_vs_generative.png │ ├── generative_model.png │ ├── mse_vs_adversial_loss.png │ ├── vae_and_gan.png │ └── vanilla_GAN.png ├── 08对抗神经网络.md ├── 09精选生成对抗网络.assets │ ├── BiGAN.png │ ├── CoGAN.png │ ├── CoGAN_arch.png │ ├── cGAN.png │ ├── cGAN_application.png │ ├── cGAN_arch.png │ ├── cycleGAN_arch.png │ ├── cycleGAN_target.png │ ├── find_latent_representation_1.png │ ├── find_latent_representation_2.png │ ├── image_text_pair.png │ ├── mode_collapse.png │ ├── vae_and_gan.png │ └── vae_and_gan_2.png ├── 09精选生成对抗网络.md ├── 10循环神经网络.assets │ ├── 3d_embedding.png │ ├── GRU.svg │ ├── LSTM.svg │ ├── async_many_to_many.png │ ├── async_many_to_many_2.png │ ├── bag-of-words.png │ ├── many_to_one.png │ ├── one-hot.png │ ├── one_to_many.png │ ├── rnn_arch.png │ ├── rnn_arch.svg │ ├── sequential_data.png │ ├── sync_many_to_many.png │ ├── sync_many_to_many_2.png │ └── word-embedding.png ├── 10循环神经网络.md ├── 11注意力机制与Transformer.assets │ ├── human_attention.png │ ├── masked_multihead_attention.png │ ├── multi-head_attention.png │ ├── multi_head_attention.svg │ ├── position_encode.png │ ├── qkv.svg │ ├── self-attention.png │ ├── self_attention_2.png │ └── transformer.svg ├── 11注意力机制与Transformer.md ├── 12用搜索解决问题.assets │ ├── basic_concept.png │ ├── eight_digital.png │ ├── informed_search.png │ └── pathfinding.png ├── 12用搜索解决问题.md ├── 13局部搜索和优化.assets │ ├── genetic_algorithms.png │ ├── genetic_algorithms_2.png │ ├── genetic_pesudo_code.png │ ├── local_optimum.png │ └── solution_space.png ├── 13局部搜索和优化.md ├── 14对抗搜索.assets │ ├── alpha_beta_pruning_pesudo_code.png │ ├── alpha_beta_pruning_pesudo_code_concise.png │ ├── alpha_pruning.png │ ├── best_situation_of_alpha_beta_pruning.png │ ├── beta_pruning.png │ ├── mcts_algorithm.png │ ├── mcts_search_tree.png │ ├── minimax_pesudo_code.png │ ├── minimax_pesudo_code_concise.png │ ├── minimax_search_tree.png │ ├── uct_pesudo_code.png │ ├── uct_pesudo_code_2.png │ └── uct_pesudo_code_3.png ├── 14对抗搜索.md ├── 15强化学习基本思想和问题模型.assets │ ├── epsilon_greedy.png │ ├── epsilon_greedy_algorithm.png │ ├── epsilon_vs_optimstic.png │ ├── gradient_bandit_bar_rt.png │ ├── reinforcement_model.png │ ├── reward_func_1.png │ └── reward_func_2.png ├── 15强化学习基本思想和问题模型.md ├── 16马尔可夫决策过程和动态规划.assets │ ├── fc.png │ ├── fc_2.png │ ├── generalized_policy_iteration.png │ ├── num_triangle.png │ ├── summary.png │ ├── summary_2.png │ └── value_iteration_and_policy_iteration_comparison.png ├── 16马尔可夫决策过程和动态规划.md ├── 17人工智能系统实践.assets │ ├── AI_pipeline.png │ ├── active_learning.png │ └── self_training.png └── 17人工智能系统实践.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.npy 2 | PPT* 3 | **/data 4 | !Homework/09-House-Price-Prediction/handout/data 5 | !Homework/10-Mahjong/data -------------------------------------------------------------------------------- /Exam/AI基础期末考试-2023春v2 - 有答案.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Exam/AI基础期末考试-2023春v2 - 有答案.docx -------------------------------------------------------------------------------- /Exam/全(有答案)2023人工智能基础期中考试v4.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Exam/全(有答案)2023人工智能基础期中考试v4.docx -------------------------------------------------------------------------------- /Homework/01-Math/01-Math.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/01-Math/01-Math.pdf -------------------------------------------------------------------------------- /Homework/01-Math/HW01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/01-Math/HW01.pdf -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/.gitignore: -------------------------------------------------------------------------------- 1 | mnist/ -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/cnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/cnn.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/loss_acc_cnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/loss_acc_cnn.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/loss_acc_mlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/loss_acc_mlp.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/mlp_adam_dropout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/mlp_adam_dropout.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/np_mnist_mlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/np_mnist_mlp.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/np_mnist_mlp_monentum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.assets/np_mnist_mlp_monentum.png -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.md: -------------------------------------------------------------------------------- 1 | # Homework 02 MNIST-Recognition 2 | 3 |
4 | 北京大学 2024 春季人工智能基础第二次课程作业 5 |
6 | 7 | --- 8 | 9 |
10 | Arthals 2110306206 11 |
12 |
zhuozhiyongde@126.com
13 | 2024.03 14 |
15 | 16 | --- 17 | 18 | ## NumPy version 19 | 20 | ### 1. MLP with SGD 21 | 22 | ![np_mnist_mlp](02-MNIST-Recognition.assets/np_mnist_mlp.png) 23 | 24 | epoch: 10, val_loss: 0.1605, val_acc: 0.9543 25 | 26 | ### 2. MLP with SGD and Momentum 27 | 28 | ![np_mnist_mlp_monentum](02-MNIST-Recognition.assets/np_mnist_mlp_monentum.png) 29 | 30 | epoch: 10, val_loss: 0.0923, val_acc: 0.9723 31 | 32 | ## PyTorch version 33 | 34 | ### MLP with Adam and Dropout 35 | 36 | ![mlp_adam_dropout](02-MNIST-Recognition.assets/mlp_adam_dropout.png) 37 | 38 | - Training Epoch:10 39 | - Loss: 0.004142 40 | - Val acc: 97.94% 41 | 42 | ![loss_acc_cnn](02-MNIST-Recognition.assets/loss_acc_mlp.png) 43 | 44 | ### CNN 45 | 46 | ![cnn](02-MNIST-Recognition.assets/cnn.png) 47 | 48 | - Training Epoch:10 49 | - Loss: 0.001387 50 | - Val acc: 99.08% 51 | 52 | ![loss_acc_cnn](02-MNIST-Recognition.assets/loss_acc_cnn.png) 53 | -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/02-MNIST-Recognition.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/02-MNIST-Recognition/02-MNIST-Recognition.pdf -------------------------------------------------------------------------------- /Homework/02-MNIST-Recognition/handout/handout.md: -------------------------------------------------------------------------------- 1 | # Homework 2 MNIST Recognition 2 | 3 | ## 作业 1:用 numpy 实现训练 MLP 网络识别手写数字 MNIST 数据集 4 | 5 | 运行、阅读并理解反向传播算法示例 bp_np.py 6 | 7 | 修改 np_mnist_template.py,更改 loss 函数、网络结构、激活函数,完成训练 MLP 网络识别手写数字 MNIST 数据集。 8 | 9 | 要求:10 个 epoch 后测试集准确率达到 94%以上 10 | 11 | 提交方式:np_mnist.py 代码文件,训练过程中打印的准确率截图 12 | 13 | ## 作业 2:使用 Pytorch 训练 MNIST 数据集的 MLP 模型 14 | 15 | 运行、阅读并理解 mnist_mlp_template.py,修改网络结构和参数,增加隐藏层,观察训练效果 16 | 17 | 使用 Adam 等不同优化器,添加 Dropout 层,观察训练效果 18 | 19 | 要求:10 个 epoch 后测试集准确率达到 97%以上 20 | 21 | 提交方式:mnist_mlp.py 代码文件,训练过程中打印的准确率截图 22 | -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.assets/accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.assets/accuracy.png -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.assets/loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.assets/loss.png -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/03-CIFAR-Recognition.pdf -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/elephant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/elephant.jpg -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/handout/handout.md: -------------------------------------------------------------------------------- 1 | # 作业 3:用 pytorch 实现卷积神经网络,对 cifar10 数据集进行分类 2 | 3 | ## 作业描述 4 | 5 | 本次作业旨在通过 PyTorch 框架实现一个卷积神经网络(CNN),以实现对 CIFAR-10 数据集的图像分类。CIFAR-10 数据集包含 60,000 张 32x32 彩色图像,分为 10 个类别,每个类别有 6,000 张图像。其中 50,000 张图像用于训练,10,000 张用于测试。 6 | 7 | ## 任务要求 8 | 9 | 1. 网络实现:使用 PyTorch 的 nn.Module 基类创建一个自定义的卷积神经网络。网络应至少包含一个卷积层(nn.Conv2d)和一个池化层(如 nn.MaxPool2d)。你可以根据自己的需要添加更多的卷积层、池化层、全连接层(nn.Linear)以及激活函数(如 ReLU)。 10 | 2. 数据加载:使用 PyTorch 的 DataLoader 和 Dataset 相关的 API 来实现 CIFAR-10 数据集的加载。确保数据被正确地归一化并划分为训练集和测试集。 11 | 3. 网络结构与参数调整:修改网络结构和参数,观察不同结构对训练效果的影响。你可以尝试不同的激活函数、卷积核大小、步长、填充等。但请注意,你不能使用任何预训练的模型、接口或代码。 12 | 4. 数据增强:为了提高模型的泛化能力,使用数据增强技术,如随机裁剪、水平翻转、色彩抖动等。这些技术可以在训练过程中为模型提供更多样化的数据样本。 13 | 5. 训练与评估:训练网络至少 100 个 epoch,并在每个 epoch 结束后计算并打印训练集和测试集的准确率。确保你的模型在 100 个 epoch 后测试集的准确率能够达到 80%以上。 14 | 15 | ## 提交要求 16 | 17 | 1. 代码文件:提交一个名为 cifar10_cnn.py 的 Python 代码文件,其中包含你的网络定义、数据加载、训练循环和评估逻辑。 18 | 2. 训练日志:提交一个包含训练过程中每个 epoch 结束时的训练集和测试集准确率的文本文件或截图。这个日志应该清晰地展示模型性能随着训练的进行而提高的过程。 19 | 3. 详细报告:提交一份详细的报告,内容应包括: 20 | - 网络结构的设计及其背后的理由。 21 | - 你在训练过程中所做的任何修改或调整,以及这些修改如何影响模型的性能。 22 | - 数据增强技术的使用及其对模型泛化能力的影响。 23 | - 训练过程中遇到的任何挑战以及你是如何解决的。 24 | - 最终的模型性能评估,包括训练集和测试集的准确率。 25 | -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/runs/VGG/Accuracy_Test/events.out.tfevents.1711864248.Aurora.7423.4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/runs/VGG/Accuracy_Test/events.out.tfevents.1711864248.Aurora.7423.4 -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/runs/VGG/Accuracy_Train/events.out.tfevents.1711864247.Aurora.7423.2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/runs/VGG/Accuracy_Train/events.out.tfevents.1711864247.Aurora.7423.2 -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/runs/VGG/Loss_Test/events.out.tfevents.1711864248.Aurora.7423.3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/runs/VGG/Loss_Test/events.out.tfevents.1711864248.Aurora.7423.3 -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/runs/VGG/Loss_Train/events.out.tfevents.1711864247.Aurora.7423.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/runs/VGG/Loss_Train/events.out.tfevents.1711864247.Aurora.7423.1 -------------------------------------------------------------------------------- /Homework/03-CIFAR-Recognition/runs/VGG/events.out.tfevents.1711864242.Aurora.7423.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/03-CIFAR-Recognition/runs/VGG/events.out.tfevents.1711864242.Aurora.7423.0 -------------------------------------------------------------------------------- /Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.assets/vs_Accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.assets/vs_Accuracy.png -------------------------------------------------------------------------------- /Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.assets/vs_Loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.assets/vs_Loss.png -------------------------------------------------------------------------------- /Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/04-IMDB-Emotion-Recognition/04-IMDB-Emotion-Recognition.pdf -------------------------------------------------------------------------------- /Homework/05-Global-Search/c++/algorithm/uniform_cost_search.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "heuristic_search.hpp" 6 | #include "../interface/state.hpp" 7 | 8 | // 一致代价搜索一个解 9 | template 10 | class UniformCostSearch : protected HeuristicSearch{ 11 | private: 12 | 13 | // 一致代价搜索即为仅使用:到达当前状态的总花费*(-1) 作为状态估值的启发式搜索 14 | // 可以理解为搜到目标就结束的dijkstra算法 15 | static double state_value_estimator(const StateType& state){ 16 | return -state.cumulative_cost(); 17 | } 18 | 19 | public: 20 | 21 | UniformCostSearch(const StateType& state) : HeuristicSearch(state) {} 22 | 23 | void search(){ 24 | HeuristicSearch::search(state_value_estimator); 25 | } 26 | }; -------------------------------------------------------------------------------- /Homework/05-Global-Search/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | using ActionBaseType = ActionType; 24 | 25 | // 当前状态下可选择的动作集 26 | virtual std::vector action_space() const = 0; 27 | 28 | // 转移到当前状态的步骤的花费 29 | virtual double cost() const = 0; 30 | 31 | // 从初始状态出发转移到当前状态的总花费 32 | virtual double cumulative_cost() const = 0; 33 | 34 | // 判断当前状态是否为目标状态 35 | virtual bool success() const = 0; 36 | 37 | // 判断从当前状态是否已经不可能转移到目标状态 38 | virtual bool fail() const = 0; 39 | 40 | // 打印当前状态 41 | virtual void show() const = 0; 42 | 43 | // 从当前状态通过动作生成下一步状态 44 | virtual const StateBase& next(const ActionType&) const = 0; 45 | 46 | // 状态哈希 47 | friend struct std::hash; 48 | 49 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 50 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 51 | return s1.cumulative_cost() == s2.cumulative_cost(); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/c++/queens_bfs_dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "problem/queens.hpp" 5 | 6 | #include "algorithm/depth_first_search.hpp" 7 | #include "algorithm/breadth_first_search.hpp" 8 | 9 | int main(){ 10 | std::ios::sync_with_stdio(false); 11 | 12 | // time_t t0 = time(nullptr); 13 | 14 | // QueensState state(12); 15 | // BreadthFirstSearch bfs(state); 16 | // bfs.search(true, false); 17 | 18 | // //DepthFirstSearch dfs(state); 19 | // //dfs.search(true, false); 20 | 21 | // std::cout << time(nullptr) - t0 << std::endl; 22 | // return 0; 23 | 24 | for (int i = 8; i <= 15; ++i) { 25 | std::cout << "Queens State for " << i << " queens:" << std::endl; 26 | 27 | time_t t0 = time(nullptr); 28 | QueensState state(i); 29 | 30 | BreadthFirstSearch bfs(state); 31 | bfs.search(true, false); 32 | std::cout << "\tBFS time = " << time(nullptr) - t0 << "s" << std::endl; 33 | 34 | t0 = time(nullptr); 35 | DepthFirstSearch dfs(state); 36 | dfs.search(true, false); 37 | std::cout << "\tDFS time = " << time(nullptr) - t0 << "s" << std::endl; 38 | 39 | std::cout << "----" << std::endl; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/c++/short_path_ucs.cpp: -------------------------------------------------------------------------------- 1 | #include "problem/directed_graph.hpp" 2 | 3 | #include "algorithm/heuristic_search.hpp" 4 | #include "algorithm/uniform_cost_search.hpp" 5 | 6 | const char pos_names[][20] = { 7 | "Oradea", // 0 8 | "Zerind", 9 | "Arad", // start:2 10 | "Sibiu", 11 | "Fagaras", 12 | 13 | "Timisoara", // 5 14 | "Rimnicu Vilcea", 15 | "Lugoj", 16 | "Pitesti", 17 | "Mehadia", 18 | 19 | "Drobeta", // 10 20 | "Craiova", 21 | "Neamt", 22 | "Iasi", 23 | "Vaslui", 24 | 25 | "Giurgiu", // 15 26 | "Bucharest", // end:16 27 | "Urziceni", 28 | "Hirsova", 29 | "Eforie" 30 | }; 31 | 32 | // 各点到目标点(编号16:Bucharest的直线距离) 33 | double to_target_dis[] = { 34 | 380, 374, 366, 253, 176, 35 | 329, 193, 244, 100, 241, 36 | 242, 160, 234, 226, 199, 37 | 77, 0, 80, 151, 161 38 | }; 39 | 40 | // 各条边的起点,无向边仅记录一次 41 | int u[] = {0,0,1,2,2, 5,7,9,10,11, 11,3,3,4,6, 8,16,16,12,13, 14,17,18}; 42 | 43 | // 各条边的终点 44 | int v[] = {1,3,2,5,3, 7,9,10,11,6, 8,6,4,16,8, 16,15,17,13,14, 17,18,19}; 45 | 46 | // 各条边的权重 47 | double w[] = {71,151,75,118,140, 111,70,75,120,146, 138,80,99,211,97, 101,90,85,87,92, 142,98,86}; 48 | 49 | double a_star_estimator(const DirectedGraphState& s){ 50 | // 填写A*的状态估值函数 51 | } 52 | 53 | double uniform_cost_estimator(const DirectedGraphState& s){ 54 | return -s.cumulative_cost(); 55 | } 56 | 57 | double greedy_estimator(const DirectedGraphState& s){ 58 | // 填写贪心的状态估值函数 59 | } 60 | 61 | 62 | int main(){ 63 | DirectedGraph graph(20); 64 | 65 | for (int i = 0; i < 23; ++ i){ 66 | graph.add_edge(u[i], v[i], w[i]); 67 | graph.add_edge(v[i], u[i], w[i]); 68 | } 69 | 70 | DirectedGraphState s(graph, 2, 16); 71 | HeuristicSearch hs(s); 72 | 73 | hs.search(uniform_cost_estimator); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Homework/05-Global-Search/c++/utils/show_path.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | void show_reversed_path(const std::unordered_map& last_state_of, const StateType& state){ 9 | StateType s = state; 10 | std::stack path; 11 | while (last_state_of.find(s) != last_state_of.end()){ 12 | path.push(s); 13 | s = last_state_of.at(s); 14 | } 15 | path.push(s); 16 | 17 | std::cout << "" << std::endl; 18 | while (not path.empty()){ 19 | s = path.top(); 20 | path.pop(); 21 | s.show(); 22 | } 23 | std::cout << "" << std::endl; 24 | } 25 | 26 | template 27 | void show_path(const std::unordered_map& next_state_of, const StateType& state){ 28 | 29 | StateType s; 30 | std::cout << "" << std::endl; 31 | for (s = state; next_state_of.find(s) != next_state_of.end(); s = next_state_of.at(s)){ 32 | s.show(); 33 | } 34 | s.show(); 35 | std::cout << "" << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/c++/algorithm/uniform_cost_search.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "heuristic_search.hpp" 6 | #include "../interface/state.hpp" 7 | 8 | // 一致代价搜索一个解 9 | template 10 | class UniformCostSearch : protected HeuristicSearch{ 11 | private: 12 | 13 | // 一致代价搜索即为仅使用:到达当前状态的总花费*(-1) 作为状态估值的启发式搜索 14 | // 可以理解为搜到目标就结束的dijkstra算法 15 | static double state_value_estimator(const StateType& state){ 16 | return -state.cumulative_cost(); 17 | } 18 | 19 | public: 20 | 21 | UniformCostSearch(const StateType& state) : HeuristicSearch(state) {} 22 | 23 | void search(){ 24 | HeuristicSearch::search(state_value_estimator); 25 | } 26 | }; -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | using ActionBaseType = ActionType; 24 | 25 | // 当前状态下可选择的动作集 26 | virtual std::vector action_space() const = 0; 27 | 28 | // 转移到当前状态的步骤的花费 29 | virtual double cost() const = 0; 30 | 31 | // 从初始状态出发转移到当前状态的总花费 32 | virtual double cumulative_cost() const = 0; 33 | 34 | // 判断当前状态是否为目标状态 35 | virtual bool success() const = 0; 36 | 37 | // 判断从当前状态是否已经不可能转移到目标状态 38 | virtual bool fail() const = 0; 39 | 40 | // 打印当前状态 41 | virtual void show() const = 0; 42 | 43 | // 从当前状态通过动作生成下一步状态 44 | virtual const StateBase& next(const ActionType&) const = 0; 45 | 46 | // 状态哈希 47 | friend struct std::hash; 48 | 49 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 50 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 51 | return s1.cumulative_cost() == s2.cumulative_cost(); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/c++/queens_bfs_dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "problem/queens.hpp" 5 | 6 | #include "algorithm/depth_first_search.hpp" 7 | #include "algorithm/breadth_first_search.hpp" 8 | 9 | int main(){ 10 | std::ios::sync_with_stdio(false); 11 | 12 | time_t t0 = time(nullptr); 13 | 14 | QueensState state(11); 15 | BreadthFirstSearch bfs(state); 16 | bfs.search(true, false); 17 | 18 | //DepthFirstSearch dfs(state); 19 | //dfs.search(true, false); 20 | 21 | std::cout << time(nullptr) - t0 << std::endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/c++/short_path_ucs.cpp: -------------------------------------------------------------------------------- 1 | #include "problem/directed_graph.hpp" 2 | 3 | #include "algorithm/heuristic_search.hpp" 4 | #include "algorithm/uniform_cost_search.hpp" 5 | 6 | const char pos_names[][20] = { 7 | "Oradea", // 0 8 | "Zerind", 9 | "Arad", // start:2 10 | "Sibiu", 11 | "Fagaras", 12 | 13 | "Timisoara", // 5 14 | "Rimnicu Vilcea", 15 | "Lugoj", 16 | "Pitesti", 17 | "Mehadia", 18 | 19 | "Drobeta", // 10 20 | "Craiova", 21 | "Neamt", 22 | "Iasi", 23 | "Vaslui", 24 | 25 | "Giurgiu", // 15 26 | "Bucharest", // end:16 27 | "Urziceni", 28 | "Hirsova", 29 | "Eforie" 30 | }; 31 | 32 | // 各点到目标点(编号16:Bucharest的直线距离) 33 | double to_target_dis[] = { 34 | 380, 374, 366, 253, 176, 35 | 329, 193, 244, 100, 241, 36 | 242, 160, 234, 226, 199, 37 | 77, 0, 80, 151, 161 38 | }; 39 | 40 | // 各条边的起点,无向边仅记录一次 41 | int u[] = {0,0,1,2,2, 5,7,9,10,11, 11,3,3,4,6, 8,16,16,12,13, 14,17,18}; 42 | 43 | // 各条边的终点 44 | int v[] = {1,3,2,5,3, 7,9,10,11,6, 8,6,4,16,8, 16,15,17,13,14, 17,18,19}; 45 | 46 | // 各条边的权重 47 | double w[] = {71,151,75,118,140, 111,70,75,120,146, 138,80,99,211,97, 101,90,85,87,92, 142,98,86}; 48 | 49 | double a_star_estimator(const DirectedGraphState& s){ 50 | // 填写A*的状态估值函数 51 | } 52 | 53 | double uniform_cost_estimator(const DirectedGraphState& s){ 54 | return -s.cumulative_cost(); 55 | } 56 | 57 | double greedy_estimator(const DirectedGraphState& s){ 58 | // 填写贪心的状态估值函数 59 | } 60 | 61 | 62 | int main(){ 63 | DirectedGraph graph(20); 64 | 65 | for (int i = 0; i < 23; ++ i){ 66 | graph.add_edge(u[i], v[i], w[i]); 67 | graph.add_edge(v[i], u[i], w[i]); 68 | } 69 | 70 | DirectedGraphState s(graph, 2, 16); 71 | HeuristicSearch hs(s); 72 | 73 | hs.search(uniform_cost_estimator); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/c++/utils/show_path.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | void show_reversed_path(const std::unordered_map& last_state_of, const StateType& state){ 9 | StateType s = state; 10 | std::stack path; 11 | while (last_state_of.find(s) != last_state_of.end()){ 12 | path.push(s); 13 | s = last_state_of.at(s); 14 | } 15 | path.push(s); 16 | 17 | std::cout << "" << std::endl; 18 | while (not path.empty()){ 19 | s = path.top(); 20 | path.pop(); 21 | s.show(); 22 | } 23 | std::cout << "" << std::endl; 24 | } 25 | 26 | template 27 | void show_path(const std::unordered_map& next_state_of, const StateType& state){ 28 | 29 | StateType s; 30 | std::cout << "" << std::endl; 31 | for (s = state; next_state_of.find(s) != next_state_of.end(); s = next_state_of.at(s)){ 32 | s.show(); 33 | } 34 | s.show(); 35 | std::cout << "" << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("..") -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/algorithm/breadth_first_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from queue import Queue 3 | 4 | from interface.state import StateBase 5 | from utils.show_path import show_reversed_path 6 | 7 | # 广度优先搜索全部解 8 | class BreadthFirstSearch: 9 | 10 | def __init__(self, state:StateBase): 11 | assert isinstance(state, StateBase) 12 | self.initial_state = deepcopy(state) 13 | 14 | # tree_search决定是否判断重复状态,require_path决定是否记录路径 15 | def search(self, tree_search:bool=True, require_path:bool=True) -> None: 16 | 17 | # 宽搜状态队列 18 | states_queue = Queue() 19 | 20 | # 记录状态的前一个状态,用于记录路径 21 | last_state_of = dict() 22 | 23 | # 防止重复状态,判断哪些状态已经访问过 24 | explored_states = set() 25 | 26 | states_queue.put(self.initial_state) 27 | explored_states.add(self.initial_state) 28 | 29 | while not states_queue.empty(): 30 | 31 | state = states_queue.get() 32 | 33 | if state.success(): 34 | if require_path: 35 | show_reversed_path(last_state_of, state) 36 | else: 37 | state.show() 38 | continue 39 | 40 | if state.fail(): 41 | continue 42 | 43 | # 考虑所有可能动作,扩展全部子状态 44 | for action in state.action_space(): 45 | 46 | new_state = state.next(action) 47 | 48 | if tree_search: 49 | # TODO:扩展待访问的结点集合——1行 50 | if require_path: 51 | # TODO:在 last_state_of 中记录路径——1行 52 | 53 | elif new_state not in explored_states: 54 | # TODO:扩展待访问的结点集合——2行 55 | if require_path: 56 | # TODO:在 last_state_of 中记录路径——1行 57 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/algorithm/depth_first_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from queue import LifoQueue 3 | 4 | from interface.state import StateBase 5 | from utils.show_path import show_reversed_path 6 | 7 | # 深度优先搜索全部解 8 | class DepthFirstSearch: 9 | 10 | def __init__(self, state:StateBase): 11 | assert isinstance(state, StateBase) 12 | self.initial_state = deepcopy(state) 13 | 14 | # tree_search决定是否判断重复状态,require_path决定是否记录路径 15 | def search(self, tree_search:bool=True, require_path:bool=True) -> None: 16 | 17 | # 记录:(当前状态, 当前状态已经访问的子节点个数) 18 | states_stack = LifoQueue() 19 | 20 | # 记录状态的前一个状态,用于记录路径 21 | last_state_of = dict() 22 | 23 | # 防止重复状态,判断哪些状态已经访问过 24 | explored_states = set() 25 | 26 | states_stack.put((self.initial_state, 0)) 27 | explored_states.add(self.initial_state) 28 | 29 | while not states_stack.empty(): 30 | state, action_id = states_stack.get() 31 | 32 | if state.success(): 33 | if require_path: 34 | show_reversed_path(last_state_of, state) 35 | else: 36 | state.show() 37 | continue 38 | 39 | if state.fail(): 40 | continue 41 | 42 | # 如果当前状态还有动作未尝试过 43 | if action_id < len(state.action_space()): 44 | 45 | # TODO:当前状态待尝试的动作变为下一个动作,等待回溯的时候尝试——1行 46 | 47 | # 尝试当前动作,获得下一步状态 48 | new_state = state.next(state.action_space()[action_id]) 49 | 50 | if tree_search: 51 | # TODO:扩展待访问的结点集合——1行 52 | if require_path: 53 | # TODO:记录路径——1行 54 | elif new_state not in explored_states: 55 | # TODO:扩展待访问的结点集合——2行 56 | if require_path: 57 | # TODO:记录路径——1行 -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/algorithm/heuristic_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from queue import PriorityQueue 3 | from typing import Callable 4 | 5 | from interface.state import StateBase 6 | from utils.show_path import show_reversed_path 7 | 8 | # 根据传入的状态估值函数启发式地搜索一个解 9 | class HeuristicSearch: 10 | 11 | ValueEstimatorType = Callable[[StateBase], float] 12 | 13 | def __init__(self, state:StateBase): 14 | assert isinstance(state, StateBase) 15 | self.initial_state = deepcopy(state) 16 | 17 | # 传入状态估值函数 18 | def search(self, value_of:ValueEstimatorType) -> None: 19 | 20 | # 优先队列中估值高的状态在堆顶,先被访问 21 | states_queue = PriorityQueue() 22 | 23 | # 某个状态的最大估值(在最短路问题中为:最短估计距离*(-1) ) 24 | best_value_of = dict() 25 | 26 | # 记录状态的前一个状态,用于记录路径 27 | last_state_of = dict() 28 | 29 | states_queue.put((0, self.initial_state)) 30 | best_value_of[self.initial_state] = 0 31 | 32 | while not states_queue.empty(): 33 | _, state = states_queue.get() 34 | 35 | if state.success(): 36 | break 37 | 38 | if state.fail(): 39 | continue 40 | 41 | # 从开结点集中估值最高的状态出发尝试所有动作 42 | for action in state.action_space(): 43 | 44 | # TODO:状态转移——1行 45 | 46 | # 如果从当前结点出发到达新结点所获得的估值高于新结点原有的估值,则更新 47 | if (new_state not in best_value_of 48 | or value_of(new_state) > best_value_of[new_state]): 49 | # TODO:更新状态价值,扩展待访问的节点集——2行 50 | # TODO:记录路径——1行 51 | 52 | if state.success(): 53 | show_reversed_path(last_state_of, state) -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/algorithm/uniform_cost_search.py: -------------------------------------------------------------------------------- 1 | from interface.state import StateBase 2 | from .heuristic_search import HeuristicSearch 3 | 4 | # 一致代价搜索一个解 5 | class UniformCostSearch: 6 | 7 | def __init__(self, state:StateBase): 8 | self._heuristic_search = HeuristicSearch(state) 9 | 10 | # 一致代价搜索即为仅使用:到达当前状态的总花费*(-1) 作为状态估值的启发式搜索 11 | # 可以理解为搜到目标就结束的dijkstra算法 12 | def search(self) -> None: 13 | self._heuristic_search.search(lambda state: -state.cumulative_cost()) -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/handout/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("..") -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/queens_bfs_dfs.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | from algorithm.breadth_first_search import BreadthFirstSearch 4 | from algorithm.depth_first_search import DepthFirstSearch 5 | from problem.queens import QueensState 6 | 7 | if __name__ == '__main__': 8 | 9 | t0 = time() 10 | 11 | s = QueensState(11) 12 | 13 | bfs = BreadthFirstSearch(s) 14 | 15 | bfs.search(True, False) 16 | 17 | #dfs = DepthFirstSearch(s) 18 | 19 | #dfs.search(True, False) 20 | 21 | print(f"time = {time() - t0}s") -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/short_path_ucs.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | from problem.directed_graph import * 4 | from algorithm.uniform_cost_search import * 5 | from algorithm.heuristic_search import * 6 | 7 | if __name__ == "__main__": 8 | 9 | pos_names = [ 10 | "Oradea", # 0 11 | "Zerind", 12 | "Arad", # start:2 13 | "Sibiu", 14 | "Fagaras", 15 | 16 | "Timisoara", # 5 17 | "Rimnicu Vilcea", 18 | "Lugoj", 19 | "Pitesti", 20 | "Mehadia", 21 | 22 | "Drobeta", # 10 23 | "Craiova", 24 | "Neamt", 25 | "Iasi", 26 | "Vaslui", 27 | 28 | "Giurgiu", # 15 29 | "Bucharest", # end:16 30 | "Urziceni", 31 | "Hirsova", 32 | "Eforie" 33 | ] 34 | 35 | # 各点到目标点(编号16:Bucharest的直线距离) 36 | to_target_dis = [ 37 | 380, 374, 366, 253, 176, 38 | 329, 193, 244, 100, 241, 39 | 242, 160, 234, 226, 199, 40 | 77, 0, 80, 151, 161 41 | ] 42 | 43 | # 各条边的起点,无向边仅记录一次 44 | u = [0,0,1,2,2, 5,7,9,10,11, 11,3,3,4,6, 8,16,16,12,13, 14,17,18] 45 | 46 | # 各条边的终点 47 | v = [1,3,2,5,3, 7,9,10,11,6, 8,6,4,16,8, 16,15,17,13,14, 17,18,19] 48 | 49 | # 各条边的权重 50 | w = [71,151,75,118,140, 111,70,75,120,146, 138,80,99,211,97, 101,90,85,87,92, 142,98,86] 51 | 52 | graph = DirectedGraph(20) 53 | 54 | for x,y,z in zip(u, v, w): 55 | graph.add_edge(x, y, z) 56 | graph.add_edge(y, x, z) 57 | 58 | state = DirectedGraphState(graph, 2, 16) 59 | 60 | hs = HeuristicSearch(state) 61 | hs.search(lambda s: -s.cumulative_cost()) 62 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/handout/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/python/utils/show_path.py: -------------------------------------------------------------------------------- 1 | from typing import Dict 2 | from queue import LifoQueue, Queue 3 | 4 | from interface.state import StateBase 5 | 6 | def show_reversed_path(last_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 7 | 8 | s = state 9 | path = LifoQueue() 10 | 11 | while s in last_state_of.keys(): 12 | path.put(s) 13 | s = last_state_of[s] 14 | path.put(s) 15 | 16 | print("") 17 | while not path.empty(): 18 | s = path.get() 19 | s.show() 20 | print("") 21 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/handout/实验报告1_学号_姓名.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/handout/实验报告1_学号_姓名.docx -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("..") -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/algorithm/breadth_first_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from queue import Queue 3 | 4 | from interface.state import StateBase 5 | from utils.show_path import show_reversed_path 6 | 7 | 8 | # 广度优先搜索全部解 9 | class BreadthFirstSearch: 10 | def __init__(self, state: StateBase): 11 | assert isinstance(state, StateBase) 12 | self.initial_state = deepcopy(state) 13 | 14 | # tree_search决定是否判断重复状态,require_path决定是否记录路径 15 | def search(self, tree_search: bool = True, require_path: bool = True) -> None: 16 | # 宽搜状态队列 17 | states_queue = Queue() 18 | 19 | # 记录状态的前一个状态,用于记录路径 20 | last_state_of = dict() 21 | 22 | # 防止重复状态,判断哪些状态已经访问过 23 | explored_states = set() 24 | 25 | states_queue.put(self.initial_state) 26 | explored_states.add(self.initial_state) 27 | queue_peek_size = 0 28 | 29 | while not states_queue.empty(): 30 | state = states_queue.get() 31 | 32 | if queue_peek_size < states_queue.qsize(): 33 | queue_peek_size = states_queue.qsize() 34 | 35 | if state.success(): 36 | # if require_path: 37 | # show_reversed_path(last_state_of, state) 38 | # else: 39 | # state.show() 40 | continue 41 | 42 | if state.fail(): 43 | continue 44 | 45 | # 考虑所有可能动作,扩展全部子状态 46 | for action in state.action_space(): 47 | new_state = state.next(action) 48 | 49 | if tree_search: 50 | states_queue.put(new_state) 51 | if require_path: 52 | last_state_of[new_state] = state 53 | 54 | elif new_state not in explored_states: 55 | states_queue.put(new_state) 56 | explored_states.add(new_state) 57 | if require_path: 58 | last_state_of[new_state] = state 59 | 60 | print(f"Queue peek size: {queue_peek_size}") 61 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/algorithm/uniform_cost_search.py: -------------------------------------------------------------------------------- 1 | from interface.state import StateBase 2 | from .heuristic_search import HeuristicSearch 3 | 4 | # 一致代价搜索一个解 5 | class UniformCostSearch: 6 | 7 | def __init__(self, state:StateBase): 8 | self._heuristic_search = HeuristicSearch(state) 9 | 10 | # 一致代价搜索即为仅使用:到达当前状态的总花费*(-1) 作为状态估值的启发式搜索 11 | # 可以理解为搜到目标就结束的dijkstra算法 12 | def search(self) -> None: 13 | self._heuristic_search.search(lambda state: -state.cumulative_cost()) -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("..") -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/queens_bfs_dfs.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | from algorithm.breadth_first_search import BreadthFirstSearch 4 | from algorithm.depth_first_search import DepthFirstSearch 5 | from problem.queens import QueensState 6 | 7 | if __name__ == "__main__": 8 | # t0 = time() 9 | 10 | # s = QueensState(12) 11 | 12 | # bfs = BreadthFirstSearch(s) 13 | 14 | # bfs.search(True, False) 15 | 16 | # dfs = DepthFirstSearch(s) 17 | 18 | # dfs.search(True, False) 19 | for i in range(8, 14): 20 | t0 = time() 21 | print(f"Queens State for {i} queens:") 22 | s = QueensState(i) 23 | bfs = BreadthFirstSearch(s) 24 | bfs.search(True, False) 25 | print(f"\tBFS time = {time() - t0}s") 26 | t0 = time() 27 | dfs = DepthFirstSearch(s) 28 | dfs.search(True, False) 29 | print(f"\tDFS time = {time() - t0}s") 30 | print("----") 31 | 32 | # print(f"time = {time() - t0}s") 33 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/05-Global-Search/python/utils/show_path.py: -------------------------------------------------------------------------------- 1 | from typing import Dict 2 | from queue import LifoQueue, Queue 3 | 4 | from interface.state import StateBase 5 | 6 | def show_reversed_path(last_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 7 | 8 | s = state 9 | path = LifoQueue() 10 | 11 | while s in last_state_of.keys(): 12 | path.put(s) 13 | s = last_state_of[s] 14 | # print(s.current_node) 15 | path.put(s) 16 | 17 | print("") 18 | while not path.empty(): 19 | s = path.get() 20 | s.show() 21 | print("") 22 | -------------------------------------------------------------------------------- /Homework/05-Global-Search/全局搜索实验报告_2110306206_Arthals.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/全局搜索实验报告_2110306206_Arthals.docx -------------------------------------------------------------------------------- /Homework/05-Global-Search/全局搜索实验报告_2110306206_Arthals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/05-Global-Search/全局搜索实验报告_2110306206_Arthals.pdf -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/algorithm/genetic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "../interface/population.hpp" 7 | #include "../utils/random_variables.hpp" 8 | 9 | // 遗传算法 10 | template 11 | class GeneticAlgorithm{ 12 | private: 13 | 14 | using ChromosomeType = typename PopulationType::ChromosomeBaseType; 15 | 16 | static_assert(std::is_base_of, PopulationType>::value, "PopulationType not derived from PopulationBase."); 17 | 18 | PopulationType _population; 19 | 20 | public: 21 | 22 | // 传入初始种群 23 | GeneticAlgorithm(const PopulationType& init_population) : _population(init_population) {} 24 | 25 | // 获得当前种群中每个个体的列表 26 | std::vector population() const {return _population.population();} 27 | 28 | // 当前种群中每个个体的适应度 29 | std::vector adaptability() const {return _population.adaptability();} 30 | 31 | // 进化n轮 32 | void evolve(int n){ 33 | 34 | for (int i = 0; i < n; ++ i){ 35 | 36 | std::cout << "" << std::endl; 37 | 38 | // 交叉操作,在整个种群中选择若干个体交叉生成子代,改变种群 39 | _population.cross(); 40 | 41 | // 突变操作:在整个种群中选择若干个体发生突变,改变种群 42 | _population.mutate(); 43 | 44 | // 输出当前种群情况 45 | _population.show(); 46 | 47 | std::cout << "" << std::endl; 48 | } 49 | } 50 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/interface/constraint_satisfaction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | class ConstraintSatisfactionBase { 9 | public: 10 | 11 | using VariableBaseType = VariableType; 12 | 13 | ConstraintSatisfactionBase() = default; 14 | virtual ~ConstraintSatisfactionBase() = default; 15 | 16 | static_assert(std::is_arithmetic::value, "Variable type not arithmetic."); 17 | 18 | virtual int n_variables() const = 0; 19 | 20 | // 约束满足问题的变元 21 | virtual std::vector variables() const = 0; 22 | 23 | // 每个变元不满足的约束个数 24 | virtual int conflicts_of(int variable_index) const = 0; 25 | 26 | // 是否仍然存在冲突 27 | virtual bool has_conflict() const = 0; 28 | 29 | // 每个变元取值的备选集 30 | virtual std::vector choices_of(int variable_index) const = 0; 31 | 32 | // 设置某个变元的值 33 | virtual void set_variable(int variable_index, VariableType new_value) = 0; 34 | 35 | // 重新开始 36 | virtual void reset() = 0; 37 | 38 | virtual void show() const = 0; 39 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/interface/population.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 种群类,ChromosomeType用于表示个体的基因序列 6 | template 7 | class PopulationBase{ 8 | public: 9 | 10 | PopulationBase() = default; 11 | virtual ~PopulationBase() = default; 12 | 13 | using ChromosomeBaseType = ChromosomeType; 14 | 15 | // 当前种群中所有个体的列表 16 | virtual std::vector population() const = 0; 17 | 18 | // 当前种群中所有个体的适应度 19 | virtual std::vector adaptability() const = 0; 20 | 21 | virtual void show() const = 0; 22 | 23 | // 在种群中选择若干个体进行交叉,更新种群 24 | virtual void cross() = 0; 25 | 26 | // 在种群中选择若干个体进行突变,更新种群 27 | virtual void mutate() = 0; 28 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | using ActionBaseType = ActionType ; 24 | 25 | // 当前状态下可选择的动作集 26 | virtual std::vector action_space() const = 0; 27 | 28 | // 转移到当前状态的步骤的花费 29 | virtual double cost() const = 0; 30 | 31 | // 从初始状态出发转移到当前状态的总花费 32 | virtual double cumulative_cost() const = 0; 33 | 34 | // 判断当前状态是否为目标状态 35 | virtual bool success() const = 0; 36 | 37 | // 判断从当前状态是否已经不可能转移到目标状态 38 | virtual bool fail() const = 0; 39 | 40 | // 打印当前状态 41 | virtual void show() const = 0; 42 | 43 | // 从当前状态通过动作生成下一步状态 44 | virtual const StateBase& next(const ActionType&) const = 0; 45 | 46 | // 状态哈希 47 | friend struct std::hash; 48 | 49 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 50 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 51 | return s1.cumulative_cost() == s2.cumulative_cost(); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/interface/state_local.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "state.hpp" 6 | 7 | // 状态接口:实现此接口可用于局部搜索算法(包括爬山和模拟退火,但不包括最小化冲突和遗传算法,因为这两者基于不同的问题建模) 8 | class StateLocalBase{ 9 | public: 10 | 11 | StateLocalBase() = default; 12 | virtual ~StateLocalBase() = default; 13 | 14 | // 生成初始状态 15 | virtual void reset() = 0; 16 | 17 | // 邻居节点的个数 18 | virtual int neighbor_count() const = 0; 19 | 20 | // 按照编号生成邻居节点 21 | virtual const StateLocalBase& neighbor(int neighbor_index) const = 0; 22 | 23 | // 打印状态 24 | virtual void show() const = 0; 25 | }; 26 | 27 | 28 | // 将全局搜索模型的状态包装为局部搜索模型的状态 29 | template 30 | class StateLocalWrapper : public StateLocalBase{ 31 | private: 32 | 33 | StateType _state; 34 | using ActionType = typename StateType::ActionBaseType; 35 | static_assert(std::is_base_of, StateType>::value, "StateType not derived from StateBase."); 36 | 37 | public: 38 | 39 | StateLocalWrapper() = default; 40 | StateLocalWrapper(const StateType& state) : _state(state) {} 41 | 42 | inline const StateType& state() const {return _state;} 43 | 44 | inline void reset() override {} 45 | 46 | inline int neighbor_count() const override { 47 | return _state.action_space().size(); 48 | } 49 | 50 | const StateLocalWrapper& neighbor(int neighbor_index) const override { 51 | static StateLocalWrapper next_state; 52 | next_state._state = _state.next(_state.action_space()[neighbor_index]); 53 | return next_state; 54 | } 55 | 56 | void show() const override { 57 | _state.show(); 58 | } 59 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/problem/queens_swap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "../interface/state_local.hpp" 6 | #include "queens_move.hpp" 7 | 8 | class QueensSwapState : public StateLocalBase{ 9 | private: 10 | 11 | std::vector > _action_space; 12 | QueensMoveState _state; 13 | 14 | public: 15 | 16 | // 初始化为不同行且不同列的皇后 17 | QueensSwapState(int n) : _state(n){ 18 | for (int i = 0; i < n; ++ i){ 19 | for (int j = i + 1; j < n; ++ j){ 20 | _action_space.push_back(std::make_pair(i, j)); 21 | } 22 | } 23 | } 24 | QueensSwapState() = default; 25 | 26 | inline const QueensMoveState& state() const {return _state;} 27 | 28 | inline std::vector > action_space() const {return _action_space;} 29 | 30 | // 交换两行,得到新的状态 31 | const QueensSwapState& neighbor(int neighbor_index) const override { 32 | static QueensSwapState next_state; 33 | int row1 = _action_space[neighbor_index].first; 34 | int row2 = _action_space[neighbor_index].second; 35 | 36 | next_state = *this; 37 | next_state._state = next_state._state.neighbor(row1 * _state.n_queens() + _state.queens()[row2]); 38 | next_state._state = next_state._state.neighbor(row2 * _state.n_queens() + _state.queens()[row1]); 39 | 40 | return next_state; 41 | } 42 | 43 | inline int neighbor_count() const override { 44 | return _action_space.size(); 45 | } 46 | 47 | void reset() override { 48 | _state.reset(); 49 | } 50 | 51 | void show() const override { 52 | _state.show(); 53 | } 54 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_conflict_minimize: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/c++/queens_conflict_minimize -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_conflict_minimize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "problem/queens_constraint.hpp" 6 | #include "algorithm/conflicts_minimize.hpp" 7 | #include "utils/selection.hpp" 8 | 9 | // 在保证非负单调递增的前提下,估值函数仅对轮盘赌算法起作用 10 | double alpha = 10; 11 | double value_of(int value) { 12 | return exp(alpha * value); 13 | } 14 | 15 | int main() { 16 | 17 | // time_t t0 = time(nullptr); 18 | auto t0 = std::chrono::high_resolution_clock::now(); 19 | 20 | std::ios::sync_with_stdio(false); 21 | 22 | int n = 15000; 23 | 24 | QueensConstraintSatisfaction q(n); 25 | 26 | ConflictMinimize cm(q); 27 | 28 | FirstBetterSelection fbs; 29 | RouletteSelection rs; 30 | MaxSelection ms; 31 | 32 | std::cout << "Question: " << n << " queens" << std::endl; 33 | 34 | // 随机重启尝试10轮,每轮最多更改变元4n次 35 | // ms: 最大选择算法,优先选择冲突最多的变元更改,优先更改到冲突最小的值(移步algorithm/conflict_minimize.hpp阅读) 36 | // value_of: 因为使用最大选择算法,因此估值函数直接使用默认的指数函数即可 37 | cm.search(10, n << 2, ms); 38 | 39 | // std::cout << "Total time: " << time(nullptr) - t0 << std::endl; 40 | auto t1 = std::chrono::high_resolution_clock::now(); 41 | auto duration = std::chrono::duration_cast(t1 - t0).count(); 42 | std::cout << "Total time: " << duration << " ms" << std::endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_genetic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/c++/queens_genetic -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_hill_climb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/c++/queens_hill_climb -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_hill_climb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "interface/state_local.hpp" 6 | #include "problem/queens_move.hpp" 7 | #include "problem/queens_swap.hpp" 8 | #include "algorithm/hill_climb.hpp" 9 | #include "utils/selection.hpp" 10 | 11 | int n = 220; 12 | double log_n = log(n); 13 | 14 | // 使用RouletteSelection时可以尝试修改下面的两个估值函数来获得更好的效果 15 | double queens_move_state_value_estimator(const QueensMoveState& state) { 16 | return exp(-log_n * state.conflicts()); 17 | } 18 | 19 | double queens_swap_state_value_estimator(const QueensSwapState& state) { 20 | return exp(-log_n * state.state().conflicts()); 21 | } 22 | 23 | int main() { 24 | 25 | auto t0 = std::chrono::high_resolution_clock::now(); 26 | 27 | std::ios::sync_with_stdio(false); 28 | 29 | // 选择第一个比当前状态好的邻居状态 30 | FirstBetterSelection f_selection; 31 | // 按照与价值成正比的概率选择邻居状态 32 | RouletteSelection r_selection; 33 | // 选择价值最高的邻居状态 34 | MaxSelection m_selection; 35 | 36 | // QueensMoveState state(n); 37 | 38 | // 用QueensMoveState的问题建模(动作为将某一行的皇后移动到某一列) 39 | // HillClimb hcs(state); 40 | 41 | // 适应度达到1.0(无冲突皇后,由估值函数决定)则算法终止,至多迭代4n步,随机重启运行5次。 42 | // hcs.search(queens_move_state_value_estimator, 1.0, n << 2, m_selection, 5); 43 | 44 | // 用QueensSwapState的问题建模(动作为交换两行的皇后) 45 | QueensSwapState state(n); 46 | HillClimb hcs(state); 47 | hcs.search(queens_swap_state_value_estimator, 1.0, n << 2, f_selection, 5); 48 | 49 | auto t1 = std::chrono::high_resolution_clock::now(); 50 | auto duration = std::chrono::duration_cast(t1 - t0).count(); 51 | std::cout << "Total time: " << duration << " ms" << std::endl; 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_simulated_anneal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/c++/queens_simulated_anneal -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/queens_simulated_anneal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "problem/queens_move.hpp" 6 | #include "problem/queens_swap.hpp" 7 | #include "algorithm/simulated_anneal.hpp" 8 | 9 | int n = 220; 10 | int max_conflicts = n * (n - 1) >> 1; 11 | 12 | // 目标值:state.state().conflicts() == 0,即函数返回max_conflicts 13 | double value_estimator_swap(const QueensSwapState& state) { 14 | return max_conflicts - state.state().conflicts(); 15 | } 16 | 17 | double value_estimator_move(const QueensMoveState& state) { 18 | return max_conflicts - state.conflicts(); 19 | } 20 | 21 | // 温度随时间变化的方式也可以尝试修改 22 | double temperature_schedule_move(int time) { 23 | static double log_n = log(n); 24 | static double start_temp_log = n / log_n; 25 | return exp(start_temp_log - double(time) / (n << 4)); 26 | } 27 | 28 | double temperature_schedule_swap(int time) { 29 | static double log_n = log(n); 30 | static double start_temp_log = n / log_n; 31 | return exp(start_temp_log - double(time) / n); 32 | } 33 | 34 | int main() { 35 | 36 | // time_t t0 = time(nullptr); 37 | auto t0 = std::chrono::high_resolution_clock::now(); 38 | 39 | std::ios::sync_with_stdio(false); 40 | 41 | // 用QueensSwapState的问题建模(动作为交换两行的皇后) 42 | // QueensSwapState q(n); 43 | // SimulatedAnneal sa(q); 44 | // // 尝试4n次模拟退火,终态温度为10^-16 45 | // sa.search(value_estimator_swap, temperature_schedule_swap, n << 2, max_conflicts, 1e-16); 46 | 47 | // 用QueensMoveState的问题建模(动作为将某一行的皇后移动到某一列) 48 | QueensMoveState q(n); 49 | SimulatedAnneal sa(q); 50 | sa.search(value_estimator_move, temperature_schedule_move, n << 4, max_conflicts, 1e-18); 51 | 52 | // std::cout << "Total time: " << time(nullptr) - t0 << std::endl; 53 | auto t1 = std::chrono::high_resolution_clock::now(); 54 | auto duration = std::chrono::duration_cast(t1 - t0).count(); 55 | std::cout << "Total time: " << duration << " ms" << std::endl; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/start.sh: -------------------------------------------------------------------------------- 1 | # hill_climb 2 | # g++ ./queens_hill_climb.cpp -o ./queens_hill_climb --std=c++11 -O3 && ./queens_hill_climb 3 | 4 | # simulated_anneal 5 | # g++ ./queens_simulated_anneal.cpp -o ./queens_simulated_anneal --std=c++11 -O3 && ./queens_simulated_anneal 6 | 7 | # genetic 8 | # g++ ./queens_genetic.cpp -o ./queens_genetic --std=c++11 -O3 && ./queens_genetic 9 | 10 | g++ ./queens_conflict_minimize.cpp -o ./queens_conflict_minimize --std=c++11 -O3 && ./queens_conflict_minimize 11 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/c++/utils/random_variables.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // Singleton Pattern, Provide random variables 8 | class RandomVariables{ 9 | private: 10 | 11 | // 固定随机种子 12 | RandomVariables() = default; 13 | 14 | // 非固定随机种子 15 | // RandomVariables() : random_engine(time(nullptr)) {} 16 | 17 | ~RandomVariables() = default; 18 | 19 | std::default_random_engine random_engine; 20 | std::uniform_real_distribution uniform_dist; 21 | std::uniform_int_distribution uniform_int_dist; 22 | 23 | static RandomVariables rv; 24 | 25 | public: 26 | 27 | // 均匀分布的正整数 28 | static int uniform_int(){ 29 | return rv.uniform_int_dist(rv.random_engine); 30 | } 31 | 32 | // [0,1)均匀分布的实数 33 | static double uniform_real(){ 34 | return rv.uniform_dist(rv.random_engine); 35 | } 36 | 37 | // 等概率分布的{0,1,2,n-1}排列 38 | static std::vector uniform_permutation(int n){ 39 | std::vector permutation(n); 40 | for (int i = 0; i < n; ++ i){ 41 | permutation[i] = i; 42 | } 43 | 44 | for (int i = 0, j; i < n; ++ i){ 45 | j = uniform_int() % (n - i) + i; 46 | std::swap(permutation[i], permutation[j]); 47 | } 48 | 49 | return permutation; 50 | } 51 | }; 52 | 53 | RandomVariables RandomVariables::rv; 54 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/algorithm/genetic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "../interface/population.hpp" 7 | #include "../utils/random_variables.hpp" 8 | 9 | // 遗传算法 10 | template 11 | class GeneticAlgorithm{ 12 | private: 13 | 14 | using ChromosomeType = typename PopulationType::ChromosomeBaseType; 15 | 16 | static_assert(std::is_base_of, PopulationType>::value, "PopulationType not derived from PopulationBase."); 17 | 18 | PopulationType _population; 19 | 20 | public: 21 | 22 | // 传入初始种群 23 | GeneticAlgorithm(const PopulationType& init_population) : _population(init_population) {} 24 | 25 | // 获得当前种群中每个个体的列表 26 | std::vector population() const {return _population.population();} 27 | 28 | // 当前种群中每个个体的适应度 29 | std::vector adaptability() const {return _population.adaptability();} 30 | 31 | // 进化n轮 32 | void evolve(int n){ 33 | 34 | for (int i = 0; i < n; ++ i){ 35 | 36 | std::cout << "" << std::endl; 37 | 38 | // 交叉操作,在整个种群中选择若干个体交叉生成子代,改变种群 39 | _population.cross(); 40 | 41 | // 突变操作:在整个种群中选择若干个体发生突变,改变种群 42 | _population.mutate(); 43 | 44 | // 输出当前种群情况 45 | _population.show(); 46 | 47 | std::cout << "" << std::endl; 48 | } 49 | } 50 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/interface/constraint_satisfaction.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | class ConstraintSatisfactionBase{ 9 | public: 10 | 11 | using VariableBaseType = VariableType; 12 | 13 | ConstraintSatisfactionBase() = default; 14 | virtual ~ConstraintSatisfactionBase() = default; 15 | 16 | static_assert(std::is_arithmetic::value, "Variable type not arithmetic."); 17 | 18 | virtual int n_variables() const = 0; 19 | 20 | // 约束满足问题的变元 21 | virtual std::vector variables() const = 0; 22 | 23 | // 每个变元不满足的约束个数 24 | virtual int conflicts_of(int variable_index) const = 0; 25 | 26 | // 是否仍然存在冲突 27 | virtual bool has_conflict() const = 0; 28 | 29 | // 每个变元取值的备选集 30 | virtual std::vector choices_of(int variable_index) const = 0; 31 | 32 | // 设置某个变元的值 33 | virtual void set_variable(int variable_index, VariableType new_value) = 0; 34 | 35 | // 重新开始 36 | virtual void reset() = 0; 37 | 38 | virtual void show() const = 0; 39 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/interface/population.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 种群类,ChromosomeType用于表示个体的基因序列 6 | template 7 | class PopulationBase{ 8 | public: 9 | 10 | PopulationBase() = default; 11 | virtual ~PopulationBase() = default; 12 | 13 | using ChromosomeBaseType = ChromosomeType; 14 | 15 | // 当前种群中所有个体的列表 16 | virtual std::vector population() const = 0; 17 | 18 | // 当前种群中所有个体的适应度 19 | virtual std::vector adaptability() const = 0; 20 | 21 | virtual void show() const = 0; 22 | 23 | // 在种群中选择若干个体进行交叉,更新种群 24 | virtual void cross() = 0; 25 | 26 | // 在种群中选择若干个体进行突变,更新种群 27 | virtual void mutate() = 0; 28 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | using ActionBaseType = ActionType ; 24 | 25 | // 当前状态下可选择的动作集 26 | virtual std::vector action_space() const = 0; 27 | 28 | // 转移到当前状态的步骤的花费 29 | virtual double cost() const = 0; 30 | 31 | // 从初始状态出发转移到当前状态的总花费 32 | virtual double cumulative_cost() const = 0; 33 | 34 | // 判断当前状态是否为目标状态 35 | virtual bool success() const = 0; 36 | 37 | // 判断从当前状态是否已经不可能转移到目标状态 38 | virtual bool fail() const = 0; 39 | 40 | // 打印当前状态 41 | virtual void show() const = 0; 42 | 43 | // 从当前状态通过动作生成下一步状态 44 | virtual const StateBase& next(const ActionType&) const = 0; 45 | 46 | // 状态哈希 47 | friend struct std::hash; 48 | 49 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 50 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 51 | return s1.cumulative_cost() == s2.cumulative_cost(); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/interface/state_local.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "state.hpp" 6 | 7 | // 状态接口:实现此接口可用于局部搜索算法(包括爬山和模拟退火,但不包括最小化冲突和遗传算法,因为这两者基于不同的问题建模) 8 | class StateLocalBase{ 9 | public: 10 | 11 | StateLocalBase() = default; 12 | virtual ~StateLocalBase() = default; 13 | 14 | // 生成初始状态 15 | virtual void reset() = 0; 16 | 17 | // 邻居节点的个数 18 | virtual int neighbor_count() const = 0; 19 | 20 | // 按照编号生成邻居节点 21 | virtual const StateLocalBase& neighbor(int neighbor_index) const = 0; 22 | 23 | // 打印状态 24 | virtual void show() const = 0; 25 | }; 26 | 27 | 28 | // 将全局搜索模型的状态包装为局部搜索模型的状态 29 | template 30 | class StateLocalWrapper : public StateLocalBase{ 31 | private: 32 | 33 | StateType _state; 34 | using ActionType = typename StateType::ActionBaseType; 35 | static_assert(std::is_base_of, StateType>::value, "StateType not derived from StateBase."); 36 | 37 | public: 38 | 39 | StateLocalWrapper() = default; 40 | StateLocalWrapper(const StateType& state) : _state(state) {} 41 | 42 | inline const StateType& state() const {return _state;} 43 | 44 | inline void reset() override {} 45 | 46 | inline int neighbor_count() const override { 47 | return _state.action_space().size(); 48 | } 49 | 50 | const StateLocalWrapper& neighbor(int neighbor_index) const override { 51 | static StateLocalWrapper next_state; 52 | next_state._state = _state.next(_state.action_space()[neighbor_index]); 53 | return next_state; 54 | } 55 | 56 | void show() const override { 57 | _state.show(); 58 | } 59 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/problem/queens_swap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "../interface/state_local.hpp" 6 | #include "queens_move.hpp" 7 | 8 | class QueensSwapState : public StateLocalBase{ 9 | private: 10 | 11 | std::vector > _action_space; 12 | QueensMoveState _state; 13 | 14 | public: 15 | 16 | // 初始化为不同行且不同列的皇后 17 | QueensSwapState(int n) : _state(n){ 18 | for (int i = 0; i < n; ++ i){ 19 | for (int j = i + 1; j < n; ++ j){ 20 | _action_space.push_back(std::make_pair(i, j)); 21 | } 22 | } 23 | } 24 | QueensSwapState() = default; 25 | 26 | inline const QueensMoveState& state() const {return _state;} 27 | 28 | inline std::vector > action_space() const {return _action_space;} 29 | 30 | // 交换两行,得到新的状态 31 | const QueensSwapState& neighbor(int neighbor_index) const override { 32 | static QueensSwapState next_state; 33 | int row1 = _action_space[neighbor_index].first; 34 | int row2 = _action_space[neighbor_index].second; 35 | 36 | next_state = *this; 37 | next_state._state = next_state._state.neighbor(row1 * _state.n_queens() + _state.queens()[row2]); 38 | next_state._state = next_state._state.neighbor(row2 * _state.n_queens() + _state.queens()[row1]); 39 | 40 | return next_state; 41 | } 42 | 43 | inline int neighbor_count() const override { 44 | return _action_space.size(); 45 | } 46 | 47 | void reset() override { 48 | _state.reset(); 49 | } 50 | 51 | void show() const override { 52 | _state.show(); 53 | } 54 | }; -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/queens_conflict_minimize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "problem/queens_constraint.hpp" 5 | #include "algorithm/conflicts_minimize.hpp" 6 | #include "utils/selection.hpp" 7 | 8 | // 在保证非负单调递增的前提下,估值函数仅对轮盘赌算法起作用 9 | double alpha = 10; 10 | double value_of(int value){ 11 | return exp(alpha * value); 12 | } 13 | 14 | int main(){ 15 | 16 | time_t t0 = time(nullptr); 17 | 18 | std::ios::sync_with_stdio(false); 19 | 20 | int n = 1000; 21 | 22 | QueensConstraintSatisfaction q(n); 23 | 24 | ConflictMinimize cm(q); 25 | 26 | FirstBetterSelection fbs; 27 | RouletteSelection rs; 28 | MaxSelection ms; 29 | 30 | // 随机重启尝试10轮,每轮最多更改变元4n次 31 | // ms: 最大选择算法,优先选择冲突最多的变元更改,优先更改到冲突最小的值(移步algorithm/conflict_minimize.hpp阅读) 32 | // value_of: 因为使用最大选择算法,因此估值函数直接使用默认的指数函数即可 33 | cm.search(10, n << 2, ms); 34 | 35 | std::cout << "Total time: " << time(nullptr) - t0 << std::endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/queens_hill_climb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "interface/state_local.hpp" 5 | #include "problem/queens_move.hpp" 6 | #include "problem/queens_swap.hpp" 7 | #include "algorithm/hill_climb.hpp" 8 | #include "utils/selection.hpp" 9 | 10 | int n = 100; 11 | double log_n = log(n); 12 | 13 | // 使用RouletteSelection时可以尝试修改下面的两个估值函数来获得更好的效果 14 | double queens_move_state_value_estimator(const QueensMoveState& state){ 15 | return exp(-log_n * state.conflicts()); 16 | } 17 | 18 | double queens_swap_state_value_estimator(const QueensSwapState& state){ 19 | return exp(-log_n * state.state().conflicts()); 20 | } 21 | 22 | int main(){ 23 | 24 | time_t t0 = time(nullptr); 25 | 26 | std::ios::sync_with_stdio(false); 27 | 28 | // 选择第一个比当前状态好的邻居状态 29 | FirstBetterSelection f_selection; 30 | // 按照与价值成正比的概率选择邻居状态 31 | RouletteSelection r_selection; 32 | // 选择价值最高的邻居状态 33 | MaxSelection m_selection; 34 | 35 | QueensMoveState state(n); 36 | 37 | // 用QueensMoveState的问题建模(动作为将某一行的皇后移动到某一列) 38 | HillClimb hcs(state); 39 | 40 | // 适应度达到1.0(无冲突皇后,由估值函数决定)则算法终止,至多迭代4n步,随机重启运行5次。 41 | hcs.search(queens_move_state_value_estimator, 1.0, n << 2, f_selection, 5); 42 | 43 | // 用QueensSwapState的问题建模(动作为交换两行的皇后) 44 | // QueensSwapState state(n); 45 | // HillClimb hcs(state); 46 | // hcs.search(queens_swap_state_value_estimator, 1.0, n << 2, f_selection, 5); 47 | 48 | std::cout << "Total time: " << time(nullptr) - t0 << std::endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/queens_simulated_anneal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "problem/queens_move.hpp" 4 | #include "problem/queens_swap.hpp" 5 | #include "algorithm/simulated_anneal.hpp" 6 | 7 | int n = 100; 8 | int max_conflicts = n * (n-1) >> 1; 9 | 10 | // 目标值:state.state().conflicts() == 0,即函数返回max_conflicts 11 | double value_estimator_swap(const QueensSwapState& state){ 12 | return max_conflicts - state.state().conflicts(); 13 | } 14 | 15 | double value_estimator_move(const QueensMoveState& state){ 16 | return max_conflicts - state.conflicts(); 17 | } 18 | 19 | // 温度随时间变化的方式也可以尝试修改 20 | double temperature_schedule_move(int time){ 21 | static double log_n = log(n); 22 | static double start_temp_log = n/log_n; 23 | return exp(start_temp_log - double(time) / (n << 4)); 24 | } 25 | 26 | double temperature_schedule_swap(int time){ 27 | static double log_n = log(n); 28 | static double start_temp_log = n/log_n; 29 | return exp(start_temp_log - double(time) / n); 30 | } 31 | 32 | int main(){ 33 | 34 | time_t t0 = time(nullptr); 35 | 36 | std::ios::sync_with_stdio(false); 37 | 38 | // 用QueensSwapState的问题建模(动作为交换两行的皇后) 39 | QueensSwapState q(n); 40 | SimulatedAnneal sa(q); 41 | // 尝试4n次模拟退火,终态温度为10^-16 42 | sa.search(value_estimator_swap, temperature_schedule_swap, n << 2, max_conflicts, 1e-16); 43 | 44 | // 用QueensMoveState的问题建模(动作为将某一行的皇后移动到某一列) 45 | //QueensMoveState q(n); 46 | //SimulatedAnneal sa(q); 47 | //sa.search(value_estimator_move, temperature_schedule_move, n << 4, max_conflicts, 1e-18); 48 | 49 | std::cout << "Total time: " << time(nullptr) - t0 << std::endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/c++/utils/random_variables.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // Singleton Pattern, Provide random variables 8 | class RandomVariables{ 9 | private: 10 | 11 | // 固定随机种子 12 | RandomVariables() = default; 13 | 14 | // 非固定随机种子 15 | // RandomVariables() : random_engine(time(nullptr)) {} 16 | 17 | ~RandomVariables() = default; 18 | 19 | std::default_random_engine random_engine; 20 | std::uniform_real_distribution uniform_dist; 21 | std::uniform_int_distribution uniform_int_dist; 22 | 23 | static RandomVariables rv; 24 | 25 | public: 26 | 27 | // 均匀分布的正整数 28 | static int uniform_int(){ 29 | return rv.uniform_int_dist(rv.random_engine); 30 | } 31 | 32 | // [0,1)均匀分布的实数 33 | static double uniform_real(){ 34 | return rv.uniform_dist(rv.random_engine); 35 | } 36 | 37 | // 等概率分布的{0,1,2,n-1}排列 38 | static std::vector uniform_permutation(int n){ 39 | std::vector permutation(n); 40 | for (int i = 0; i < n; ++ i){ 41 | permutation[i] = i; 42 | } 43 | 44 | for (int i = 0, j; i < n; ++ i){ 45 | j = uniform_int() % (n - i) + i; 46 | std::swap(permutation[i], permutation[j]); 47 | } 48 | 49 | return permutation; 50 | } 51 | }; 52 | 53 | RandomVariables RandomVariables::rv; 54 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('../') -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/algorithm/genetic.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from interface.population import PopulationBase 4 | 5 | class GeneticAlgorithm: 6 | 7 | ChromosomeType = PopulationBase.ChromosomeType 8 | 9 | def __init__(self, init_population:PopulationBase): 10 | self.population = init_population 11 | 12 | def population(self) -> List[ChromosomeType]: 13 | return self.population.population() 14 | 15 | def adaptability(self) -> List[float]: 16 | return self.population.adaptability() 17 | 18 | def evolve(self, n:int) -> None: 19 | for i in range(n): 20 | print(f"") 21 | self.population.cross() 22 | self.population.mutate() 23 | self.population.show() 24 | print(f"") -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/handout/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/interface/constraint_satisfaction.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | from typing import List, Union 3 | 4 | class ConstraintSatisfactionBase(metaclass=ABCMeta): 5 | 6 | VariableType = Union[int, float] 7 | 8 | def __init__(self, n:int): 9 | pass 10 | 11 | @abstractmethod 12 | def n_variables(self) -> int: 13 | raise NotImplementedError 14 | 15 | @abstractmethod 16 | def variables(self) -> List[VariableType]: 17 | raise NotImplementedError 18 | 19 | @abstractmethod 20 | def conflicts_of(self, variable_index:int) -> int: 21 | raise NotImplementedError 22 | 23 | @abstractmethod 24 | def has_conflict(self) -> bool: 25 | raise NotImplementedError 26 | 27 | @abstractmethod 28 | def choices_of(self, variable_index:int) -> List[VariableType]: 29 | raise NotImplementedError 30 | 31 | @abstractmethod 32 | def set_variable(self, variable_index:int, new_value:VariableType): 33 | raise NotImplementedError 34 | 35 | @abstractmethod 36 | def reset(self) -> None: 37 | raise NotImplementedError 38 | 39 | @abstractmethod 40 | def show(self) -> None: 41 | raise NotImplementedError -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/interface/population.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | from typing import List, Any 3 | 4 | class PopulationBase(metaclass=ABCMeta): 5 | 6 | ChromosomeType = Any 7 | 8 | @abstractmethod 9 | def population(self) -> List[ChromosomeType]: 10 | raise NotImplementedError 11 | 12 | @abstractmethod 13 | def adaptability(self) -> List[float]: 14 | raise NotImplementedError 15 | 16 | @abstractmethod 17 | def show(self) -> None: 18 | raise NotImplementedError 19 | 20 | @abstractmethod 21 | def cross(self) -> None: 22 | raise NotImplementedError 23 | 24 | @abstractmethod 25 | def mutate(self) -> None: 26 | raise NotImplementedError -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/interface/state_local.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | 4 | class StateLocalBase(metaclass=ABCMeta): 5 | 6 | @abstractmethod 7 | def reset(self) -> None: 8 | raise NotImplementedError 9 | 10 | @abstractmethod 11 | def neighbor_count(self) -> int: 12 | raise NotImplementedError 13 | 14 | @abstractmethod 15 | def neighbor(self, neighbor_index:int) -> "StateLocalBase": 16 | raise NotImplementedError 17 | 18 | @abstractmethod 19 | def show(self) -> None: 20 | raise NotImplementedError 21 | 22 | class StateLocalWrapper(StateLocalBase): 23 | 24 | def __init__(self, state:StateLocalBase): 25 | self._state = state 26 | 27 | def state(self) -> StateLocalBase: return self._state 28 | 29 | def reset(self) -> None: 30 | pass 31 | 32 | def neighbor_count(self) -> int: 33 | return len(self._state.action_space()) 34 | 35 | def neighbor(self, neighbor_index:int) -> "StateLocalBase": 36 | next_state = StateLocalWrapper(self._state.next(self._state.action_space()[neighbor_index])) 37 | return next_state 38 | 39 | def show(self) -> None: 40 | self._state.show() -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('../') -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/problem/queens_constraint.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from interface.constraint_satisfaction import ConstraintSatisfactionBase 4 | from utils.random_variables import RandomVariables 5 | from .queens_move import QueensMoveState 6 | 7 | class QueensConstraintSatisfaction(QueensMoveState, ConstraintSatisfactionBase): 8 | 9 | def __init__(self, n:int): 10 | super().__init__(n) 11 | self._choices = list(range(n)) 12 | 13 | def n_variables(self) -> int: 14 | return self.n_queens 15 | 16 | def variables(self) -> List[int]: 17 | return self.queens 18 | 19 | def conflicts_of(self, variable_index:int) -> int: 20 | row, column = variable_index, self.queens[variable_index] 21 | return (self.column_count[column] 22 | + self.right_left_count[column + row] 23 | + self.left_right_count[column - row + self.n_queens - 1] 24 | - 3 25 | ) 26 | 27 | def has_conflict(self) -> bool: 28 | return any(self.conflicts_of(i) for i in range(self.n_queens)) 29 | 30 | def choices_of(self, variable_index:int) -> List[int]: 31 | return self._choices 32 | 33 | def set_variable(self, variable_index:int, new_variable:float) -> None: 34 | self._add_queen(variable_index, self.queens[variable_index], -1) 35 | self._add_queen(variable_index, new_variable) 36 | 37 | def show(self) -> None: 38 | QueensMoveState.show(self) 39 | 40 | def reset(self) -> None: 41 | QueensMoveState.reset(self) -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/problem/queens_move.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.state_local import StateLocalBase 4 | from utils.random_variables import RandomVariables 5 | class QueensMoveState(StateLocalBase): 6 | 7 | def _add_queen(self, row:int, column:int, n:int=1) -> None: 8 | self.queens[row] = column 9 | self.conflicts += n * (self.column_count[column] 10 | + self.right_left_count[column + row] 11 | + self.left_right_count[column - row + self.n_queens - 1] 12 | ) 13 | self.conflicts -= n * 3 if n < 0 else 0 14 | 15 | self.column_count[column] += n 16 | self.right_left_count[column + row] += n 17 | self.left_right_count[column - row + self.n_queens - 1] += n 18 | 19 | def __init__(self, n:int=0): 20 | self.n_queens = n 21 | self.reset() 22 | 23 | def reset(self) -> None: 24 | self.queens = RandomVariables.uniform_permutation(self.n_queens) 25 | self.column_count = [0] * self.n_queens 26 | self.left_right_count = [0] * (self.n_queens << 1) 27 | self.right_left_count = [0] * (self.n_queens << 1) 28 | self.conflicts = 0 29 | 30 | for i in range(self.n_queens): 31 | self._add_queen(i, self.queens[i]) 32 | 33 | def neighbor_count(self) -> int: 34 | return self.n_queens ** 2 35 | 36 | def neighbor(self, neighbor_index:int) -> "QueensMoveState": 37 | row = neighbor_index // self.n_queens 38 | column = self.queens[row] 39 | to_column = neighbor_index % self.n_queens 40 | 41 | next_state = deepcopy(self) 42 | next_state._add_queen(row, column, -1) 43 | next_state._add_queen(row, to_column) 44 | 45 | return next_state 46 | 47 | def show(self) -> None: 48 | print(f"Queens: {self.queens}") 49 | print(f"Conflicts: {self.conflicts}\n") -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/problem/queens_swap.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.state_local import StateLocalBase 4 | from .queens_move import QueensMoveState 5 | 6 | 7 | class QueensSwapState(StateLocalBase): 8 | 9 | def __init__(self, n:int): 10 | 11 | self.state = QueensMoveState(n) 12 | self.action_space = [(i, j) for i in range(n) for j in range(i+1, n)] 13 | 14 | def neighbor(self, neighbor_index:int) -> "QueensSwapState": 15 | row1, row2 = self.action_space[neighbor_index] 16 | 17 | next_state = deepcopy(self) 18 | next_state.state = next_state.state.neighbor(row1 * self.state.n_queens + self.state.queens[row2]) 19 | next_state.state = next_state.state.neighbor(row2 * self.state.n_queens + self.state.queens[row1]) 20 | 21 | return next_state 22 | 23 | def neighbor_count(self) -> int: 24 | return len(self.action_space) 25 | 26 | def reset(self) -> None: 27 | self.state.reset() 28 | 29 | def show(self) -> None: 30 | self.state.show() -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/queens_conflict_minimize.py: -------------------------------------------------------------------------------- 1 | from math import exp 2 | from time import time 3 | 4 | from problem.queens_constraint import QueensConstraintSatisfaction 5 | from algorithm.conflicts_minimize import ConflictMinimize 6 | from utils.selection import MaxSelection, FirstBetterSelection, RouletteSelection 7 | 8 | if __name__ == '__main__': 9 | t0 = time() 10 | alpha = 10 11 | value_of = lambda value: exp(alpha * value) 12 | 13 | n = 1000 14 | q = QueensConstraintSatisfaction(n) 15 | cm = ConflictMinimize(q) 16 | 17 | fbs = FirstBetterSelection() 18 | rs = RouletteSelection() 19 | ms = MaxSelection() 20 | 21 | cm.search(10, n << 2, ms) 22 | 23 | print(f"Total time: {time() - t0}") 24 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/queens_hill_climb.py: -------------------------------------------------------------------------------- 1 | from math import log, exp 2 | from time import time 3 | 4 | from problem.queens_move import QueensMoveState 5 | from problem.queens_swap import QueensSwapState 6 | from algorithm.hill_climb import HillClimb 7 | from utils.selection import MaxSelection, FirstBetterSelection, RouletteSelection 8 | 9 | 10 | if __name__ == "__main__": 11 | n = 20 12 | log_n = log(n) 13 | queens_move_state_value_estimator = lambda state : exp(-log_n * state.conflicts) 14 | queens_swap_state_value_estimator = lambda state : exp(-log_n * state.state.conflicts) 15 | 16 | t0 = time() 17 | f_selection = FirstBetterSelection() 18 | r_selection = RouletteSelection() 19 | m_selection = MaxSelection() 20 | 21 | # state = QueensMoveState(n) 22 | # hcs = HillClimb(state) 23 | # hcs.search(queens_move_state_value_estimator, 1.0, n<<2, f_selection, 5) 24 | 25 | state = QueensSwapState(n) 26 | hcs = HillClimb(state) 27 | hcs.search(queens_swap_state_value_estimator, 1.0, n<<2, f_selection, 5) 28 | 29 | print(time() - t0) -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/queens_simulated_anneal.py: -------------------------------------------------------------------------------- 1 | from math import exp, log 2 | from time import time 3 | 4 | from problem.queens_move import QueensMoveState 5 | from problem.queens_swap import QueensSwapState 6 | from algorithm.simulated_anneal import SimulatedAnneal 7 | 8 | if __name__ == '__main__': 9 | n = 20 10 | log_n = log(n) 11 | start_temp_log = n / log(n) 12 | max_conflicts = n * (n - 1) >> 1 13 | value_estimator_swap = lambda state: max_conflicts - state.state.conflicts 14 | value_estimator_move = lambda state: max_conflicts - state.conflicts 15 | 16 | temperature_schedule_move = lambda time: exp(start_temp_log - time / (n << 4)) 17 | temperature_schedule_swap = lambda time: exp(start_temp_log - time / n) 18 | 19 | t0 = time() 20 | 21 | q = QueensSwapState(n) 22 | sa = SimulatedAnneal(q) 23 | sa.search(value_estimator_swap, temperature_schedule_swap, n << 2, max_conflicts, 1e-16) 24 | 25 | #q = QueensMoveState(n) 26 | #sa = SimulatedAnneal(q) 27 | #sa.search(value_estimator_move, temperature_schedule_move, n << 2, max_conflicts, 1e-16) 28 | 29 | print(f"Total time: {time() - t0}") -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/handout/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/python/utils/random_variables.py: -------------------------------------------------------------------------------- 1 | import random 2 | from typing import List 3 | 4 | class RandomVariables: 5 | _int_max = 2147483647 6 | seed = 0 7 | random.seed(seed) 8 | 9 | def __init__(self): 10 | raise RuntimeError("RandomVariables can not be instantiated.") 11 | 12 | @classmethod 13 | def uniform_int(cls) -> int: 14 | return random.randint(0, cls._int_max) 15 | 16 | @classmethod 17 | def uniform_real(cls) -> float: 18 | return random.random() 19 | 20 | @classmethod 21 | def uniform_permutation(cls, n:int) -> List[int]: 22 | p = list(range(n)) 23 | 24 | for i in range(n): 25 | j = random.randint(0, n-1) 26 | p[i], p[j] = p[j], p[i] 27 | 28 | return p 29 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/handout/实验报告2_学号_姓名.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/handout/实验报告2_学号_姓名.docx -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('../') -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/algorithm/genetic.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from interface.population import PopulationBase 4 | 5 | class GeneticAlgorithm: 6 | 7 | ChromosomeType = PopulationBase.ChromosomeType 8 | 9 | def __init__(self, init_population:PopulationBase): 10 | self.population = init_population 11 | 12 | def population(self) -> List[ChromosomeType]: 13 | return self.population.population() 14 | 15 | def adaptability(self) -> List[float]: 16 | return self.population.adaptability() 17 | 18 | def evolve(self, n:int) -> None: 19 | for i in range(n): 20 | print(f"") 21 | self.population.cross() 22 | self.population.mutate() 23 | self.population.show() 24 | print(f"") -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/interface/constraint_satisfaction.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | from typing import List, Union 3 | 4 | class ConstraintSatisfactionBase(metaclass=ABCMeta): 5 | 6 | VariableType = Union[int, float] 7 | 8 | def __init__(self, n:int): 9 | pass 10 | 11 | @abstractmethod 12 | def n_variables(self) -> int: 13 | raise NotImplementedError 14 | 15 | @abstractmethod 16 | def variables(self) -> List[VariableType]: 17 | raise NotImplementedError 18 | 19 | @abstractmethod 20 | def conflicts_of(self, variable_index:int) -> int: 21 | raise NotImplementedError 22 | 23 | @abstractmethod 24 | def has_conflict(self) -> bool: 25 | raise NotImplementedError 26 | 27 | @abstractmethod 28 | def choices_of(self, variable_index:int) -> List[VariableType]: 29 | raise NotImplementedError 30 | 31 | @abstractmethod 32 | def set_variable(self, variable_index:int, new_value:VariableType): 33 | raise NotImplementedError 34 | 35 | @abstractmethod 36 | def reset(self) -> None: 37 | raise NotImplementedError 38 | 39 | @abstractmethod 40 | def show(self) -> None: 41 | raise NotImplementedError -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/interface/population.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | from typing import List, Any 3 | 4 | class PopulationBase(metaclass=ABCMeta): 5 | 6 | ChromosomeType = Any 7 | 8 | @abstractmethod 9 | def population(self) -> List[ChromosomeType]: 10 | raise NotImplementedError 11 | 12 | @abstractmethod 13 | def adaptability(self) -> List[float]: 14 | raise NotImplementedError 15 | 16 | @abstractmethod 17 | def show(self) -> None: 18 | raise NotImplementedError 19 | 20 | @abstractmethod 21 | def cross(self) -> None: 22 | raise NotImplementedError 23 | 24 | @abstractmethod 25 | def mutate(self) -> None: 26 | raise NotImplementedError -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/interface/state_local.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | 4 | class StateLocalBase(metaclass=ABCMeta): 5 | 6 | @abstractmethod 7 | def reset(self) -> None: 8 | raise NotImplementedError 9 | 10 | @abstractmethod 11 | def neighbor_count(self) -> int: 12 | raise NotImplementedError 13 | 14 | @abstractmethod 15 | def neighbor(self, neighbor_index:int) -> "StateLocalBase": 16 | raise NotImplementedError 17 | 18 | @abstractmethod 19 | def show(self) -> None: 20 | raise NotImplementedError 21 | 22 | class StateLocalWrapper(StateLocalBase): 23 | 24 | def __init__(self, state:StateLocalBase): 25 | self._state = state 26 | 27 | def state(self) -> StateLocalBase: return self._state 28 | 29 | def reset(self) -> None: 30 | pass 31 | 32 | def neighbor_count(self) -> int: 33 | return len(self._state.action_space()) 34 | 35 | def neighbor(self, neighbor_index:int) -> "StateLocalBase": 36 | next_state = StateLocalWrapper(self._state.next(self._state.action_space()[neighbor_index])) 37 | return next_state 38 | 39 | def show(self) -> None: 40 | self._state.show() -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('../') -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/problem/queens_constraint.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from interface.constraint_satisfaction import ConstraintSatisfactionBase 4 | from utils.random_variables import RandomVariables 5 | from .queens_move import QueensMoveState 6 | 7 | class QueensConstraintSatisfaction(QueensMoveState, ConstraintSatisfactionBase): 8 | 9 | def __init__(self, n:int): 10 | super().__init__(n) 11 | self._choices = list(range(n)) 12 | 13 | def n_variables(self) -> int: 14 | return self.n_queens 15 | 16 | def variables(self) -> List[int]: 17 | return self.queens 18 | 19 | def conflicts_of(self, variable_index:int) -> int: 20 | row, column = variable_index, self.queens[variable_index] 21 | return (self.column_count[column] 22 | + self.right_left_count[column + row] 23 | + self.left_right_count[column - row + self.n_queens - 1] 24 | - 3 25 | ) 26 | 27 | def has_conflict(self) -> bool: 28 | return any(self.conflicts_of(i) for i in range(self.n_queens)) 29 | 30 | def choices_of(self, variable_index:int) -> List[int]: 31 | return self._choices 32 | 33 | def set_variable(self, variable_index:int, new_variable:float) -> None: 34 | self._add_queen(variable_index, self.queens[variable_index], -1) 35 | self._add_queen(variable_index, new_variable) 36 | 37 | def show(self) -> None: 38 | QueensMoveState.show(self) 39 | 40 | def reset(self) -> None: 41 | QueensMoveState.reset(self) -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/problem/queens_move.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.state_local import StateLocalBase 4 | from utils.random_variables import RandomVariables 5 | class QueensMoveState(StateLocalBase): 6 | 7 | def _add_queen(self, row:int, column:int, n:int=1) -> None: 8 | self.queens[row] = column 9 | self.conflicts += n * (self.column_count[column] 10 | + self.right_left_count[column + row] 11 | + self.left_right_count[column - row + self.n_queens - 1] 12 | ) 13 | self.conflicts -= n * 3 if n < 0 else 0 14 | 15 | self.column_count[column] += n 16 | self.right_left_count[column + row] += n 17 | self.left_right_count[column - row + self.n_queens - 1] += n 18 | 19 | def __init__(self, n:int=0): 20 | self.n_queens = n 21 | self.reset() 22 | 23 | def reset(self) -> None: 24 | self.queens = RandomVariables.uniform_permutation(self.n_queens) 25 | self.column_count = [0] * self.n_queens 26 | self.left_right_count = [0] * (self.n_queens << 1) 27 | self.right_left_count = [0] * (self.n_queens << 1) 28 | self.conflicts = 0 29 | 30 | for i in range(self.n_queens): 31 | self._add_queen(i, self.queens[i]) 32 | 33 | def neighbor_count(self) -> int: 34 | return self.n_queens ** 2 35 | 36 | def neighbor(self, neighbor_index:int) -> "QueensMoveState": 37 | row = neighbor_index // self.n_queens 38 | column = self.queens[row] 39 | to_column = neighbor_index % self.n_queens 40 | 41 | next_state = deepcopy(self) 42 | next_state._add_queen(row, column, -1) 43 | next_state._add_queen(row, to_column) 44 | 45 | return next_state 46 | 47 | def show(self) -> None: 48 | print(f"Queens: {self.queens}") 49 | print(f"Conflicts: {self.conflicts}\n") -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/problem/queens_swap.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.state_local import StateLocalBase 4 | from .queens_move import QueensMoveState 5 | 6 | 7 | class QueensSwapState(StateLocalBase): 8 | 9 | def __init__(self, n:int): 10 | 11 | self.state = QueensMoveState(n) 12 | self.action_space = [(i, j) for i in range(n) for j in range(i+1, n)] 13 | 14 | def neighbor(self, neighbor_index:int) -> "QueensSwapState": 15 | row1, row2 = self.action_space[neighbor_index] 16 | 17 | next_state = deepcopy(self) 18 | next_state.state = next_state.state.neighbor(row1 * self.state.n_queens + self.state.queens[row2]) 19 | next_state.state = next_state.state.neighbor(row2 * self.state.n_queens + self.state.queens[row1]) 20 | 21 | return next_state 22 | 23 | def neighbor_count(self) -> int: 24 | return len(self.action_space) 25 | 26 | def reset(self) -> None: 27 | self.state.reset() 28 | 29 | def show(self) -> None: 30 | self.state.show() -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/queens_conflict_minimize.py: -------------------------------------------------------------------------------- 1 | from math import exp 2 | from time import time 3 | 4 | from problem.queens_constraint import QueensConstraintSatisfaction 5 | from algorithm.conflicts_minimize import ConflictMinimize 6 | from utils.selection import MaxSelection, FirstBetterSelection, RouletteSelection 7 | 8 | if __name__ == '__main__': 9 | t0 = time() 10 | alpha = 10 11 | value_of = lambda value: exp(alpha * value) 12 | 13 | n = 1000 14 | q = QueensConstraintSatisfaction(n) 15 | cm = ConflictMinimize(q) 16 | 17 | fbs = FirstBetterSelection() 18 | rs = RouletteSelection() 19 | ms = MaxSelection() 20 | 21 | cm.search(10, n << 2, ms) 22 | 23 | print(f"Total time: {time() - t0}") 24 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/queens_hill_climb.py: -------------------------------------------------------------------------------- 1 | from math import log, exp 2 | from time import time 3 | 4 | from problem.queens_move import QueensMoveState 5 | from problem.queens_swap import QueensSwapState 6 | from algorithm.hill_climb import HillClimb 7 | from utils.selection import MaxSelection, FirstBetterSelection, RouletteSelection 8 | 9 | 10 | if __name__ == "__main__": 11 | n = 20 12 | log_n = log(n) 13 | queens_move_state_value_estimator = lambda state : exp(-log_n * state.conflicts) 14 | queens_swap_state_value_estimator = lambda state : exp(-log_n * state.state.conflicts) 15 | 16 | t0 = time() 17 | f_selection = FirstBetterSelection() 18 | r_selection = RouletteSelection() 19 | m_selection = MaxSelection() 20 | 21 | # state = QueensMoveState(n) 22 | # hcs = HillClimb(state) 23 | # hcs.search(queens_move_state_value_estimator, 1.0, n<<2, f_selection, 5) 24 | 25 | state = QueensSwapState(n) 26 | hcs = HillClimb(state) 27 | hcs.search(queens_swap_state_value_estimator, 1.0, n<<2, f_selection, 5) 28 | 29 | print(time() - t0) -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/queens_simulated_anneal.py: -------------------------------------------------------------------------------- 1 | from math import exp, log 2 | from time import time 3 | 4 | from problem.queens_move import QueensMoveState 5 | from problem.queens_swap import QueensSwapState 6 | from algorithm.simulated_anneal import SimulatedAnneal 7 | 8 | if __name__ == '__main__': 9 | n = 20 10 | log_n = log(n) 11 | start_temp_log = n / log(n) 12 | max_conflicts = n * (n - 1) >> 1 13 | value_estimator_swap = lambda state: max_conflicts - state.state.conflicts 14 | value_estimator_move = lambda state: max_conflicts - state.conflicts 15 | 16 | temperature_schedule_move = lambda time: exp(start_temp_log - time / (n << 4)) 17 | temperature_schedule_swap = lambda time: exp(start_temp_log - time / n) 18 | 19 | t0 = time() 20 | 21 | q = QueensSwapState(n) 22 | sa = SimulatedAnneal(q) 23 | sa.search(value_estimator_swap, temperature_schedule_swap, n << 2, max_conflicts, 1e-16) 24 | 25 | #q = QueensMoveState(n) 26 | #sa = SimulatedAnneal(q) 27 | #sa.search(value_estimator_move, temperature_schedule_move, n << 2, max_conflicts, 1e-16) 28 | 29 | print(f"Total time: {time() - t0}") -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/06-Local-Search/python/utils/random_variables.py: -------------------------------------------------------------------------------- 1 | import random 2 | from typing import List 3 | 4 | class RandomVariables: 5 | _int_max = 2147483647 6 | seed = 0 7 | random.seed(seed) 8 | 9 | def __init__(self): 10 | raise RuntimeError("RandomVariables can not be instantiated.") 11 | 12 | @classmethod 13 | def uniform_int(cls) -> int: 14 | return random.randint(0, cls._int_max) 15 | 16 | @classmethod 17 | def uniform_real(cls) -> float: 18 | return random.random() 19 | 20 | @classmethod 21 | def uniform_permutation(cls, n:int) -> List[int]: 22 | p = list(range(n)) 23 | 24 | for i in range(n): 25 | j = random.randint(0, n-1) 26 | p[i], p[j] = p[j], p[i] 27 | 28 | return p 29 | -------------------------------------------------------------------------------- /Homework/06-Local-Search/局部搜索实验报告_2110306206_Arthals.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/局部搜索实验报告_2110306206_Arthals.docx -------------------------------------------------------------------------------- /Homework/06-Local-Search/局部搜索实验报告_2110306206_Arthals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/06-Local-Search/局部搜索实验报告_2110306206_Arthals.pdf -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/algorithm/general_game_search.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "../interface/game_state.hpp" 9 | #include "../utils/show_path.hpp" 10 | 11 | // 一般博弈搜索算法,适用于(完美信息)多人扩展式博弈 12 | template 13 | class GeneralGameSearch{ 14 | private: 15 | 16 | GameStateType initial_state; 17 | 18 | // 记录路径 19 | std::unordered_map next_state_of; 20 | 21 | using ActionType = typename GameStateType::ActionBaseType; 22 | 23 | static_assert(std::is_base_of, GameStateType>::value, "GameStateType not derived from GameStateBase."); 24 | 25 | std::vector search(const GameStateType& state){ 26 | 27 | // 到达结束状态,返回所有玩家分数的列表 28 | if (state.done()){ 29 | return state.cumulative_rewards(); 30 | } 31 | 32 | GameStateType next_state; 33 | std::vector best_cumulative_rewards(state.n_players(), -DBL_MAX), cumulative_rewards; 34 | auto action_space = state.action_space(); 35 | 36 | // 逐个尝试所有可行动作 37 | for (auto action : action_space){ 38 | 39 | next_state = state.next(action); 40 | cumulative_rewards = search(next_state); 41 | 42 | if (/*****TODO:如果某个动作会导致当前玩家的分数提高,则切换到该动作******/){ 43 | best_cumulative_rewards = cumulative_rewards; 44 | next_state_of[state] = next_state; 45 | } 46 | } 47 | 48 | return best_cumulative_rewards; 49 | } 50 | 51 | public: 52 | 53 | GeneralGameSearch(const GameStateType& state) : initial_state(state) {} 54 | 55 | void search(){ 56 | 57 | next_state_of.clear(); 58 | auto results = search(initial_state); 59 | for (int i = 0; i < results.size(); ++ i){ 60 | std::cout << "Score " << i << ": " << results[i] << std::endl; 61 | } 62 | show_path(next_state_of, initial_state); 63 | } 64 | }; 65 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/hex_mcts_test.cpp: -------------------------------------------------------------------------------- 1 | #include "algorithm/monte_carlo_tree_search.hpp" 2 | #include "problem/hex.hpp" 3 | 4 | int main(){ 5 | 6 | HexState<11> t; 7 | 8 | while (not t.done()){ 9 | 10 | MonteCarloTreeSearch mcts(t); 11 | 12 | auto action = mcts.select_action(5000, 0.2); 13 | std::cout << action << std::endl; 14 | t = t.next(action); 15 | 16 | t.show(); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | typedef ActionType ActionBaseType; 24 | 25 | // 当前状态下可选动作集大小 26 | virtual int n_actions() const = 0; 27 | 28 | // 当前状态下可选择的动作集 29 | virtual std::vector action_space() const = 0; 30 | 31 | // 转移到当前状态的步骤的花费 32 | virtual double cost() const = 0; 33 | 34 | // 从初始状态出发转移到当前状态的总花费 35 | virtual double cumulative_cost() const = 0; 36 | 37 | // 判断当前状态是否为目标状态 38 | virtual bool success() const = 0; 39 | 40 | // 判断从当前状态是否已经不可能转移到目标状态 41 | virtual bool fail() const = 0; 42 | 43 | // 打印当前状态 44 | virtual void show() const = 0; 45 | 46 | // 从当前状态通过动作生成下一步状态 47 | virtual const StateBase& next(const ActionType&) const = 0; 48 | 49 | // 状态哈希 50 | friend struct std::hash; 51 | 52 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 53 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 54 | return s1.cumulative_cost() == s2.cumulative_cost(); 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/tictactoe_search.cpp: -------------------------------------------------------------------------------- 1 | #include "algorithm/alpha_beta_search.hpp" 2 | #include "algorithm/general_game_search.hpp" 3 | #include "problem/tictactoe.hpp" 4 | 5 | int main(){ 6 | 7 | TicTacToeState t; 8 | 9 | GeneralGameSearch fgs(t); 10 | 11 | fgs.search(); 12 | 13 | AlphaBetaSearch abs(t); 14 | 15 | abs.search(); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/utils/random_variables.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // Singleton Pattern, Provide random variables 8 | class RandomVariables{ 9 | private: 10 | 11 | // 固定随机种子 12 | RandomVariables() = default; 13 | 14 | // 非固定随机种子 15 | // RandomVariables() : random_engine(time(nullptr)) {} 16 | 17 | ~RandomVariables() = default; 18 | 19 | std::default_random_engine random_engine; 20 | std::uniform_real_distribution uniform_dist; 21 | std::uniform_int_distribution uniform_int_dist; 22 | 23 | static RandomVariables rv; 24 | 25 | public: 26 | 27 | // 均匀分布的正整数 28 | static int uniform_int(){ 29 | return rv.uniform_int_dist(rv.random_engine); 30 | } 31 | 32 | // [0,1)均匀分布的实数 33 | static double uniform_real(){ 34 | return rv.uniform_dist(rv.random_engine); 35 | } 36 | 37 | // 等概率分布的{0,1,2,n-1}排列 38 | static std::vector uniform_permutation(int n){ 39 | std::vector permutation(n); 40 | for (int i = 0; i < n; ++ i){ 41 | permutation[i] = i; 42 | } 43 | 44 | for (int i = 0, j; i < n; ++ i){ 45 | j = uniform_int() % (n - i) + i; 46 | std::swap(permutation[i], permutation[j]); 47 | } 48 | 49 | return permutation; 50 | } 51 | }; 52 | 53 | RandomVariables RandomVariables::rv; 54 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/utils/show_path.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | void show_reversed_path(const std::unordered_map& last_state_of, const StateType& state){ 9 | StateType s = state; 10 | std::stack path; 11 | while (last_state_of.find(s) != last_state_of.end()){ 12 | path.push(s); 13 | s = last_state_of.at(s); 14 | } 15 | path.push(s); 16 | 17 | std::cout << "" << std::endl; 18 | while (not path.empty()){ 19 | s = path.top(); 20 | path.pop(); 21 | s.show(); 22 | } 23 | std::cout << "" << std::endl; 24 | } 25 | 26 | template 27 | void show_path(const std::unordered_map& next_state_of, const StateType& state){ 28 | 29 | StateType s; 30 | std::cout << "" << std::endl; 31 | for (s = state; next_state_of.find(s) != next_state_of.end(); s = next_state_of.at(s)){ 32 | s.show(); 33 | } 34 | s.show(); 35 | std::cout << "" << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/c++/utils/union_find_set.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 并查集算法 6 | class UnionFindSet{ 7 | private: 8 | 9 | // 保存元素所属的类别 10 | std::vector color; 11 | 12 | public: 13 | 14 | UnionFindSet(int n){ 15 | for (int i = 0; i < n; ++ i){ 16 | color.push_back(i); 17 | } 18 | } 19 | 20 | UnionFindSet() = default; 21 | 22 | // 查找一个元素所属的类别 23 | int find(int x){ 24 | return x == color[x] ? x : (color[x] = find(color[x])); 25 | } 26 | 27 | // 合并两个元素所属的类别 28 | void join(int x, int y){ 29 | int cx = find(x), cy = find(y); 30 | if (cx != cy){ 31 | color[cx] = cy; 32 | } 33 | } 34 | }; -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/algorithm/general_game_search.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "../interface/game_state.hpp" 9 | #include "../utils/show_path.hpp" 10 | 11 | // 一般博弈搜索算法,适用于(完美信息)多人扩展式博弈 12 | template 13 | class GeneralGameSearch{ 14 | private: 15 | 16 | GameStateType initial_state; 17 | 18 | // 记录路径 19 | std::unordered_map next_state_of; 20 | 21 | using ActionType = typename GameStateType::ActionBaseType; 22 | 23 | static_assert(std::is_base_of, GameStateType>::value, "GameStateType not derived from GameStateBase."); 24 | 25 | std::vector search(const GameStateType& state){ 26 | 27 | // 到达结束状态,返回所有玩家分数的列表 28 | if (state.done()){ 29 | return state.cumulative_rewards(); 30 | } 31 | 32 | GameStateType next_state; 33 | std::vector best_cumulative_rewards(state.n_players(), -DBL_MAX), cumulative_rewards; 34 | auto action_space = state.action_space(); 35 | 36 | // 逐个尝试所有可行动作 37 | for (auto action : action_space){ 38 | 39 | next_state = state.next(action); 40 | cumulative_rewards = search(next_state); 41 | 42 | if (/*****TODO:如果某个动作会导致当前玩家的分数提高,则切换到该动作******/){ 43 | best_cumulative_rewards = cumulative_rewards; 44 | next_state_of[state] = next_state; 45 | } 46 | } 47 | 48 | return best_cumulative_rewards; 49 | } 50 | 51 | public: 52 | 53 | GeneralGameSearch(const GameStateType& state) : initial_state(state) {} 54 | 55 | void search(){ 56 | 57 | next_state_of.clear(); 58 | auto results = search(initial_state); 59 | for (int i = 0; i < results.size(); ++ i){ 60 | std::cout << "Score " << i << ": " << results[i] << std::endl; 61 | } 62 | show_path(next_state_of, initial_state); 63 | } 64 | }; 65 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/hex_mcts_test.cpp: -------------------------------------------------------------------------------- 1 | #include "algorithm/monte_carlo_tree_search.hpp" 2 | #include "problem/hex.hpp" 3 | 4 | int main(){ 5 | 6 | HexState<11> t; 7 | 8 | while (not t.done()){ 9 | 10 | MonteCarloTreeSearch mcts(t); 11 | 12 | auto action = mcts.select_action(5000, 0.2); 13 | std::cout << action << std::endl; 14 | t = t.next(action); 15 | 16 | t.show(); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/interface/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 用于搜索需要让State可哈希,以下提供一个哈希范例 6 | /* 7 | template <> 8 | struct std::hash { 9 | size_t operator()(const StateType& x) const { 10 | return 0; 11 | } 12 | }; 13 | */ 14 | 15 | // 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 16 | template 17 | class StateBase{ 18 | public: 19 | 20 | StateBase() = default; 21 | virtual ~StateBase() = default; 22 | 23 | typedef ActionType ActionBaseType; 24 | 25 | // 当前状态下可选动作集大小 26 | virtual int n_actions() const = 0; 27 | 28 | // 当前状态下可选择的动作集 29 | virtual std::vector action_space() const = 0; 30 | 31 | // 转移到当前状态的步骤的花费 32 | virtual double cost() const = 0; 33 | 34 | // 从初始状态出发转移到当前状态的总花费 35 | virtual double cumulative_cost() const = 0; 36 | 37 | // 判断当前状态是否为目标状态 38 | virtual bool success() const = 0; 39 | 40 | // 判断从当前状态是否已经不可能转移到目标状态 41 | virtual bool fail() const = 0; 42 | 43 | // 打印当前状态 44 | virtual void show() const = 0; 45 | 46 | // 从当前状态通过动作生成下一步状态 47 | virtual const StateBase& next(const ActionType&) const = 0; 48 | 49 | // 状态哈希 50 | friend struct std::hash; 51 | 52 | // operator== 应当重载子类的该运算符,用于搜索中的状态判重 53 | friend bool operator== (const StateBase& s1, const StateBase& s2){ 54 | return s1.cumulative_cost() == s2.cumulative_cost(); 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/tictactoe_search.cpp: -------------------------------------------------------------------------------- 1 | #include "algorithm/alpha_beta_search.hpp" 2 | #include "algorithm/general_game_search.hpp" 3 | #include "problem/tictactoe.hpp" 4 | 5 | int main(){ 6 | 7 | TicTacToeState t; 8 | 9 | GeneralGameSearch fgs(t); 10 | 11 | fgs.search(); 12 | 13 | AlphaBetaSearch abs(t); 14 | 15 | abs.search(); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/utils/random_variables.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // Singleton Pattern, Provide random variables 8 | class RandomVariables{ 9 | private: 10 | 11 | // 固定随机种子 12 | RandomVariables() = default; 13 | 14 | // 非固定随机种子 15 | // RandomVariables() : random_engine(time(nullptr)) {} 16 | 17 | ~RandomVariables() = default; 18 | 19 | std::default_random_engine random_engine; 20 | std::uniform_real_distribution uniform_dist; 21 | std::uniform_int_distribution uniform_int_dist; 22 | 23 | static RandomVariables rv; 24 | 25 | public: 26 | 27 | // 均匀分布的正整数 28 | static int uniform_int(){ 29 | return rv.uniform_int_dist(rv.random_engine); 30 | } 31 | 32 | // [0,1)均匀分布的实数 33 | static double uniform_real(){ 34 | return rv.uniform_dist(rv.random_engine); 35 | } 36 | 37 | // 等概率分布的{0,1,2,n-1}排列 38 | static std::vector uniform_permutation(int n){ 39 | std::vector permutation(n); 40 | for (int i = 0; i < n; ++ i){ 41 | permutation[i] = i; 42 | } 43 | 44 | for (int i = 0, j; i < n; ++ i){ 45 | j = uniform_int() % (n - i) + i; 46 | std::swap(permutation[i], permutation[j]); 47 | } 48 | 49 | return permutation; 50 | } 51 | }; 52 | 53 | RandomVariables RandomVariables::rv; 54 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/utils/show_path.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | void show_reversed_path(const std::unordered_map& last_state_of, const StateType& state){ 9 | StateType s = state; 10 | std::stack path; 11 | while (last_state_of.find(s) != last_state_of.end()){ 12 | path.push(s); 13 | s = last_state_of.at(s); 14 | } 15 | path.push(s); 16 | 17 | std::cout << "" << std::endl; 18 | while (not path.empty()){ 19 | s = path.top(); 20 | path.pop(); 21 | s.show(); 22 | } 23 | std::cout << "" << std::endl; 24 | } 25 | 26 | template 27 | void show_path(const std::unordered_map& next_state_of, const StateType& state){ 28 | 29 | StateType s; 30 | std::cout << "" << std::endl; 31 | for (s = state; next_state_of.find(s) != next_state_of.end(); s = next_state_of.at(s)){ 32 | s.show(); 33 | } 34 | s.show(); 35 | std::cout << "" << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/c++/utils/union_find_set.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // 并查集算法 6 | class UnionFindSet{ 7 | private: 8 | 9 | // 保存元素所属的类别 10 | std::vector color; 11 | 12 | public: 13 | 14 | UnionFindSet(int n){ 15 | for (int i = 0; i < n; ++ i){ 16 | color.push_back(i); 17 | } 18 | } 19 | 20 | UnionFindSet() = default; 21 | 22 | // 查找一个元素所属的类别 23 | int find(int x){ 24 | return x == color[x] ? x : (color[x] = find(color[x])); 25 | } 26 | 27 | // 合并两个元素所属的类别 28 | void join(int x, int y){ 29 | int cx = find(x), cy = find(y); 30 | if (cx != cy){ 31 | color[cx] = cy; 32 | } 33 | } 34 | }; -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/__main__.py: -------------------------------------------------------------------------------- 1 | import json 2 | from algorithm.monte_carlo_tree_search import MonteCarloTreeSearch 3 | from problem.hex import HexState 4 | 5 | N = 11 6 | 7 | state = HexState(11) 8 | 9 | # 解析读入的JSON 10 | full_input = json.loads(input()) 11 | 12 | turn_id = len(full_input["responses"]) 13 | for i in range(turn_id): 14 | x = int(full_input["requests"][i]["x"]) 15 | y = int(full_input["requests"][i]["y"]) 16 | if x >= 0 and y >= 0: 17 | state = state.next(x * N + y) 18 | x = int(full_input["responses"][i]["x"]) 19 | y = int(full_input["responses"][i]["y"]) 20 | state = state.next(x * N + y) 21 | 22 | x = int(full_input["requests"][turn_id]["x"]) 23 | y = int(full_input["requests"][turn_id]["y"]) 24 | 25 | if x >= 0 and y >= 0: 26 | state = state.next(x * N + y) 27 | forced_flag = False 28 | elif "forced_x" in full_input["requests"][0]: 29 | forced_flag = True 30 | else: 31 | forced_flag = False 32 | 33 | mcts = MonteCarloTreeSearch(state) 34 | 35 | if not forced_flag: 36 | action = mcts.select_action(150, 0.2) 37 | else: 38 | action = 1 * 11 + 2 39 | 40 | my_action = {"x": int(action)//11, "y": int(action)%11} 41 | print(json.dumps({ 42 | "response": my_action 43 | })) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("../") -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/algorithm/alpha_beta_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.game_state import GameStateBase 4 | from utils.show_path import show_path 5 | 6 | class AlphaBetaSearch: 7 | 8 | def __init__(self, state:GameStateBase): 9 | self._initial_state = deepcopy(state) 10 | 11 | def _search(self, state:GameStateBase, alpha:float, beta:float) -> float: 12 | if state.done(): 13 | return state.cumulative_rewards()[0] 14 | 15 | action_space = state.action_space() 16 | for action in action_space: 17 | next_state = state.next(action) 18 | new_value = self._search(next_state, alpha, beta) 19 | 20 | if state.active_player() == 0 and new_value > alpha: 21 | # TODO:更新alpha或者beta的值——1行 22 | self._next_state_of[state] = next_state 23 | elif state.active_player() == 1 and new_value < beta: 24 | # TODO:更新alpha或者beta的值——1行 25 | self._next_state_of[state] = next_state 26 | 27 | # TODO:剪枝 28 | 29 | return alpha if state.active_player() == 0 else beta 30 | 31 | def search(self) -> None: 32 | self._next_state_of = dict() 33 | result = self._search(self._initial_state, -float("inf"), float("inf")) 34 | print(f"Score 0: {result}") 35 | show_path(self._next_state_of, self._initial_state) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/algorithm/general_game_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from typing import List 3 | 4 | from interface.game_state import GameStateBase 5 | from utils.show_path import show_path 6 | 7 | class GeneralGameSearch: 8 | 9 | def __init__(self, state:GameStateBase): 10 | self._initial_state = deepcopy(state) 11 | 12 | def _search(self, state:GameStateBase) -> List[float]: 13 | 14 | if state.done(): 15 | return state.cumulative_rewards() 16 | 17 | best_cumulative_rewards = (-float("inf"),) * state.n_players() 18 | action_space = state.action_space() 19 | 20 | for action in action_space: 21 | next_state = state.next(action) 22 | cumulative_rewards = self._search(next_state) 23 | if # TODO:如果某个动作会导致当前玩家的分数提高,则切换到该动作 24 | best_cumulative_rewards = cumulative_rewards 25 | self._next_state_of[state] = next_state 26 | return best_cumulative_rewards 27 | 28 | def search(self) -> None: 29 | self._next_state_of = dict() 30 | result = self._search(self._initial_state) 31 | print(f"Scores: {result}") 32 | show_path(self._next_state_of, self._initial_state) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/hex_mcts_test.py: -------------------------------------------------------------------------------- 1 | from algorithm.monte_carlo_tree_search import MonteCarloTreeSearch 2 | from problem.hex import HexState 3 | 4 | if __name__ == '__main__': 5 | t = HexState(11) 6 | while not t.done(): 7 | mcts = MonteCarloTreeSearch(t) 8 | action = mcts.select_action(150, 0.2) 9 | print(action) 10 | t = t.next(action) 11 | t.show() 12 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/handout/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("../") -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/tictactoe_search.py: -------------------------------------------------------------------------------- 1 | from algorithm.alpha_beta_search import AlphaBetaSearch 2 | from algorithm.general_game_search import GeneralGameSearch 3 | from problem.tictactoe import TicTacToeState 4 | 5 | if __name__ == "__main__": 6 | 7 | t = TicTacToeState() 8 | fgs = GeneralGameSearch(t) 9 | 10 | fgs.search() 11 | 12 | abs = AlphaBetaSearch(t) 13 | 14 | abs.search() -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/handout/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/utils/random_variables.py: -------------------------------------------------------------------------------- 1 | import random 2 | from typing import List 3 | 4 | class RandomVariables: 5 | _int_max = 2147483647 6 | seed = 0 7 | random.seed(seed) 8 | 9 | def __init__(self): 10 | raise RuntimeError("RandomVariables can not be instantiated.") 11 | 12 | @classmethod 13 | def uniform_int(cls) -> int: 14 | return random.randint(0, cls._int_max) 15 | 16 | @classmethod 17 | def uniform_real(cls) -> float: 18 | return random.random() 19 | 20 | @classmethod 21 | def uniform_permutation(cls, n:int) -> List[int]: 22 | p = list(range(n)) 23 | 24 | for i in range(n): 25 | j = random.randint(0, n-1) 26 | p[i], p[j] = p[j], p[i] 27 | 28 | return p -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/utils/search_tree.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class SearchTreeNode: 4 | 5 | def __init__(self, index:int): 6 | self.index = index 7 | self.parent = None 8 | self.n_children = 0 9 | self.children = list() 10 | 11 | def add_child(self, child:"SearchTreeNode") -> None: 12 | self.n_children += 1 13 | self.children.append(child) 14 | child.parent = self 15 | 16 | def child(self, index:int) -> "SearchTreeNode": 17 | return self.children[index] 18 | 19 | def __eq__(self, other:"SearchTreeNode") -> bool: 20 | return self.index == other.index 21 | 22 | def __hash__(self) -> int: 23 | return self.index 24 | 25 | class SearchTree: 26 | 27 | def __init__(self): 28 | self.root = SearchTreeNode(0) 29 | self._unique_identifier = 1 30 | self.n_nodes = 1 31 | self.node_of = {0:self.root} 32 | 33 | def create_node(self) -> SearchTreeNode: 34 | new_node = SearchTreeNode(self._unique_identifier) 35 | self.node_of[self._unique_identifier] = new_node 36 | self._unique_identifier += 1 37 | return new_node 38 | 39 | def add_as_child(self, parent:SearchTreeNode, child:SearchTreeNode) -> None: 40 | parent.add_child(child) 41 | self.n_nodes += 1 -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/utils/show_path.py: -------------------------------------------------------------------------------- 1 | from typing import Dict 2 | from queue import LifoQueue, Queue 3 | 4 | from interface.state import StateBase 5 | 6 | def show_reversed_path(last_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 7 | 8 | s = state 9 | path = LifoQueue() 10 | 11 | while s in last_state_of.keys(): 12 | path.put(s) 13 | s = last_state_of[s] 14 | path.put(s) 15 | 16 | print("") 17 | while not path.empty(): 18 | s = path.get() 19 | s.show() 20 | print("") 21 | 22 | def show_path(next_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 23 | print("") 24 | s = state 25 | while s in next_state_of.keys(): 26 | s.show() 27 | s = next_state_of[s] 28 | s.show() 29 | print("") 30 | 31 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/python/utils/union_find_set.py: -------------------------------------------------------------------------------- 1 | class UnionFindSet: 2 | 3 | def __init__(self, n:int): 4 | self.n = n 5 | self.color = list(range(n)) 6 | 7 | def find(self, x:int) -> int: 8 | if x == self.color[x]: 9 | return x 10 | self.color[x] = self.find(self.color[x]) 11 | return self.color[x] 12 | 13 | def join(self, x:int, y:int) -> None: 14 | cx, cy = self.find(x), self.find(y) 15 | if cx != cy: 16 | self.color[cx] = cy -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/作业要求说明.txt: -------------------------------------------------------------------------------- 1 | 作业说明: 2 | 3 | 4 | 1. 样例代码: 5 | 6 | C++的代码目录下包含5个文件夹:algorithm, interface, problem, utils, jsoncpp 7 | 【interface】 8 | game_state.hpp是多智能体游戏状态接口,实现此接口可使用搜索算法求解游戏 9 | 【algorithm】 10 | general_game_search.hpp实现了一般博弈的搜索求解,求解双人零和博弈时,此算法与MinMax相同(供参考学习) 11 | alpha_beta_search.hpp实现了alpha-beta剪枝算法,用于求解双人零和博弈(供参考学习) 12 | 13 | monte_carlo_tree_search.hpp实现了蒙特卡洛树搜索算法(UCT),用于求解一般的博弈(作业涉及此算法) 14 | 【problem】 15 | tictactoe.hpp实现了3*3井字棋和4*4井字棋的环境 16 | hex.hpp实现了棋盘大小为N*N的六边形棋的环境(N可以自行选择) 17 | 【utils】 18 | search_tree.hpp提供了建立搜索树的工具类 19 | selection.hpp提供了选择算法,MCTS实现中用到了最大选择MaxSelection 20 | random_variables.hpp提供了随机变量工具类 21 | union_find_set.hpp提供了并查集工具类(用于判断Hex同色棋块的连通性) 22 | 23 | tictactoe_search.cpp实现了alpha-beta剪枝求解3*3井字棋(4*4可求解但用时较长),输出双方最优决策序列。(供参考学习) 24 | hex_mcts_test.cpp实现了蒙特卡洛树搜索算法模拟11*11的六边形棋对局(供参考学习) 25 | 26 | 27 | 2. 任务描述 28 | 29 | 【任务0:补全代码】 30 | 补全algorithm中的算法代码,填写实验报告(python C++任选其一) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/handout/实验报告3_学号_姓名.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/handout/实验报告3_学号_姓名.docx -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/__main__.py: -------------------------------------------------------------------------------- 1 | import json 2 | from algorithm.monte_carlo_tree_search import MonteCarloTreeSearch 3 | from problem.hex import HexState 4 | 5 | N = 11 6 | 7 | state = HexState(11) 8 | 9 | # 解析读入的JSON 10 | full_input = json.loads(input()) 11 | 12 | turn_id = len(full_input["responses"]) 13 | for i in range(turn_id): 14 | x = int(full_input["requests"][i]["x"]) 15 | y = int(full_input["requests"][i]["y"]) 16 | if x >= 0 and y >= 0: 17 | state = state.next(x * N + y) 18 | x = int(full_input["responses"][i]["x"]) 19 | y = int(full_input["responses"][i]["y"]) 20 | state = state.next(x * N + y) 21 | 22 | x = int(full_input["requests"][turn_id]["x"]) 23 | y = int(full_input["requests"][turn_id]["y"]) 24 | 25 | if x >= 0 and y >= 0: 26 | state = state.next(x * N + y) 27 | forced_flag = False 28 | elif "forced_x" in full_input["requests"][0]: 29 | forced_flag = True 30 | else: 31 | forced_flag = False 32 | 33 | mcts = MonteCarloTreeSearch(state) 34 | 35 | if not forced_flag: 36 | action = mcts.select_action(150, 0.2) 37 | else: 38 | action = 1 * 11 + 2 39 | 40 | my_action = {"x": int(action)//11, "y": int(action)%11} 41 | print(json.dumps({ 42 | "response": my_action 43 | })) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("../") -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/algorithm/alpha_beta_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | 3 | from interface.game_state import GameStateBase 4 | from utils.show_path import show_path 5 | 6 | class AlphaBetaSearch: 7 | 8 | def __init__(self, state:GameStateBase): 9 | self._initial_state = deepcopy(state) 10 | 11 | def _search(self, state:GameStateBase, alpha:float, beta:float) -> float: 12 | if state.done(): 13 | return state.cumulative_rewards()[0] 14 | 15 | action_space = state.action_space() 16 | for action in action_space: 17 | next_state = state.next(action) 18 | new_value = self._search(next_state, alpha, beta) 19 | 20 | if state.active_player() == 0 and new_value > alpha: 21 | # TODO:更新alpha或者beta的值——1行 22 | alpha = new_value 23 | self._next_state_of[state] = next_state 24 | elif state.active_player() == 1 and new_value < beta: 25 | # TODO:更新alpha或者beta的值——1行 26 | beta = new_value 27 | self._next_state_of[state] = next_state 28 | 29 | # TODO:剪枝 30 | if alpha >= beta: 31 | break 32 | 33 | return alpha if state.active_player() == 0 else beta 34 | 35 | def search(self) -> None: 36 | self._next_state_of = dict() 37 | result = self._search(self._initial_state, -float("inf"), float("inf")) 38 | print(f"Score 0: {result}") 39 | show_path(self._next_state_of, self._initial_state) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/algorithm/general_game_search.py: -------------------------------------------------------------------------------- 1 | from copy import deepcopy 2 | from typing import List 3 | 4 | from interface.game_state import GameStateBase 5 | from utils.show_path import show_path 6 | 7 | class GeneralGameSearch: 8 | 9 | def __init__(self, state:GameStateBase): 10 | self._initial_state = deepcopy(state) 11 | 12 | def _search(self, state:GameStateBase) -> List[float]: 13 | 14 | if state.done(): 15 | return state.cumulative_rewards() 16 | 17 | best_cumulative_rewards = (-float("inf"),) * state.n_players() 18 | action_space = state.action_space() 19 | 20 | for action in action_space: 21 | next_state = state.next(action) 22 | cumulative_rewards = self._search(next_state) 23 | # TODO:如果某个动作会导致当前玩家的分数提高,则切换到该动作 24 | if cumulative_rewards[state.active_player()] > best_cumulative_rewards[state.active_player()]: 25 | best_cumulative_rewards = cumulative_rewards 26 | self._next_state_of[state] = next_state 27 | return best_cumulative_rewards 28 | 29 | def search(self) -> None: 30 | self._next_state_of = dict() 31 | result = self._search(self._initial_state) 32 | print(f"Scores: {result}") 33 | show_path(self._next_state_of, self._initial_state) -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/hex_mcts_test.py: -------------------------------------------------------------------------------- 1 | from algorithm.monte_carlo_tree_search import MonteCarloTreeSearch 2 | from problem.hex import HexState 3 | 4 | if __name__ == '__main__': 5 | t = HexState(11) 6 | while not t.done(): 7 | mcts = MonteCarloTreeSearch(t) 8 | action = mcts.select_action(150, 0.2) 9 | print(action) 10 | t = t.next(action) 11 | t.show() 12 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/interface/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/python/interface/__init__.py -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/interface/state.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | # 状态接口,实现此接口可用于各种全局搜索算法BFS/DFS/heuristic等 4 | class StateBase(metaclass=ABCMeta): 5 | 6 | # 当前状态下可选择的动作集 7 | @abstractmethod 8 | def action_space(self) -> list: 9 | raise NotImplementedError 10 | 11 | # 转移到当前状态的步骤的花费 12 | @abstractmethod 13 | def cost(self) -> float: 14 | raise NotImplementedError 15 | 16 | # 从初始状态出发转移到当前状态的总花费 17 | @abstractmethod 18 | def cumulative_cost(self) -> float: 19 | raise NotImplementedError 20 | 21 | # 判断当前状态是否为目标状态 22 | @abstractmethod 23 | def success(self) -> bool: 24 | raise NotImplementedError 25 | 26 | # 判断从当前状态是否已经不可能转移到目标状态 27 | @abstractmethod 28 | def fail(self) -> bool: 29 | raise NotImplementedError 30 | 31 | # 打印当前状态 32 | @abstractmethod 33 | def show(self) -> None: 34 | raise NotImplementedError 35 | 36 | # 从当前状态通过动作生成下一步状态 37 | @abstractmethod 38 | def next(self, action) -> "StateBase": 39 | raise NotImplementedError 40 | 41 | # ==运算符重载,用于判重 42 | @abstractmethod 43 | def __eq__(self, state:"StateBase") -> bool: 44 | raise NotImplementedError 45 | 46 | # <运算符重载,用于优先队列比较 47 | @abstractmethod 48 | def __lt__(self) -> bool: 49 | raise NotImplementedError 50 | 51 | # 用于搜索需要State可哈希 52 | @abstractmethod 53 | def __hash__(self) -> int: 54 | raise NotImplementedError 55 | 56 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/problem/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("../") -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/tictactoe_search.py: -------------------------------------------------------------------------------- 1 | from algorithm.alpha_beta_search import AlphaBetaSearch 2 | from algorithm.general_game_search import GeneralGameSearch 3 | from problem.tictactoe import TicTacToeState 4 | 5 | if __name__ == "__main__": 6 | 7 | t = TicTacToeState() 8 | fgs = GeneralGameSearch(t) 9 | 10 | fgs.search() 11 | 12 | abs = AlphaBetaSearch(t) 13 | 14 | abs.search() -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/python/utils/__init__.py -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/utils/random_variables.py: -------------------------------------------------------------------------------- 1 | import random 2 | from typing import List 3 | 4 | class RandomVariables: 5 | _int_max = 2147483647 6 | seed = 0 7 | random.seed(seed) 8 | 9 | def __init__(self): 10 | raise RuntimeError("RandomVariables can not be instantiated.") 11 | 12 | @classmethod 13 | def uniform_int(cls) -> int: 14 | return random.randint(0, cls._int_max) 15 | 16 | @classmethod 17 | def uniform_real(cls) -> float: 18 | return random.random() 19 | 20 | @classmethod 21 | def uniform_permutation(cls, n:int) -> List[int]: 22 | p = list(range(n)) 23 | 24 | for i in range(n): 25 | j = random.randint(0, n-1) 26 | p[i], p[j] = p[j], p[i] 27 | 28 | return p -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/utils/search_tree.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class SearchTreeNode: 4 | 5 | def __init__(self, index:int): 6 | self.index = index 7 | self.parent = None 8 | self.n_children = 0 9 | self.children = list() 10 | 11 | def add_child(self, child:"SearchTreeNode") -> None: 12 | self.n_children += 1 13 | self.children.append(child) 14 | child.parent = self 15 | 16 | def child(self, index:int) -> "SearchTreeNode": 17 | return self.children[index] 18 | 19 | def __eq__(self, other:"SearchTreeNode") -> bool: 20 | return self.index == other.index 21 | 22 | def __hash__(self) -> int: 23 | return self.index 24 | 25 | class SearchTree: 26 | 27 | def __init__(self): 28 | self.root = SearchTreeNode(0) 29 | self._unique_identifier = 1 30 | self.n_nodes = 1 31 | self.node_of = {0:self.root} 32 | 33 | def create_node(self) -> SearchTreeNode: 34 | new_node = SearchTreeNode(self._unique_identifier) 35 | self.node_of[self._unique_identifier] = new_node 36 | self._unique_identifier += 1 37 | return new_node 38 | 39 | def add_as_child(self, parent:SearchTreeNode, child:SearchTreeNode) -> None: 40 | parent.add_child(child) 41 | self.n_nodes += 1 -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/utils/show_path.py: -------------------------------------------------------------------------------- 1 | from typing import Dict 2 | from queue import LifoQueue, Queue 3 | 4 | from interface.state import StateBase 5 | 6 | def show_reversed_path(last_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 7 | 8 | s = state 9 | path = LifoQueue() 10 | 11 | while s in last_state_of.keys(): 12 | path.put(s) 13 | s = last_state_of[s] 14 | path.put(s) 15 | 16 | print("") 17 | while not path.empty(): 18 | s = path.get() 19 | s.show() 20 | print("") 21 | 22 | def show_path(next_state_of:Dict[StateBase, StateBase], state:StateBase) -> None: 23 | print("") 24 | s = state 25 | while s in next_state_of.keys(): 26 | s.show() 27 | s = next_state_of[s] 28 | s.show() 29 | print("") 30 | 31 | -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/python/utils/union_find_set.py: -------------------------------------------------------------------------------- 1 | class UnionFindSet: 2 | 3 | def __init__(self, n:int): 4 | self.n = n 5 | self.color = list(range(n)) 6 | 7 | def find(self, x:int) -> int: 8 | if x == self.color[x]: 9 | return x 10 | self.color[x] = self.find(self.color[x]) 11 | return self.color[x] 12 | 13 | def join(self, x:int, y:int) -> None: 14 | cx, cy = self.find(x), self.find(y) 15 | if cx != cy: 16 | self.color[cx] = cy -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/对抗搜索实验报告_2110306206_Arthals.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/对抗搜索实验报告_2110306206_Arthals.docx -------------------------------------------------------------------------------- /Homework/07-Adversial-Search/对抗搜索实验报告_2110306206_Arthals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/07-Adversial-Search/对抗搜索实验报告_2110306206_Arthals.pdf -------------------------------------------------------------------------------- /Homework/08-Reinforcement-Learning/handout/动态规划实验报告.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/08-Reinforcement-Learning/handout/动态规划实验报告.docx -------------------------------------------------------------------------------- /Homework/08-Reinforcement-Learning/handout/第6课-基于动态规划的路径规划-实验指导书(挖空).docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/08-Reinforcement-Learning/handout/第6课-基于动态规划的路径规划-实验指导书(挖空).docx -------------------------------------------------------------------------------- /Homework/08-Reinforcement-Learning/动态规划实验报告_2110306206_Arthals.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/08-Reinforcement-Learning/动态规划实验报告_2110306206_Arthals.docx -------------------------------------------------------------------------------- /Homework/08-Reinforcement-Learning/动态规划实验报告_2110306206_Arthals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/08-Reinforcement-Learning/动态规划实验报告_2110306206_Arthals.pdf -------------------------------------------------------------------------------- /Homework/08-Reinforcement-Learning/第6课-基于动态规划的路径规划-实验指导书(挖空).docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/08-Reinforcement-Learning/第6课-基于动态规划的路径规划-实验指导书(挖空).docx -------------------------------------------------------------------------------- /Homework/09-House-Price-Prediction/09-House-Price-Prediction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/09-House-Price-Prediction/09-House-Price-Prediction.pdf -------------------------------------------------------------------------------- /Homework/09-House-Price-Prediction/handout/作业说明.txt: -------------------------------------------------------------------------------- 1 | 作业说明: 2 | 3 | 0. 截止日期: 4 | 2023年6月16日23:59 5 | 6 | 1. 任务简述: 7 | 本次作业要求利用一系列**可能**与房价相关的因素(特征)训练出一个房价预测模型 8 | 9 | 2. 文件结构: 10 | data文件夹: 存放有: 11 | - train.csv: 训练集的特征以及对应的目标预测值(即房价) 12 | - test.csv: 测试集的特征 13 | - test_groundtruth.csv: 测试集对应的真实房价(即Y) 14 | - 学号_姓名.csv: 预测结果示例,输出预测的结果需要和此文件相同 15 | 注意:本次作业给出了测试集的Y,主要是帮助更好地大家分析预测模型,测试集的数据不应该被用于训练!!! 16 | 本次作业的评分主要依据是提交的代码和实验报告,考察依据是对数据的分析以及利用分析的结果尝试构建一个更好的预测模型, 17 | 预测模型的效果只作为一个指标上的参考 18 | 数据字典文件夹:存放特征说明文件 19 | baselines.ipynb:基线预测模型,可以直接运行,可在此基础上搭建自己的预测模型 20 | 21 | 3. xxx.ipynb文件如何打开? 22 | xxx.ipynb是jupyter notebook 文件,可在浏览器中交互式地执行代码,同时可以将代码、文字完美结合起来, 23 | 在数据科学领域相关(机器学习、数据分析等)被广泛使用。 24 | 可以通过以下命令事先安装好jupyter notebook: pip install jupyter 25 | 安装好jupyter后通过终端进入xxx.ipynb所在目录,执行 jupyter notebook 即可在浏览器打开xxx.ipynb文件 26 | 27 | 4. 预测模型评估指标 28 | 所搭建的预测模型将以MAPE(mean absolute percentage error)作为评估指标,其计算公式可以参考如下链接: 29 | https://en.wikipedia.org/wiki/Mean_absolute_percentage_error 30 | 可以通过sklearn库中的api计算,具体操作如下: 31 | from sklearn.metrics import mean_absolute_percentage_error 32 | mean_absolute_percentage_error(test_y, pred_y) 33 | 34 | 5. 提交与评分 35 | 本次作业通过教学网提交,需要包括以下文件: 36 | 1. 实验报告:包括但不限于背景介绍,数据探索性分析,特征工程,预测模型的建立,结果的分析与总结等。 37 | 2. 源代码:可以是原始py文件或者jupyter notebook文件,提交的版本需要是干净,易懂,可运行,能输出预测结果的。 38 | 3. 预测结果:输出内容需要和`学号_姓名.csv`相同,需要文件命名规则,例如张三,学号20232023,则预测结果文件命名需为`20232023_张三.csv` 39 | 本次作业实验报告占比8分。预测模型指标占比2分,其中计算规则如下: 40 | - MAPE达到 0.3100:2分 41 | - MAPE达到 0.3300:1.5分 42 | - MAPE达到 0.3500:1分 43 | 本次作业所提供基线模型的MAPE为0.3680。若MAPE未达到0.3500,则预测模型指标的部分得分将按照 (0.3500/your MAPE)*1 计算 44 | 45 | 6. 补充材料 46 | 【matplotlib】 47 | https://matplotlib.org/stable/index.html 48 | 【seaborn】 49 | https://seaborn.pydata.org/ 50 | 【sklearn】 51 | https://scikit-learn.org/ 52 | 【pandas】 53 | https://pandas.pydata.org/docs/ -------------------------------------------------------------------------------- /Homework/09-House-Price-Prediction/handout/数据字典/Building Classifications 数据字典.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/09-House-Price-Prediction/handout/数据字典/Building Classifications 数据字典.pdf -------------------------------------------------------------------------------- /Homework/09-House-Price-Prediction/handout/数据字典/数据字典.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/09-House-Price-Prediction/handout/数据字典/数据字典.pdf -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/128_vs_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/128_vs_256.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/accuracy.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/botzone_group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/botzone_group.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/botzone_rank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/botzone_rank.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/botzone_rank_score.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/botzone_rank_score.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/complex_feature.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/complex_feature.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/loss.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.assets/vit_vs_resnet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.assets/vit_vs_resnet.png -------------------------------------------------------------------------------- /Homework/10-Mahjong/10-Mahjong.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/10-Mahjong.pdf -------------------------------------------------------------------------------- /Homework/10-Mahjong/agent.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- encoding: utf-8 -*- 3 | # @Author : Arthals 4 | # @File : agent.py 5 | # @Time : 2024/06/30 19:05:31 6 | # @Contact : zhuozhiyongde@126.com 7 | # @Software: Visual Studio Code 8 | 9 | 10 | """ 11 | agent.py: 接口类,描述了与Botzone交互的智能体的行为。在 FeatureAgent 中进一步实现。 12 | 也即,按顺序接收一个玩家在对局中观察到的事件,并在每个决策点整理出状态特征;将网络输出的动作转为事件。 13 | """ 14 | 15 | 16 | class MahjongGBAgent: 17 | def __init__(self, seatWind): 18 | pass 19 | 20 | """ 21 | Wind 0..3 22 | Deal XX XX ... 23 | Player N Draw 24 | Player N Gang 25 | Player N(me) Play XX 26 | Player N(me) BuGang XX 27 | Player N(not me) Peng 28 | Player N(not me) Chi XX 29 | Player N(me) UnPeng 30 | Player N(me) UnChi XX 31 | 32 | Player N Hu 33 | Huang 34 | Player N Invalid 35 | Draw XX 36 | Player N(not me) Play XX 37 | Player N(not me) BuGang XX 38 | Player N(me) Peng 39 | Player N(me) Chi XX 40 | """ 41 | 42 | def request2obs(self, request): 43 | pass 44 | 45 | """ 46 | Hu 47 | Play XX 48 | (An)Gang XX 49 | BuGang XX 50 | Gang 51 | Peng 52 | Chi XX 53 | Pass 54 | """ 55 | 56 | def action2response(self, action): 57 | pass 58 | -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/Mahjong.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/handout/Mahjong.zip -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/code/agent.py: -------------------------------------------------------------------------------- 1 | """ 2 | 接口类,描述了与Botzone交互的智能体的行为:按顺序接收一个玩家在对局中观察到的事件,并在每个决策点整理出状态特征;将网络输出的动作转为事件。 3 | 4 | 无需修改。 5 | """ 6 | 7 | class MahjongGBAgent: 8 | 9 | def __init__(self, seatWind): 10 | pass 11 | 12 | ''' 13 | Wind 0..3 14 | Deal XX XX ... 15 | Player N Draw 16 | Player N Gang 17 | Player N(me) Play XX 18 | Player N(me) BuGang XX 19 | Player N(not me) Peng 20 | Player N(not me) Chi XX 21 | Player N(me) UnPeng 22 | Player N(me) UnChi XX 23 | 24 | Player N Hu 25 | Huang 26 | Player N Invalid 27 | Draw XX 28 | Player N(not me) Play XX 29 | Player N(not me) BuGang XX 30 | Player N(me) Peng 31 | Player N(me) Chi XX 32 | ''' 33 | def request2obs(self, request): 34 | pass 35 | 36 | ''' 37 | Hu 38 | Play XX 39 | (An)Gang XX 40 | BuGang XX 41 | Gang 42 | Peng 43 | Chi XX 44 | Pass 45 | ''' 46 | def action2response(self, action): 47 | pass -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/code/dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | 将预处理好的数据集一次性加载到内存中,按全局索引获取状态动作对。 3 | 4 | 提示:可以修改这个类,从而支持: 5 | 1. 延迟加载数据(特征复杂后数据量特别大,没法装进内存); 6 | 2. 数据增强。 7 | """ 8 | 9 | from torch.utils.data import Dataset 10 | import numpy as np 11 | from bisect import bisect_right 12 | 13 | class MahjongGBDataset(Dataset): 14 | 15 | def __init__(self, begin = 0, end = 1, augment = False): 16 | import json 17 | with open('data/count.json') as f: 18 | self.match_samples = json.load(f) 19 | self.total_matches = len(self.match_samples) 20 | self.total_samples = sum(self.match_samples) 21 | self.begin = int(begin * self.total_matches) 22 | self.end = int(end * self.total_matches) 23 | self.match_samples = self.match_samples[self.begin : self.end] 24 | self.matches = len(self.match_samples) 25 | self.samples = sum(self.match_samples) 26 | self.augment = augment 27 | t = 0 28 | for i in range(self.matches): 29 | a = self.match_samples[i] 30 | self.match_samples[i] = t 31 | t += a 32 | self.cache = {'obs': [], 'mask': [], 'act': []} 33 | for i in range(self.matches): 34 | if i % 128 == 0: print('loading', i) 35 | d = np.load('data/%d.npz' % (i + self.begin)) 36 | for k in d: 37 | self.cache[k].append(d[k]) 38 | 39 | def __len__(self): 40 | return self.samples 41 | 42 | def __getitem__(self, index): 43 | match_id = bisect_right(self.match_samples, index, 0, self.matches) - 1 44 | sample_id = index - self.match_samples[match_id] 45 | return self.cache['obs'][match_id][sample_id], self.cache['mask'][match_id][sample_id], self.cache['act'][match_id][sample_id] -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/code/model.py: -------------------------------------------------------------------------------- 1 | """ 2 | 示例用神经网络,包括三层卷积和两层全连接。 3 | 4 | 提示:需要修改这个类,从而实现: 5 | 1. 表达能力更强的神经网络。 6 | """ 7 | import torch 8 | from torch import nn 9 | 10 | class CNNModel(nn.Module): 11 | 12 | def __init__(self): 13 | nn.Module.__init__(self) 14 | self._tower = nn.Sequential( 15 | nn.Conv2d(6, 64, 3, 1, 1, bias = False), 16 | nn.ReLU(True), 17 | nn.Conv2d(64, 64, 3, 1, 1, bias = False), 18 | nn.ReLU(True), 19 | nn.Conv2d(64, 64, 3, 1, 1, bias = False), 20 | nn.ReLU(True), 21 | nn.Flatten(), 22 | nn.Linear(64 * 4 * 9, 256), 23 | nn.ReLU(), 24 | nn.Linear(256, 235) 25 | ) 26 | 27 | for m in self.modules(): 28 | if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): 29 | nn.init.kaiming_normal_(m.weight) 30 | 31 | def forward(self, input_dict): 32 | self.train(mode = input_dict.get("is_training", False)) 33 | obs = input_dict["obs"]["observation"].float() 34 | action_logits = self._tower(obs) 35 | action_mask = input_dict["obs"]["action_mask"].float() 36 | inf_mask = torch.clamp(torch.log(action_mask), -1e38, 1e38) 37 | return action_logits + inf_mask -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/作业说明.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/handout/作业说明.docx -------------------------------------------------------------------------------- /Homework/10-Mahjong/handout/国标麻将实验报告_学号_姓名.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Homework/10-Mahjong/handout/国标麻将实验报告_学号_姓名.docx -------------------------------------------------------------------------------- /Homework/10-Mahjong/valid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- encoding: utf-8 -*- 3 | # @Author : Arthals 4 | # @File : valid.py 5 | # @Time : 2024/06/30 18:34:46 6 | # @Contact : zhuozhiyongde@126.com 7 | # @Software: Visual Studio Code 8 | 9 | """ 10 | valid.py: 筛选出不能增强的绿一色、推不倒番型的对局,保存到 cannot_enhance_matches.json中 11 | """ 12 | 13 | import json 14 | import re 15 | 16 | match_id = -1 17 | cannot_enhance_matches = [] 18 | with open("data/data.txt", "r") as f: 19 | for line in f: 20 | if re.match("Match", line): 21 | match_id += 1 22 | if re.search("绿一色|推不倒", line): 23 | cannot_enhance_matches.append(match_id) 24 | 25 | print("[Detected] ", len(cannot_enhance_matches)) 26 | with open("data/cannot_enhance_matches.json", "w") as f: 27 | json.dump(cannot_enhance_matches, f) 28 | -------------------------------------------------------------------------------- /Note/03机器学习介绍与线性模型.assets/gaussian_distribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/03机器学习介绍与线性模型.assets/gaussian_distribution.png -------------------------------------------------------------------------------- /Note/03机器学习介绍与线性模型.assets/gd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/03机器学习介绍与线性模型.assets/gd.png -------------------------------------------------------------------------------- /Note/03机器学习介绍与线性模型.assets/nonlinear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/03机器学习介绍与线性模型.assets/nonlinear.png -------------------------------------------------------------------------------- /Note/04机器学习中的线性回归与分类问题.assets/dnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/04机器学习中的线性回归与分类问题.assets/dnn.png -------------------------------------------------------------------------------- /Note/04机器学习中的线性回归与分类问题.assets/logistic_regression_vs_linear_regression.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/04机器学习中的线性回归与分类问题.assets/logistic_regression_vs_linear_regression.png -------------------------------------------------------------------------------- /Note/04机器学习中的线性回归与分类问题.assets/mlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/04机器学习中的线性回归与分类问题.assets/mlp.png -------------------------------------------------------------------------------- /Note/04机器学习中的线性回归与分类问题.assets/softmax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/04机器学习中的线性回归与分类问题.assets/softmax.png -------------------------------------------------------------------------------- /Note/04机器学习中的线性回归与分类问题.assets/xor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/04机器学习中的线性回归与分类问题.assets/xor.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/L1-vs-L2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/L1-vs-L2.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/backpropagation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/backpropagation.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/bias.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/bias.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/dropout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/dropout.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/early-stopping.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/early-stopping.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/encoder-decoder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/encoder-decoder.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/gradient_descent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/gradient_descent.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/mlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/mlp.png -------------------------------------------------------------------------------- /Note/05神经网络基础.assets/neuron.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/05神经网络基础.assets/neuron.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/deconvolution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/deconvolution.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/depthwise_separable_convolution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/depthwise_separable_convolution.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/dilated_convolution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/dilated_convolution.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/horizontal_edge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/horizontal_edge.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/max_pooling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/max_pooling.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/mean_pooling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/mean_pooling.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/multi_layer_receptive_field.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/multi_layer_receptive_field.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/no_padding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/no_padding.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/pyramid_pooling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/pyramid_pooling.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/receptive_field.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/receptive_field.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/residual_block.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/residual_block.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/stereoscopic_perspective.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/stereoscopic_perspective.png -------------------------------------------------------------------------------- /Note/06卷积神经网络.assets/zero_padding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/06卷积神经网络.assets/zero_padding.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/NMS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/NMS.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/YOLO.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/YOLO.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/YOLO_v2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/YOLO_v2.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/ap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/ap.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/close-set-and-open-set.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/close-set-and-open-set.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/cpm_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/cpm_arch.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/depth_estimation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/depth_estimation.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/fcn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/fcn.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/feature_vector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/feature_vector.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/image-segmentation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/image-segmentation.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/image_translation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/image_translation.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/iou.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/iou.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/loss_weighting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/loss_weighting.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/mirror_padding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/mirror_padding.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/open-pose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/open-pose.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/open_pose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/open_pose.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/paf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/paf.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/point-wise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/point-wise.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/ppn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/ppn.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/ppn_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/ppn_arch.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/rcnn-vs-spp-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/rcnn-vs-spp-2.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/rcnn-vs-spp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/rcnn-vs-spp.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/roi-align.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/roi-align.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/roi-pooling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/roi-pooling.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/rpn-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/rpn-1.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/rpn-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/rpn-2.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/segnet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/segnet.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/skip-connection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/skip-connection.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/style_transfer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/style_transfer.png -------------------------------------------------------------------------------- /Note/07卷积神经网络应用.assets/super_resolution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/07卷积神经网络应用.assets/super_resolution.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/CV_and_CG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/CV_and_CG.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/CV_and_CG_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/CV_and_CG_2.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/GAN_traning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/GAN_traning.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/discriminative_vs_generative.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/discriminative_vs_generative.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/generative_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/generative_model.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/mse_vs_adversial_loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/mse_vs_adversial_loss.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/vae_and_gan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/vae_and_gan.png -------------------------------------------------------------------------------- /Note/08对抗神经网络.assets/vanilla_GAN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/08对抗神经网络.assets/vanilla_GAN.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/BiGAN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/BiGAN.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/CoGAN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/CoGAN.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/CoGAN_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/CoGAN_arch.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/cGAN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/cGAN.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/cGAN_application.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/cGAN_application.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/cGAN_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/cGAN_arch.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/cycleGAN_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/cycleGAN_arch.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/cycleGAN_target.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/cycleGAN_target.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/find_latent_representation_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/find_latent_representation_1.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/find_latent_representation_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/find_latent_representation_2.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/image_text_pair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/image_text_pair.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/mode_collapse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/mode_collapse.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/vae_and_gan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/vae_and_gan.png -------------------------------------------------------------------------------- /Note/09精选生成对抗网络.assets/vae_and_gan_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/09精选生成对抗网络.assets/vae_and_gan_2.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/3d_embedding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/3d_embedding.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/async_many_to_many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/async_many_to_many.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/async_many_to_many_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/async_many_to_many_2.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/bag-of-words.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/bag-of-words.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/many_to_one.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/many_to_one.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/one-hot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/one-hot.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/one_to_many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/one_to_many.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/rnn_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/rnn_arch.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/sequential_data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/sequential_data.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/sync_many_to_many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/sync_many_to_many.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/sync_many_to_many_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/sync_many_to_many_2.png -------------------------------------------------------------------------------- /Note/10循环神经网络.assets/word-embedding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/10循环神经网络.assets/word-embedding.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/human_attention.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/human_attention.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/masked_multihead_attention.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/masked_multihead_attention.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/multi-head_attention.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/multi-head_attention.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/position_encode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/position_encode.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/self-attention.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/self-attention.png -------------------------------------------------------------------------------- /Note/11注意力机制与Transformer.assets/self_attention_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/11注意力机制与Transformer.assets/self_attention_2.png -------------------------------------------------------------------------------- /Note/12用搜索解决问题.assets/basic_concept.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/12用搜索解决问题.assets/basic_concept.png -------------------------------------------------------------------------------- /Note/12用搜索解决问题.assets/eight_digital.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/12用搜索解决问题.assets/eight_digital.png -------------------------------------------------------------------------------- /Note/12用搜索解决问题.assets/informed_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/12用搜索解决问题.assets/informed_search.png -------------------------------------------------------------------------------- /Note/12用搜索解决问题.assets/pathfinding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/12用搜索解决问题.assets/pathfinding.png -------------------------------------------------------------------------------- /Note/13局部搜索和优化.assets/genetic_algorithms.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/13局部搜索和优化.assets/genetic_algorithms.png -------------------------------------------------------------------------------- /Note/13局部搜索和优化.assets/genetic_algorithms_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/13局部搜索和优化.assets/genetic_algorithms_2.png -------------------------------------------------------------------------------- /Note/13局部搜索和优化.assets/genetic_pesudo_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/13局部搜索和优化.assets/genetic_pesudo_code.png -------------------------------------------------------------------------------- /Note/13局部搜索和优化.assets/local_optimum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/13局部搜索和优化.assets/local_optimum.png -------------------------------------------------------------------------------- /Note/13局部搜索和优化.assets/solution_space.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/13局部搜索和优化.assets/solution_space.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/alpha_beta_pruning_pesudo_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/alpha_beta_pruning_pesudo_code.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/alpha_beta_pruning_pesudo_code_concise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/alpha_beta_pruning_pesudo_code_concise.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/alpha_pruning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/alpha_pruning.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/best_situation_of_alpha_beta_pruning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/best_situation_of_alpha_beta_pruning.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/beta_pruning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/beta_pruning.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/mcts_algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/mcts_algorithm.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/mcts_search_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/mcts_search_tree.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/minimax_pesudo_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/minimax_pesudo_code.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/minimax_pesudo_code_concise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/minimax_pesudo_code_concise.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/minimax_search_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/minimax_search_tree.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/uct_pesudo_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/uct_pesudo_code.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/uct_pesudo_code_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/uct_pesudo_code_2.png -------------------------------------------------------------------------------- /Note/14对抗搜索.assets/uct_pesudo_code_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/14对抗搜索.assets/uct_pesudo_code_3.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/epsilon_greedy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/epsilon_greedy.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/epsilon_greedy_algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/epsilon_greedy_algorithm.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/epsilon_vs_optimstic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/epsilon_vs_optimstic.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/gradient_bandit_bar_rt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/gradient_bandit_bar_rt.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/reinforcement_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/reinforcement_model.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/reward_func_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/reward_func_1.png -------------------------------------------------------------------------------- /Note/15强化学习基本思想和问题模型.assets/reward_func_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/15强化学习基本思想和问题模型.assets/reward_func_2.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/fc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/fc.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/fc_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/fc_2.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/generalized_policy_iteration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/generalized_policy_iteration.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/num_triangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/num_triangle.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/summary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/summary.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/summary_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/summary_2.png -------------------------------------------------------------------------------- /Note/16马尔可夫决策过程和动态规划.assets/value_iteration_and_policy_iteration_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/16马尔可夫决策过程和动态规划.assets/value_iteration_and_policy_iteration_comparison.png -------------------------------------------------------------------------------- /Note/17人工智能系统实践.assets/AI_pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/17人工智能系统实践.assets/AI_pipeline.png -------------------------------------------------------------------------------- /Note/17人工智能系统实践.assets/active_learning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/17人工智能系统实践.assets/active_learning.png -------------------------------------------------------------------------------- /Note/17人工智能系统实践.assets/self_training.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuozhiyongde/Fundamentals-of-Artificial-Intelligence-2024Spring-PKU/08b9a07cfd5cbca8666561e003ca04b58b343e1e/Note/17人工智能系统实践.assets/self_training.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fundamentals-of-Artificial-Intelligence-2024Spring-PKU 2 | 3 | 2024 年春季学期北京大学人工智能基础课程的课程资料,仉尚航老师班。 4 | 5 | 包括: 6 | 7 | - 笔记(Markdown 格式,包含一些考试题目) 8 | - 作业 9 | - 往年题(小测基本也都是从这里出的) 10 | 11 | 笔记内容可能存在 AI 生成的内容,我已经尽量保证内容的准确性,但仍不保证没有错误。如果发现错误,欢迎提 issue 或者 PR。 12 | 13 | 我的笔记亦可在我的博客阅览:https://arthals.ink/ 14 | 15 | PPT 因版权原因不提供,请在上课时自行下载。 16 | 17 | 麻将大作业的思路主要来自于 IJCAI 2023 的第一名,但代码为我自己实现。 18 | 19 | ## 广告 20 | 21 | [PKU Art](https://arthals.ink/posts/web/pku-art):教学网美化样式 22 | 23 | ## 感谢 24 | 25 | - [weekgoodday / Intro2ai](https://github.com/weekgoodday/Intro2ai) 26 | - [d2l-ai / d2l-zh](https://github.com/d2l-ai/d2l-zh) 27 | --------------------------------------------------------------------------------