474.Ones and Zeroes

474.Ones and Zeroes

  • 题目描述:In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.

    For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.

    Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once.

    Note:

    1. The given numbers of 0s and 1s will both not exceed 100
    2. The size of given string array won’t exceed 600.
  • Example 1:

    Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
    Output: 4
    
    Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0
  • Example 2:

    Input: Array = {"10", "0", "1"}, m = 1, n = 1
    Output: 2
    
    Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
  • 题目大意:给定一个只包含0与1的字符数组array,给定m表述0最多出现的次数,给定n表示1最多出现的次数,问在给定的m与n的范围内,可以从array中选出的最多字符的个数。

  • 思路:DP,0,1背包的变种,dp[i][j]表示m为i,n为j能找出的最多字符串的个数。

  • 代码

    package DP;
    
    /**
    * @author OovEver
    * 2017/12/25 9:59
    */
    public class LeetCode474 {
      public int findMaxForm(String[] strs, int m, int n) {
          int[][] dp = new int[m + 1][n + 1];
          for (String s : strs) {
              int[] count = count(s);
              for(int i=m;i>=count[0];i--) {
                  for(int j=n;j>=count[1];j--) {
                      dp[i][j] = Math.max(dp[i][j], 1 + dp[i - count[0]][j - count[1]]);
                  }
              }
          }
          return dp[m][n];
      }
    
      /**
       * 统计s中0与1的数量
       * @param s 要统计的字符串
       * @return 字符串中0与1的数量
       */
      private int[] count(String s) {
          int[] res = new int[2];
          for(int i=0;i<s.length();i++) {
              res[s.charAt(i) - '0']++;
          }
          return res;
      }
    }
    
import swift import roboticstoolbox as rtb import spatialgeometry as sg import spatialmath as sm import qpsolvers as qp import numpy as np import math def step_robot(r: rtb.ERobot, Tep): wTe = r.fkine(r.q) eTep = np.linalg.inv(wTe) @ Tep # Spatial error et = np.sum(np.abs(eTep[:3, -1])) # Gain term (lambda) for control minimisation Y = 0.01 # Quadratic component of objective function Q = np.eye(r.n + 6) # Joint velocity component of Q Q[: r.n, : r.n] *= Y Q[:2, :2] *= 1.0 / et # Slack component of Q Q[r.n :, r.n :] = (1.0 / et) * np.eye(6) v, _ = rtb.p_servo(wTe, Tep, 1.5) v[3:] *= 1.3 # The equality contraints Aeq = np.c_[r.jacobe(r.q), np.eye(6)] beq = v.reshape((6,)) # The inequality constraints for joint limit avoidance Ain = np.zeros((r.n + 6, r.n + 6)) bin = np.zeros(r.n + 6) # The minimum angle (in radians) in which the joint is allowed to approach # to its limit ps = 0.1 # The influence angle (in radians) in which the velocity damper # becomes active pi = 0.9 # Form the joint limit velocity damper Ain[: r.n, : r.n], bin[: r.n] = r.joint_velocity_damper(ps, pi, r.n) # Linear component of objective function: the manipulability Jacobian c = np.concatenate( (np.zeros(2), -r.jacobm(start=r.links[4]).reshape((r.n - 2,)), np.zeros(6)) ) # Get base to face end-effector kε = 0.5 bTe = r.fkine(r.q, include_base=False).A θε = math.atan2(bTe[1, -1], bTe[0, -1]) ε = kε * θε c[0] = -ε # The lower and upper bounds on the joint velocity and slack variable lb = -np.r_[r.qdlim[: r.n], 10 * np.ones(6)] ub = np.r_[r.qdlim[: r.n], 10 * np.ones(6)] # Solve for the joint velocities dq qd = qp.solve_qp(Q, c, Ain, bin, Aeq, beq, lb=lb, ub=ub) qd = qd[: r.n] if et > 0.5: qd *= 0.7 / et else: qd *= 1.4 if et < 0.02: return True, qd else:
最新发布
03-12
<think>好的,用户的问题是关于如何使用Python中的roboticstoolbox、spatialmath、qpsolvers和numpy库来实现机器人路径规划与控制中的关节速度求解。我需要先理解这些库的功能以及它们在路径规划中的作用。 首先,roboticstoolbox是一个用于机器人建模和控制的库,可以方便地创建机器人模型,进行运动学和动力学计算。spatialmath可能用于处理空间中的几何变换,比如位姿的表示和转换。qpsolvers应该是用来解决二次规划问题的,这在优化关节速度时可能会用到。numpy则是进行数值计算的基础库。 用户提到的关节速度求解,可能涉及到逆运动学问题。传统逆运动学通常求解关节位置,但在动态控制中,可能需要直接求解关节速度,使末端执行器达到期望的速度。这时候,雅可比矩阵就派上用场了。雅可比矩阵将关节速度与末端执行器的速度联系起来,公式是$v = J(q) \dot{q}$。为了求解$\dot{q}$,通常需要求雅可比矩阵的逆或伪逆,尤其是在机器人处于奇异位形时,伪逆可以提供一个最小二乘解。 不过,用户还提到了使用qpsolvers,这意味着可能需要处理带有约束的优化问题。例如,在求解关节速度时,除了满足末端速度要求,还要考虑关节速度限制、避免碰撞等约束。这时候,二次规划(QP)可以将问题转化为优化目标函数,同时满足线性约束。 接下来,我需要考虑如何将这些库结合起来。首先用roboticstoolbox建立机器人模型,计算当前位姿下的雅可比矩阵。然后,设定末端期望速度,构建QP问题,目标是最小化关节速度的二次型,同时满足雅可比方程和其他约束条件。使用qpsolvers来求解这个QP问题,得到最优的关节速度。 需要注意的细节包括:雅可比矩阵的正确计算,QP问题的正确构建(比如目标函数和约束的矩阵形式),以及如何处理冗余自由度的情况。此外,还要考虑数值稳定性,比如奇异位形下的处理,可能需要阻尼最小二乘法等方法。 然后,我需要验证这个思路是否正确。查阅相关文档,比如roboticstoolbox的逆速度运动学示例,是否支持通过QP求解。可能还需要参考类似的问题或教程,看看有没有使用这些库组合的案例。 最后,整理步骤,编写示例代码,并解释关键部分。确保代码正确导入了所需的库,正确创建了机器人模型,计算了雅可比矩阵,构建了QP问题并调用求解器。同时,添加必要的注释,说明参数的意义和调整方法。</think>以下是如何使用`roboticstoolbox`、`spatialmath`、`qpsolvers`和`numpy`实现机器人关节速度求解的完整方案: ### 一、核心原理 在机器人路径规划中,关节速度求解通常通过逆运动学速度关系实现。雅可比矩阵$J(q)$将关节速度$\dot{q}$与末端执行器速度$v$联系起来: $$v = J(q)\dot{q}$$ 当需要满足约束条件时,可构建二次规划问题: $$ \begin{aligned} \min_{\dot{q}} \quad & \frac{1}{2}\dot{q}^T Q \dot{q} + c^T \dot{q} \\ \text{s.t.} \quad & A\dot{q} \leq b \\ & A_{eq}\dot{q} = b_{eq} \end{aligned} $$ ### 二、实现步骤 #### 1. 环境配置 ```python pip install roboticstoolbox spatialmath-python qpsolvers numpy ``` #### 2. 代码实现 ```python import numpy as np from roboticstoolbox import DHRobot, RevoluteDH from spatialmath import SE3 from qpsolvers import solve_qp # 1. 构建机械臂模型(以6轴机械臂为例) robot = DHRobot([ RevoluteDH(d=0.1, alpha=np.pi/2), RevoluteDH(a=0.5), RevoluteDH(a=0.5, alpha=-np.pi/2), RevoluteDH(d=0.5, alpha=np.pi/2), RevoluteDH(alpha=-np.pi/2), RevoluteDH()] ) # 2. 定义当前关节角度和期望末端速度 q_current = np.array([0.1, 0.5, -0.3, 0, 0.2, 0]) # 当前关节位置 v_desired = np.array([0.1, 0, 0, 0, 0, 0.1]) # [vx, vy, vz, ωx, ωy, ωz] # 3. 计算雅可比矩阵 J = robot.jacob0(q_current) # 空间雅可比矩阵 # 4. 构建QP问题参数 Q = np.eye(robot.n) # 目标函数权重矩阵 A = np.vstack((np.eye(robot.n), -np.eye(robot.n))) # 关节速度上下限约束 b = np.hstack((np.ones(robot.n)*0.5, np.ones(robot.n)*0.5)) # ±0.5 rad/s限幅 # 5. 等式约束 (Jq̇ = v_desired) A_eq = J b_eq = v_desired # 6. 求解QP问题 q_dot = solve_qp( P=Q, q=None, G=A, h=b, A=A_eq, b=b_eq, solver="osqp" ) print("Optimal joint velocities:", q_dot) ``` ### 三、关键参数说明 1. **雅可比矩阵计算**:`robot.jacob0()`计算空间坐标系下的雅可比矩阵[^1] 2. **速度约束**:通过$A\dot{q} \leq b$实现关节速度限幅 3. **QP求解器选择**:支持`osqp`、`quadprog`等多种求解器,可根据需求选择 ### 四、扩展应用 对于冗余机械臂(关节数>6),可添加零空间优化目标: ```python Q = np.eye(robot.n) + 0.1 * (np.eye(robot.n) - J.T @ np.linalg.pinv(J.T)) ``` 这将在满足末端速度的同时最小化关节运动量[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值