首页 > 其他分享 >大二暑假第五周博客

大二暑假第五周博客

时间:2023-08-13 21:13:15浏览次数:40  
标签:loc Docking ships 博客 暑假 time 大二 Type ports

第五周,一些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

相关文章

  • 博客园 cn book 主题备份
    又到了一年一度的换肤环节cnbook......
  • 暑假牛客多校第八场 2023-8-11(H、K)
    H.Insert1,Insert2,Insert3,...算法:栈做法:   我们分析题目发现每个区间的左端点一定是\(1\),而且每个新加入的数\(x\)一定是匹配最靠近它的且未经匹配的\(x-1\)。举个例子,在[1,1,2,2,3]中我们加入一个数\(3\)时由于从左到右的第二个\(2\)是已经和第一个......
  • 基于Hexo和Butterfly创建个人技术博客,(14) 给博客站点添加Aplayer音乐
    本章目标:掌握aplayer音乐插件的用法给博客站点添加音乐功能一、概述个人比较倾向网站以简洁为主,并不赞成把网站做成花里虎哨的,比如添加鼠标特效或各种动态的元素。但个人站点的随意性比较大,虽没必要做成全局的音乐播放能力,但还是可以做成单独页或在无关紧的模块添加音乐功能。笔者......
  • Typora上传文件到博客园解决图片问题
    EasyBlogImageForTypora使用Typora写作,图片即时同步到博客网站,无需第三方图床,写完可直接粘贴。支持网络图片上传。适用范围本程序基于.netcore3.1开发,支持在win-x64,macosx-x64系统运行,免安装。linux暂时不考虑,如果有需要再说。程序的上传服务是使用MetaWebBlogAPI,MetaWe......
  • 对博客美化的尝试
    随手粘一个以前oi时期的博文看看效果今天考试T1考这个,以前只在床上看书时推过一遍,现在忘完了。复习(重学)一下。学习博客:CSDNOIWIKIDFS树树枝边:DFS时经过的边,即DFS搜索树上的边。前向边:与DFS方向一致,从某个结点指向其某个子孙的边。后向边:与DFS方向相反,从某个结点指向其......
  • 暑假周记(8.8)
    voidgetChars(intsrcBegin,intsrcEnd,char[]dst,intdstBegin):复制到目标字符数组inthashCode():哈希码intindexOf(intch):返回指定字符在此字符串中第一次出现处的索引intindexOf(intch,intfromIndex):从指定的索引开始搜索intindexOf(Stringstr):返回指定子字符串在......
  • 暑假周记(8.9)
    intlastIndexOf(Stringstr):返回指定子字符串在此字符串中最右边出现处的索引intlastIndexOf(Stringstr,intfromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索intlength():长度booleanmatches(Stringregex):是否匹配给定的正则表达式boo......
  • 暑假第六周总结
    在本周,我学习的内容很少,因为我在本周选择了出去旅行放松身心。在这一周中,我带着我的弟弟去了北京。这是我半年前答应好他的,在这个暑假我来实现这个承诺。说是带他去北京,但是这也是我为数不多去北京的机会,之前因为年纪小去过的景点都有些忘记,这一次去又是一种全新的体验......
  • 8.7-8.13学习总结博客五:Hive进阶与复杂查询
    博客题目:学习总结五:Hive进阶与复杂查询实践内容概要:学习Hive进阶的使用方法,包括复杂查询、数据转换和性能优化等方面的知识。学习资源:推荐的Hive进阶教程、实践案例和性能优化技巧。实践内容:通过编写复杂的Hive查询语句,探索Hive的高级功能和性能优化方法,并分享实践中的挑战和解决......
  • 2023.8.7-2023.8.14暑假第五周博客
    2023.8.7今天人在外,因此博客休息一天图片如下 2023.8.8今天对hive有了进一步的了解首先要明确一个流程当我打开三台虚拟机,用finalshell连接上后首先要使用如下命令1.su-hadoop切换到hadoop用户,大部分操作都必须在hadoop用户中完成,而千万不要再root中,因为root用户一......