2020建模穿越沙漠第一关python代码

两段Python代码分别对应两个不同的沙漠穿越问题,涉及路径规划、资源管理和动态规划。代码计算了在不同天气、地点和行动策略下,如何在限定时间内从起点到达终点,同时最大化收益并确保生存。主要涉及数据结构、算法和决策制定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2020建模穿越沙漠第一问python代码

第一关

import numpy as np

qua = np.array((0, 1, 2, 3))  # 4种类型的特殊点:0-> 起点; 1-> 村庄;2 ->矿山; 3-> 终点
dist = np.array(((0, 6, 8, 3),
                 (6, 0, 2, 3),
                 (8, 2, 0, 5),
                 (3, 3, 5, 0)))
f = np.array(((0, 1, 1, 1),  # 判断4中特殊点之间的关系
              (0, 0, 1, 1),
              (0, 1, 0, 1),
              (0, 0, 0, 0)))
wea = np.array((2, 2, 1, 3, 1,  # 30天内的天气
                2, 3, 1, 2, 2,
                3, 2, 1, 2, 2,
                2, 3, 3, 2, 2,
                1, 1, 2, 1, 3,
                2, 1, 1, 2, 2))
mx, my = 3, 2  # 水和事物的重量
cx, cy = 5, 10  # 水和事物的价格
sx = np.array((0, 5, 8, 10))  # 第i种天气的水消耗量
sy = np.array((0, 7, 6, 10))  # 第i种天气的食物消耗量

n = 4  # 特殊点个数

maxm = 1200  # 背包容量
coins = 10000  # 初始资金
base = 1000  # 基础收益
date = 30  # 最晚期限
costx = np.zeros((32, 4, 4))  # 第k天从i到j的水消耗量
costy = np.zeros((32, 4, 4))  # 第k天从i到j的事食物消耗量
days = np.zeros((32, 4, 4), dtype=int)  # 第k天从i到j需要多长时间
ans = 0  # 最后的资金
act = np.empty(32)  # 每天的行为:2-> 挖矿;1-> 在矿山休息;0-> 村庄购买食物
rec = np.empty(32)  # 记录每天在哪里

# ansx、ansact记录最优路径上的信息
ansx = np.empty(32)
ansact = np.empty(32)
ansg, ansh = 0, 0  # 记录最优的初始水和食物 


def dfs(day: int, now: int, nm: int, c: int, x: int, y: int, type: int) -> None:
    act[day] = type
    rec[day] = now
    global ans, ansg, ansh

    if qua[now] == 3:
        if ans <= c+x*cx+y*cy:
            ansg = g
            ansh = h
            ans = c+x*cx+y*cy
            for i in range(date+1):
                ansx[i] = rec[i]
                ansact[i] = act[i]
        act[day] = -1
        rec[day] = -1
        return

    if day >= date:
        act[day] = -1
        rec[day] = -1
        return

    if qua[now] == 1:
        nm = maxm - mx*x - my*y

    for i in range(n):
        if f[qua[now]][qua[i]]:
            tx = costx[day][now][i]
            ty = costy[day][now][i]
            ucost = c
            um =nm
            if x >= tx:
                ux = x - tx
            else:
                ux = 0
                ucost -= 2*(tx-x)*cx
                um -= (tx - x)*mx
            if y >= ty:
                uy = y - ty
            else:
                uy = 0
                ucost -= 2*(ty - y)*cy
                um -= (ty - y)*my

            if ucost < 0 or um < 0:
                continue
            dfs(day+days[day][now][i], i, um, ucost, ux, uy, 0)

    if qua[now] == 2:
        attday = day
        tx = sx[wea[attday]]
        ty = sy[wea[attday]]
        attday += 1
        if x > tx:
            x -= tx
            tx = 0
        else:
            tx = tx - x
            x = 0
        if y >= ty:
            y -= ty
            ty = 0
        else:
            ty = ty - y
            y = 0
        nm -= tx*mx + ty*my
        c -= 2*tx*cx + 2*ty*cy
        if nm >= 0 and c >= 0:
            dfs(attday, now, nm, c, x, y, 1)

        attday = day
        tx = sx[wea[attday]]*2
        ty = sy[wea[attday]]*2
        attday += 1
        if x >= tx:
            x -= tx
            tx = 0
        else:
            tx = tx - x
            x = 0
        if y >= ty:
            y -= ty
            ty = 0
        else:
            ty = ty -y
            y = 0
        nm -= tx*mx + ty*my
        c -= 2*tx*cx + 2*ty*cy
        c += base
        if nm >= 0 and c >= 0:
            dfs(attday, now, nm, c, x, y, 2)

    rec[day] = -1
    act[day] = -1

if __name__ == '__main__':
    for d in range(date+1):
        rec[d] = -1
        act[d] = -1

    for d in range(date):
        for i in range(n):
            for j in range(n):
                if f[qua[i]][qua[j]]:
                    now, count, sumx, sumy = 0, 0, 0, 0
                    while count < dist[i][j]:
                        if wea[now+d] != 3:
                            count += 1
                            sumx += 2*sx[wea[now+d]]
                            sumy += 2*sy[wea[now+d]]
                        else:
                            sumx += sx[wea[now+d]]
                            sumy += sy[wea[now+d]]

                        now += 1
                        if now + d >= date:
                            break
                    if count < dist[i][j]:
                        sumx = sumy = 20000
                        now = 30
                    costx[d][i][j] = sumx
                    costy[d][i][j] = sumy
                    days[d][i][j] = now
    print(type(days[0,0,0]))

    dic = {}
    for i in range(maxm+1):
        g = i // mx
        h = (maxm-i)//my
        # print(g, h)
        dic.setdefault((g,h), 0)
        if not dic[(g,h)]:
            print((g,h))
            dfs(0, 0, 0, coins-g*cx-h*cy, g, h, -1)
        dic[(g, h)] = 1

    print(ans)

第二关

import numpy as np

qua = np.array((0, 2, 1, 2, 1, 3))  # 4种类型的特殊点:0-> 起点; 1-> 村庄;2 ->矿山; 3-> 终点
dist = np.array(((0, 7, 8, 9, 9, 11),
                 (7, 0, 1, 3, 4, 4),
                 (8, 1, 0, 2, 3, 3),
                 (9, 3, 2, 0, 1, 2),
                 (9, 4, 3, 1, 0, 2),
                 (11, 4, 3, 2, 2, 0)))
f = np.array(((0, 1, 1, 1),  # 判断4中特殊点之间的关系
              (0, 0, 1, 1),
              (0, 1, 0, 1),
              (0, 0, 0, 0)))
wea = np.array((2, 2, 1, 3, 1,  # 30天内的天气
                2, 3, 1, 2, 2,
                3, 2, 1, 2, 2,
                2, 3, 3, 2, 2,
                1, 1, 2, 1, 3,
                2, 1, 1, 2, 2))
mx, my = 3, 2  # 水和事物的重量
cx, cy = 5, 10  # 水和事物的价格
sx = np.array((0, 5, 8, 10))  # 第i种天气的水消耗量
sy = np.array((0, 7, 6, 10))  # 第i种天气的食物消耗量

n = 6  # 特殊点个数

maxm = 1200  # 背包容量
coins = 10000  # 初始资金
base = 1000  # 基础收益
date = 30  # 最晚期限
costx = np.zeros((32, 6, 6))  # 第k天从i到j的水消耗量
costy = np.zeros((32, 6, 6))  # 第k天从i到j的事食物消耗量
days = np.zeros((32, 6, 6), dtype=int)  # 第k天从i到j需要多长时间
ans = 0  # 最后的资金
act = np.empty(32)  # 每天的行为:2-> 挖矿;1-> 在矿山休息;0-> 村庄购买食物
rec = np.empty(32)  # 记录每天在哪里

# ansx、ansact记录最优路径上的信息
ansx = np.empty(32)
ansact = np.empty(32)
ansg, ansh = 0, 0  # 记录最优的初始水和食物


def dfs(day: int, now: int, nm: int, c: int, x: int, y: int, type: int) -> None:
    act[day] = type
    rec[day] = now
    global ans, ansg, ansh

    if qua[now] == 3:
        if ans <= c+x*cx+y*cy:
            ansg = g
            ansh = h
            ans = c+x*cx+y*cy
            for i in range(date+1):
                ansx[i] = rec[i]
                ansact[i] = act[i]
        act[day] = -1
        rec[day] = -1
        return

    if day >= date:
        act[day] = -1
        rec[day] = -1
        return

    if qua[now] == 1:
        nm = maxm - mx*x - my*y

    for i in range(n):
        if f[qua[now]][qua[i]]:
            tx = costx[day][now][i]
            ty = costy[day][now][i]
            ucost = c
            um =nm
            if x >= tx:
                ux = x - tx
            else:
                ux = 0
                ucost -= 2*(tx-x)*cx
                um -= (tx - x)*mx
            if y >= ty:
                uy = y - ty
            else:
                uy = 0
                ucost -= 2*(ty - y)*cy
                um -= (ty - y)*my

            if ucost < 0 or um < 0:
                continue
            dfs(day+days[day][now][i], i, um, ucost, ux, uy, 0)

    if qua[now] == 2:
        attday = day
        tx = sx[wea[attday]]
        ty = sy[wea[attday]]
        attday += 1
        if x > tx:
            x -= tx
            tx = 0
        else:
            tx = tx - x
            x = 0
        if y >= ty:
            y -= ty
            ty = 0
        else:
            ty = ty - y
            y = 0
        nm -= tx*mx + ty*my
        c -= 2*tx*cx + 2*ty*cy
        if nm >= 0 and c >= 0:
            dfs(attday, now, nm, c, x, y, 1)

        attday = day
        tx = sx[wea[attday]]*2
        ty = sy[wea[attday]]*2
        attday += 1
        if x >= tx:
            x -= tx
            tx = 0
        else:
            tx = tx - x
            x = 0
        if y >= ty:
            y -= ty
            ty = 0
        else:
            ty = ty -y
            y = 0
        nm -= tx*mx + ty*my
        c -= 2*tx*cx + 2*ty*cy
        c += base
        if nm >= 0 and c >= 0:
            dfs(attday, now, nm, c, x, y, 2)

    rec[day] = -1
    act[day] = -1

if __name__ == '__main__':
    for d in range(date+1):
        rec[d] = -1
        act[d] = -1

    for d in range(date):
        for i in range(n):
            for j in range(n):
                if f[qua[i]][qua[j]]:
                    now, count, sumx, sumy = 0, 0, 0, 0
                    while count < dist[i][j]:
                        if wea[now+d] != 3:
                            count += 1
                            sumx += 2*sx[wea[now+d]]
                            sumy += 2*sy[wea[now+d]]
                        else:
                            sumx += sx[wea[now+d]]
                            sumy += sy[wea[now+d]]

                        now += 1
                        if now + d >= date:
                            break
                    if count < dist[i][j]:
                        sumx = sumy = 20000
                        now = 30
                    costx[d][i][j] = sumx
                    costy[d][i][j] = sumy
                    days[d][i][j] = now
    print(type(days[0,0,0]))

    dic = {}
    for i in range(maxm+1):
        g = i // mx
        h = (maxm-i)//my
        # print(g, h)
        dic.setdefault((g,h), 0)
        if not dic[(g,h)]:
            print((g,h))
            dfs(0, 0, 0, coins-g*cx-h*cy, g, h, -1)
        dic[(g, h)] = 1

    print(ans)

根据引用和引用的内容,如果要在数学建模中使用Python穿越沙漠,可以考虑使用动态规划作为主要的求解方法。可以使用Python编写代码来实现动态规划算法,并根据问题的需求来定义状态转移方程和约束条件。 例如,引用中提到的状态转移方程可以用Python代码实现如下: ```python # 初始化dp数组 dp = [[[0 * (max_water + 1) for _ in range(max_food + 1)] for _ in range(max_days + 1)] # 动态规划求解 for k in range(1, max_days + 1): for jj in range(num_states): for w in range(max_water + 1): for f in range(max_food + 1): if not is_sandstorm_day(k): # 更新状态转移方程 dp[k][jj][w][f = max(dp[k-1][j][w-walk*xh_water[tq]][f-walk*xh_food[tq]] for j in range(num_states)) ``` 在上述代码中,`dp`代表状态数组,`k`表示第k天,`jj`表示状态,`w`表示剩余的水量,`f`表示剩余的食物量。`max_days`、`max_water`、`max_food`分别表示最大天数、最大水量和最大食物量。`is_sandstorm_day(k)`用来判断第k天是否是沙暴天气。 需要注意的是,以上只是一个示例,具体的数学建模问题可能需要根据实际情况进行适当的调整和修改。 综上所述,可以使用Python编程语言来进行穿越沙漠的数学建模。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [数学建模2020B题穿越沙漠](https://blog.youkuaiyun.com/qq_21561833/article/details/122783459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andy-wen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值