Protel99seMEX3

2023-05-16

Protel99se的鼠标增强软件,可以实现用鼠标放大与缩小电路图,十分方便!

文件:n459.com/file/25127180-478161061

以下内容无关:

-------------------------------------------分割线---------------------------------------------

说起Alias,你可能第一个联想到的是Linux中的Alias命令,就像中世纪那些躲在茅坑下面(是真的,起码日本有粪坑忍者,没有马桶的年代就是社会的噩梦)进行刺杀的杀手一样,让人防不胜防,对于那些被这个命令坑过的人来说,电脑必须时刻出现在视野内,因为你不知道你身边的杀手朋友什么时候会模仿中世纪茅坑杀手在你的终端执行这样一条命令。

alias cd=rm -rf

(如果不懂这个梗,给一个小提示,alias命令是给某个命令重命名,这里把cd命令改成了rm -rf命令,你每次进入目录其实是删除了目录)

说回正题,前段时间看同事写的代码有一个应用到权重的地方,对于系统中用到的两个数据源,我们希望有一定比例走新数据源,一定比例走老数据源(感觉用的花里胡哨,直接配置中心搞一个开关不就好了),看到他们写的代码中使用了Alias Method来实现权重,所以就专门学习了一下(虽然感觉在新老数据源这个场景中使用有点鸡肋,但是掌握一门权重算法的实现还是很有帮助的)。

一. 原理
权重算法的本质是指定时间发生的几率,比如用户的每一次请求我有30%的几率打到服务器A,有20%的几率到服务器B,还有50%的几率到服务器C。那么给出未来十次的用户请求序列,要求给出它们会分别打到哪个服务器。

最简单的实现当然是产生一个位于0.0~1.0之间的随机数。

当随机数位于0~0.3时,请求服务器A
当随机数位于0.3~0.5时,请求服务器B
当随机数位于0.5~1时,请求服务器C
这种方式实现简单,生成一个随机数,然后在对应时间上if-else判断即可,但是存在精度问题。

另一种实现方式是离散算法,通过概率分布构造几个点,[30, 50, 100],代表三个区间,再生成1100的整数,看它位于哪个区间,比如45位于[3050]区间,就说明本次请求打到服务器B。

现在进入正题Alias Method算法,Alias Method的最终结果是要构造拼装出一个每一列合都为1的矩形,若每一列最后都要为1,那么要将所有元素都乘以概率类型的数量(此处为3)。

此时右边乘以3之后的模型概率大于一或者小于一,需要用大于一去补足小于一的,而且必须满足每列必须至多只有两种组合。

根据填充后的上图我们可以得到两个数组,分别是位于下方的[9/10, 6/10, 10/10]组成的Prod数组[0.9, 0.6, 1]以及上方用来填充的颜色的序号[3, 3, NULL],第一个3代表红色列被第三列蓝色填充,第二个3代表绿色列被第三列蓝色填充,第三列概率大于1,不需要填充,为NULL。

T 1 2 3
PDF 0.3 0.2 0.5
Prob 0.9 0.6 1
Alias 3 3 NULL
得到这两个数组之后,随机取其中的一列(代表事件),比如是第二列,让Prob[2]的值与一个随机小数f比较,如果f小于Prob[2],那么结果就是2,否则就是Alias[2],即3,若为第三列,Prob[3]必定大于小数f,结果就是3。

可以来简单验证一下,比如随机到第三列的概率是1/3,第三列全部为红色,则第三列事件3发生概率就是1/3乘1,蓝色再其他列上部分还有覆盖,分别是1/3乘2/5和1/3乘1/10,最终的结果还是为0.5,符合原来的pdf概率。这种算法初始化较复杂,但生成随机结果的时间复杂度为O(1),是一种性能非常好的算法。

二. 代码实现
下面是一个较为完整的代码实现,该实例在使用中只需要在AliasMethod中初始化好我们之前所说的Alias和Prob数组即可。

import java.util.*;

/**

  • @description 权重算法
    /
    public final class AliasMethod {
    /
    *

    • The random number generator used to sample from the distribution.
      /
      private final Random random;
      /
      *
    • The probability and alias tables.
      */
      private final int[] alias;
      private final double[] probability;

    /**

    • Constructs a new AliasMethod to sample from a discrete distribution and
    • hand back outcomes based on the probability distribution.
    • Given as input a list of probabilities corresponding to outcomes 0, 1,
    • …, n - 1, this constructor creates the probability and alias tables
    • needed to efficiently sample from this distribution.
    • @param probabilities The list of probabilities.
      */
      public AliasMethod(List probabilities) {
      this(probabilities, new Random());
      }

    /**

    • Constructs a new AliasMethod to sample from a discrete distribution and
    • hand back outcomes based on the probability distribution.
    • Given as input a list of probabilities corresponding to outcomes 0, 1,
    • …, n - 1, along with the random number generator that should be used
    • as the underlying generator, this constructor creates the probability
    • and alias tables needed to efficiently sample from this distribution.
    • @param probabilities The list of probabilities.
    • @param random The random number generator
      /
      public AliasMethod(List probabilities, Random random) {
      /
      Begin by doing basic structural checks on the inputs. /
      if (probabilities == null || random == null) {
      throw new NullPointerException();
      }
      if (probabilities.size() == 0) {
      throw new IllegalArgumentException(“Probability vector must be nonempty.”);
      }
      /
      Allocate space for the probability and alias tables. /
      probability = new double[probabilities.size()];
      alias = new int[probabilities.size()];
      /
      Store the underlying generator. /
      this.random = random;
      /
      Compute the average probability and cache it for later use. /
      final double average = 1.0 / probabilities.size();
      /
      Make a copy of the probabilities list, since we will be making
      • changes to it.
        /
        probabilities = new ArrayList<>(probabilities);
        /
        Create two stacks to act as worklists as we populate the tables. /
        Deque small = new ArrayDeque<>();
        Deque large = new ArrayDeque<>();
        /
        Populate the stacks with the input probabilities. /
        for (int i = 0; i < probabilities.size(); ++i) {
        /
        If the probability is below the average probability, then we add
        • it to the small list; otherwise we add it to the large list.
          /
          if (probabilities.get(i) >= average) {
          large.add(i);
          } else {
          small.add(i);
          }
          }
          /
          As a note: in the mathematical specification of the algorithm, we
      • will always exhaust the small list before the big list. However,
      • due to floating point inaccuracies, this is not necessarily true.
      • Consequently, this inner loop (which tries to pair small and large
      • elements) will have to check that both lists aren’t empty.
        /
        while (!small.isEmpty() && !large.isEmpty()) {
        /
        Get the index of the small and the large probabilities. /
        int less = small.removeLast();
        int more = large.removeLast();
        /
        These probabilities have not yet been scaled up to be such that
        • 1/n is given weight 1.0. We do this here instead.
          /
          probability[less] = probabilities.get(less) * probabilities.size();
          alias[less] = more;
          /
          Decrease the probability of the larger one by the appropriate
        • amount.
          /
          probabilities.set(more,
          (probabilities.get(more) + probabilities.get(less)) - average);
          /
          If the new probability is less than the average, add it into the
        • small list; otherwise add it to the large list.
          /
          if (probabilities.get(more) >= 1.0 / probabilities.size()) {
          large.add(more);
          } else {
          small.add(more);
          }
          }
          /
          At this point, everything is in one list, which means that the
      • remaining probabilities should all be 1/n. Based on this, set them
      • appropriately. Due to numerical issues, we can’t be sure which
      • stack will hold the entries, so we empty both.
        */
        while (!small.isEmpty()) {
        probability[small.removeLast()] = 1.0;
        }
        while (!large.isEmpty()) {
        probability[large.removeLast()] = 1.0;
        }
        }

    /**

    • Samples a value from the underlying distribution.
    • @return A random value sampled from the underlying distribution.
      /
      public int next() {
      /
      Generate a fair die roll to determine which column to inspect. /
      int column = random.nextInt(probability.length);
      /
      Generate a biased coin toss to determine which option to pick. /
      boolean coinToss = random.nextDouble() < probability[column];
      /
      Based on the outcome, return either the column or its alias. */
      return coinToss ? column : alias[column];
      }
      }
      另外,stackoverflow上有一个回答,关于程序中掷骰子这个事件的模型改用什么数据结构,无疑可以使用Alias Method,感兴趣的老铁可以自己画画它的概率模型。

另外回答中还有各种千奇百怪的,比如说下面这个,使用平衡二叉搜索树,骰子的每一个面设置一个节点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Protel99seMEX3 的相关文章

随机推荐

  • 磁珠基本原理

    概念 磁珠的全称为铁氧体磁珠滤波器 xff0c 是一种抗干扰元件 xff0c 主要功能是滤除高频噪声 xff0c 消除存在于传输线结构 xff08 电路 xff09 中的噪声 工作原理 磁珠通过阻抗吸收并以发热的形式将不需要频段的能量耗散掉
  • BUCK型DC-DC变换器

    前述 DCDC从控制手段上来说分为PWM式 谐振式以及他们的结合式 每 一种方式中从输入与输出之间是否有变压器隔离又可以分为有隔离 无隔离两类 每一类有六种拓扑结构 BUCK Boost BUCK Boost Cuk Sepic和Zeat
  • dubbo服务超时导致的异常org.apache.dubbo.remoting.TimeoutException

    1 dubbo服务超时异常提示信息如下 xff1a cause org apache dubbo remoting TimeoutException Waiting server side response timeout by scan
  • 基于TCP/IP实现串口到网络的通讯转换

    工作模式 通过串口服务器 xff0c 采集到天平的称量值发送到PC端 操作步骤 1 软件测试 测试工具 xff1a USR M0 V2 2 5 8 基础设置 xff1a 模块静态IP 设置成服务器IP xff0c HTTP服务端口 设置成4
  • 结构体的对其规则以及为什么要对其

    结构体的内存对齐规则以及为什么要对齐 内存对齐规则 span class token number 1 span 第一个成员在与结构体变量偏移量为 span class token number 0 span 的地址处 span class
  • 宏定义参数

    宏定义的参数以逗号 xff08 作为分隔符 span class token macro property span class token directive keyword include span span class token s
  • [STM32]关于环形队列的实现

    在程序中使用环形队列判断接收数据格式 xff0c 避免在中断中处理造成程序响应速度慢的问题 直接贴代码 xff1a LoopRxCommu h ifndef LOOPRXCOMMU H define LOOPRXCOMMU H includ
  • C#旅程——串口发送数据

    串口发送数据时可以一个byte一个byte的发送数据 xff0c 也可以一次性丢出 xff0c 分多次丢出的话会导致一段数据被分成多段发出 xff0c 中间的延时可能会超过2ms xff0c 与FW通讯时会出现异常 span class t
  • 【记录】一次51单片机串口乱码问题排查

    记录 一次51单片机串口乱码问题排查 项目场景问题描述原因分析解决方案结语 项目场景 在51串口收发仿真实验中使用两个单片机互相通信 xff0c 程序设定A上电1s后通过串口以16进制给B发送AA 直到B收到AA后回复BB xff0c 当A
  • IO流java基础

    二十四 IO流 24 1 File 1 1 File 类概述和构造方法 File 它是文件和目录路径名的抽象表示 文件和目录是可以通过File封装成对象的 对于File而言 其封装的并不是一个真正存在的文件 仅仅是一个路径名而已 它可以是存
  • TX2上布置vins_fusion_gpu指南

    1 参考链接 如果初次安装 xff0c 新的TX2环境 xff0c 请参考文档 https github com arjunskumar vins fusion gpu tx2 nano 2 问题记录 1 xff0c 自己的环境情况 我的环
  • Ubuntu下安装cmake

    Ubuntu下安装cmake 今天因为项目的原因需要将cmake升级一下 xff0c 原来我是按照链接没有卸载旧版本 xff0c 直接升级 但是出现一些问题 xff0c 然后我全部卸载后 xff0c 重新安装 以下就是我的安装步骤 第一步
  • AUTH:basic认证和digest认证

    Http authentication BASIC In the context of an HTTP transaction basic access authentication is a method for a web browse
  • Quick Audience组织和工作空间功能解读

    简介 xff1a Quick Audience完成了权限系统全面升级 xff0c 可以解决集团企业不同品牌 不同运营组织 xff0c 不同消费者运营的诉求 xff0c 精细化保障企业数据访问安全 xff0c 提升管控的灵活度 更多关于数智化
  • Socket编程基础总结,全网最全

    IP地址 xff1a 可以在网络环境中 xff0c 唯一标识一台主机 端口号 xff1a 可以定位网络的一台主机上 xff0c 唯一标识一个进程 ip地址 43 端口号 xff1a 可以在网络环境中 xff0c 唯一标识一个进程 在TCP
  • 嵌入式开发--RS-485通讯的问题

    嵌入式开发 RS 485通讯的问题 RS 485说明接口芯片硬件连接CubeMX设置代码编写引脚定义使能串口中断函数发送数据接收数据 有一个问题 xff0c 多收了一个数数据线上的波形问题分析问题解决 RS 485说明 RS 485一般简称
  • UNIX网络编程卷1(第三版)字节排序函数测试

    内存中存储多字节有两种方法 xff0c 即小端字节序和大端字节序 xff0c Ubuntu10 04 是小端字节序 xff0c 网际协议所用的字节序为大端字节序 内存地址增长方向 低序字节 gt 高序字节 小端字节序 高序字节 gt 低序字
  • android非常好的在线视频播放器源码(包含在线音频播放源码)

    一 在线音频播放器 lt xml version 61 34 1 0 34 encoding 61 34 utf 8 34 gt lt LinearLayout xmlns android 61 34 http schemas androi
  • 深入理解任务堆栈

    先来看这一个小函数 xff0c 猜猜他的运行结果 VC6环境 xff1f include lt stdio h gt void b int data 10 printf 34 helloworld r n 34 data 11 61 5 i
  • Protel99seMEX3

    Protel99se的鼠标增强软件 xff0c 可以实现用鼠标放大与缩小电路图 xff0c 十分方便 xff01 文件 xff1a n459 com file 25127180 478161061 以下内容无关 xff1a 分割线 说起Al