第五周,一些python的代码,有一些bug还没找出来
{ "cells": [ { "cell_type": "markdown", "id": "f427f82c", "metadata": {}, "source": [ "# 第一题" ] }, { "cell_type": "code", "execution_count": 12, "id": "3bb0e5ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "总的等待时间: 25 h\n", "总的延迟时间: 1 h\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# 创建码头的DataFrame\n", "ports = pd.DataFrame({'Type': ['Bulk', 'Container', 'Gas'],\n", " 'Number': [3, 2, 1],\n", " 'Capacity': [4, 3, 2],\n", " 'Docking time limit': [8, 12, 16]})\n", "\n", "# 创建船只的DataFrame\n", "ships = pd.DataFrame({'Type': ['Bulk', 'Container', 'Gas'],\n", " 'Arrival': [10, 6, 3],\n", " 'Docking time': [4, 6, 8],\n", " 'Average stay': [6, 8, 10],\n", " 'Priority': [1, 2, 3]})\n", "\n", "# 模拟船只的到达和离开\n", "for i in range(24): # 对于每一个小时\n", " # 检查是否有船只到达\n", " for j in range(len(ships)):\n", " if np.random.rand() < ships.loc[j, 'Arrival'] / 24: # 如果类型j的船只到达\n", " # 寻找可用的、优先级最高的码头\n", " available_ports = ports[(ports['Number'] > 0) & (ports['Type'] == ships.loc[j, 'Type'])]\n", " if len(available_ports) > 0:\n", " # 减少可用码头的数量\n", " ports.loc[available_ports.index[0], 'Number'] -= 1\n", " # 增加船只的停靠时间\n", " ships.loc[j, 'Docking time'] += ships.loc[j, 'Average stay']\n", " else:\n", " # 如果没有可用码头,船只需要等待\n", " ships.loc[j, 'Docking time'] += 1\n", "\n", " # 检查是否有船只离开\n", " for j in range(len(ships)):\n", " if ships.loc[j, 'Docking time'] > 0:\n", " ships.loc[j, 'Docking time'] -= 1\n", " if ships.loc[j, 'Docking time'] == 0:\n", " # 如果船只完成了停靠,增加可用码头的数量\n", " ports.loc[ports[ports['Type'] == ships.loc[j, 'Type']].index[0], 'Number'] += 1\n", "\n", "# 计算总的等待时间和延迟时间\n", "total_waiting_time = ships['Docking time'].sum()\n", "total_delay_time = ships['Docking time'].sum() - ships['Average stay'].sum()\n", "\n", "print(f'总的等待时间: {total_waiting_time}','h')\n", "print(f'总的延迟时间: {total_delay_time}','h')" ] }, { "cell_type": "markdown", "id": "ce5c1f05", "metadata": {}, "source": [ "# 第二题" ] }, { "cell_type": "code", "execution_count": 10, "id": "cdefc713", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "日总收入(万元): 110\n", "运营效率: 1.1956521739130435\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# 创建码头的DataFrame\n", "ports = pd.DataFrame({'Type': ['Bulk', 'Container', 'Gas'],\n", " 'Number': [3, 2, 1],\n", " 'Capacity': [4, 3, 2],\n", " 'Docking time limit': [8, 12, 16],\n", " 'Average Load': [0.8, 0.7, 0.6]})\n", "\n", "# 创建船只的DataFrame\n", "ships = pd.DataFrame({'Type': ['Bulk', 'Container', 'Gas'],\n", " 'Arrival': [10, 6, 3],\n", " 'Docking time': [4, 6, 8],\n", " 'Average stay': [6, 8, 10],\n", " 'Priority': [1, 2, 3],\n", " 'Revenue': [15, 20, 25]})\n", "\n", "# 每日运营成本\n", "operating_cost = 100\n", "\n", "# 计算每日收益\n", "daily_revenue = 0\n", "\n", "# 模拟船只的到达和离开\n", "for i in range(24): # 对于每一个小时\n", " # 检查是否有船只到达\n", " for j in range(len(ships)):\n", " if np.random.rand() < ships.loc[j, 'Arrival'] / 24: # 如果类型j的船只到达\n", " # 寻找可用的、优先级最高的码头\n", " available_ports = ports[(ports['Number'] > 0) & (ports['Type'] == ships.loc[j, 'Type'])]\n", " if len(available_ports) > 0:\n", " # 减少可用码头的数量\n", " ports.loc[available_ports.index[0], 'Number'] -= 1\n", " # 增加船只的停靠时间\n", " ships.loc[j, 'Docking time'] += ships.loc[j, 'Average stay']\n", " # 增加收益\n", " daily_revenue += ships.loc[j, 'Revenue']\n", " else:\n", " # 如果没有可用码头,船只需要等待\n", " ships.loc[j, 'Docking time'] += 1\n", "\n", " # 检查是否有船只离开\n", " for j in range(len(ships)):\n", " if ships.loc[j, 'Docking time'] > 0:\n", " ships.loc[j, 'Docking time'] -= 1\n", " if ships.loc[j, 'Docking time'] == 0:\n", " # 如果船只完成了停靠,增加可用码头的数量\n", " ports.loc[ports[ports['Type'] == ships.loc[j, 'Type']].index[0], 'Number'] += 1\n", "\n", "# 计算总的等待时间和延迟时间\n", "total_waiting_time = ships['Docking time'].sum()\n", "total_delay_time = ships['Docking time'].sum() - ships['Average stay'].sum()\n", "\n", "# 计算运营效率\n", "operating_efficiency = daily_revenue / (total_waiting_time + total_delay_time + operating_cost)\n", "\n", "print(f\"日总收入(万元): {daily_revenue}\")\n", "print(f\"运营效率: {operating_efficiency}\")" ] }, { "cell_type": "markdown", "id": "7ead9f92", "metadata": {}, "source": [ "# 第三题" ] }, { "cell_type": "code", "execution_count": 13, "id": "6f9e6c4b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "总的等待时间: 13 hours\n", "总的延迟时间: 24 hours\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# 定义港口数据\n", "ports = pd.DataFrame({\n", " 'Type': ['Bulk', 'Container', 'LPG'],\n", " 'Number': [3, 2, 1],\n", " 'Capacity': [4, 3, 2],\n", " 'Time Limit': [8, 12, 16],\n", " 'Priority': [1, 2, 3]\n", "}).set_index('Type')\n", "\n", "\n", "# 定义船只数据\n", "ships = pd.DataFrame({\n", " 'Type': ['Bulk', 'Container', 'LPG'],\n", " 'Arrival Number': [10, 6, 3],\n", " 'Docking Time': [6, 8, 10],\n", " 'Priority': [1, 2, 3]\n", "}).set_index('Type')\n", "\n", "# 初始化等待时间和延迟时间\n", "waiting_time = 0\n", "delay_time = 0\n", "\n", "# 模拟24小时的运营过程\n", "for hour in range(24):\n", " # 检查是否有船只到达\n", " arriving_ships = ships[ships['Arrival Number'] > 0]\n", " if len(arriving_ships) > 0:\n", " selected_ship = arriving_ships.sort_values(by='Priority', ascending=False).iloc[0]\n", " # 减少到达数量\n", " ships.loc[selected_ship.name, 'Arrival Number'] -= 1\n", " \n", " # 检查是否有可用的码头\n", " available_ports = ports[ports['Number'] > 0]\n", " if len(available_ports) > 0:\n", " selected_port = available_ports.sort_values(by='Priority', ascending=False).iloc[0]\n", " # 减少可用码头的数量\n", " ports.loc[selected_port.name, 'Number'] -= 1\n", " \n", " # 计算停靠时间\n", " docking_time = selected_ship['Docking Time']\n", " if docking_time > selected_port['Time Limit']:\n", " delay_time += docking_time - selected_port['Time Limit']\n", " docking_time = selected_port['Time Limit']\n", " ports.loc[selected_port.name, 'Time Limit'] -= docking_time\n", " else:\n", " # 如果没有可用码头,增加等待时间\n", " waiting_time += 1\n", "\n", "print(f\"总的等待时间: {waiting_time} hours\")\n", "print(f\"总的延迟时间: {delay_time} hours\")" ] }, { "cell_type": "code", "execution_count": null, "id": "451748ea", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 5 }
标签:loc,Docking,ships,博客,暑假,time,大二,Type,ports From: https://www.cnblogs.com/Bronya019c/p/17627268.html