- 浏览: 10430 次
- 性别:
- 来自: 北京
最新评论
-
fangqi2218133:
这个听起来很简单啊,只需要重写spring加载配置项的bean ...
spring远程加载spring配置文件--问题 -
coolboyysy:
你用redis,不如直接访问内存,更高效redis还的磁盘读写 ...
类淘宝橱窗web应用设计1 -
coolboyysy:
模糊的需求,稀缺的经验,坑爹的接口,天真的设计。最终结果,必然 ...
类淘宝橱窗web应用设计问题 -
coolboyysy:
基本概念应该是 观察者模式Observer Pattern至于 ...
类淘宝橱窗web应用设计问题 -
coolboyysy:
首先,你不可能每次用户来访问你的时候 你都用JSP去连接口取数 ...
类淘宝橱窗web应用设计问题
package com.mytest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
class MsgChannel implements Serializable{
private static final long serialVersionUID = 1895082597124213342L;
private String id;
private String name;
private String signName;
private int status;//状态
private int priority;//权重
private Map<String,String> paramMap = new HashMap<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public void addParam(String key,String value){
paramMap.put(key,value);
}
public String getParam(String key){
String s= paramMap.get(key);
return s==null?"":s;
}
public int getParamInt(String key){
String v = paramMap.get(key);
if(StringUtils.isBlank(v)){
return 0;
}
try{
return Integer.parseInt(v);
}catch(Exception e){
return 0;
}
}
public Map<String, String> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
/**
* @return the priority
*/
public int getPriority() {
return priority;
}
/**
* @param priority the priority to set
*/
public void setPriority(int priority) {
this.priority = priority;
}
}
/**
* @author coolboyysy [coolboyysy @126.com]
* @since 15-3-6
*/
public class RandomAverageUtil {
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为4档,[1][2-3][4-6][7-11]随机抽取11内自然数,落在哪个区间内就只该权值的值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则比对,0+1,0+1+2,0+1+2+3,0+1+2+3+5四次,在第四次获取到的即为选中的channel
* @param channels
* @return
*/
public static MsgChannel getSpecialChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
for (MsgChannel channel:channels) {
totalrate = totalrate+channel.getPriority();
}
if(totalrate==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int hitnumber = new Random().nextInt(totalrate);
int tmp_total = 0;
for (MsgChannel channel:channels) {
tmp_total += channel.getPriority();
if (tmp_total >= hitnumber) {
luckChannel = channel;
break;
}
}
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,填入[0-11)内自然数,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得7对应的channel
* @param channels
* @return
*/
public static MsgChannel getLuckyChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
map.put(j, channel);
}
totalrate += channel.getPriority();
}
if(totalrate==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int hitnumber = new Random().nextInt(totalrate);
luckChannel = map.get(hitnumber);
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,在数组内填入[0-11)内自然数,map 中填入对应id的channel,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2+1)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得index为7对应的map中的channel
* @param channels
* @return
*/
public static MsgChannel getLuckChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
int sum = 0;
for (MsgChannel channel:channels) {
sum += channel.getPriority();
}
if(sum==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int[] temp = new int[sum];
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
temp[j]=j;
map.put(j, channel);
}
totalrate += channel.getPriority();
}
int index = (int) (Math.random() * temp.length);
luckChannel = map.get(index);
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,填入[0-11)内自然数为key的map中,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得index为7对应的map中的channel
* 但随机抽取方法是按时间来做,因此,随着系统时间的不同,会有不同的几率表现,在瞬时大规模时候,偏向其中几个。适合随着时间长度增加而进行的随机平均算法
* @param channels
* @return
*/
public static MsgChannel getTimeLuckChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
map.put(j, channel);
}
totalrate += channel.getPriority();
}
if(totalrate>0){
int index = (int) (System.currentTimeMillis()%totalrate)+1;
luckChannel = map.get(index);
}else{
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
}
}
return luckChannel;
}
/**
* 算术除法运算
* @param a
* @param b
* @return
*/
public static String devideNum(int a,int b){
if(b==0){
return String.valueOf(a);
}
BigDecimal bda = new BigDecimal(String.valueOf(a));
BigDecimal bdb = new BigDecimal(String.valueOf(b));
bda.setScale(5,BigDecimal.ROUND_HALF_UP);
bdb.setScale(5,BigDecimal.ROUND_HALF_UP);
BigDecimal res = bda.divide(bdb,5,BigDecimal.ROUND_HALF_UP);
double f =res.setScale(5,BigDecimal.ROUND_HALF_UP).doubleValue();
return String.format("%.2f", f);
}
/**
* Map排序算法
* @param map
*/
public static void sortMap(Map<MsgChannel, Integer> map){
List<Map.Entry<MsgChannel, Integer>> infoIds = new ArrayList<Map.Entry<MsgChannel, Integer>>(map.entrySet());
// 排序
Collections.sort(infoIds, new Comparator<Map.Entry<MsgChannel, Integer>>() {
public int compare(Map.Entry<MsgChannel, Integer> o1,Map.Entry<MsgChannel, Integer> o2) {
return (o1.getValue()-o2.getValue());
}
});
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int total = 0;
List<MsgChannel> channels = new ArrayList<MsgChannel>();
int channelNum = 5;
for (int i = 1; i <=channelNum; i++) {
MsgChannel channel = new MsgChannel();
channel.setId(String.valueOf(i));
channel.setName("第"+i+"个");
channel.setSignName("hello"+i);
channel.setParamMap(new HashMap<String,String>(i));
channel.setPriority( new Random().nextInt(200));
channels.add(channel);
}
for (MsgChannel channel: channels) {
total +=channel.getPriority();
System.out.println("channelid: "+channel.getId()+" 权值: "+channel.getPriority());
}
int loopnum = 1000;
Map<MsgChannel,Integer> hitmap1 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap2 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap3 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap4 = new HashMap<MsgChannel,Integer>();
long start1Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel1 = getSpecialChannel(channels);
if(null!=hitmap1.get(channel1)){
int num = hitmap1.get(channel1);
num =num+1;
hitmap1.put(channel1, num);
}else{
int num =1;
hitmap1.put(channel1, num);
}
}
System.out.println("第一个方法耗时:"+(System.currentTimeMillis()-start1Time)+"毫秒");
long start2Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel2 = getLuckyChannel(channels);
if(null!=hitmap2.get(channel2)){
int num = hitmap2.get(channel2);
num =num+1;
hitmap2.put(channel2, num);
}else{
int num =1;
hitmap2.put(channel2, num);
}
}
System.out.println("第二个方法耗时:"+(System.currentTimeMillis()-start2Time)+"毫秒");
long start3Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel3 = getLuckChannel(channels);
if(null!=hitmap3.get(channel3)){
int num = hitmap3.get(channel3);
num =num+1;
hitmap3.put(channel3, num);
}else{
int num =1;
hitmap3.put(channel3, num);
}
}
System.out.println("第三个方法耗时:"+(System.currentTimeMillis()-start3Time)+"毫秒");
long start4Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
MsgChannel channel4 = getTimeLuckChannel(channels);
if(null!=hitmap4.get(channel4)){
int num = hitmap4.get(channel4);
num =num+1;
hitmap4.put(channel4, num);
}else{
int num =1;
hitmap4.put(channel4, num);
}
}
System.out.println("第四个方法耗时:"+(System.currentTimeMillis()-start4Time)+"毫秒");
sortMap(hitmap1);
sortMap(hitmap2);
sortMap(hitmap3);
sortMap(hitmap4);
long endTime = System.currentTimeMillis();
System.out.println("总循环次数"+loopnum+" 总共耗时:"+(endTime-startTime)+"毫秒");
System.out.println("总权值数目:"+total);
for (MsgChannel channel: channels) {
System.out.println("channelid: "+channel.getId()+" 算术平均几率: "+devideNum(channel.getPriority()*100,total)+"% 权值: "+channel.getPriority());
}
for (MsgChannel channel:hitmap1.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第一个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap1.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap2.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第二个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap2.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap3.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第三个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap3.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap4.keySet()) {
if(null!=channel&&null!=hitmap4.get(channel)){
System.out.println("第四个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap4.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
}
}
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
class MsgChannel implements Serializable{
private static final long serialVersionUID = 1895082597124213342L;
private String id;
private String name;
private String signName;
private int status;//状态
private int priority;//权重
private Map<String,String> paramMap = new HashMap<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public void addParam(String key,String value){
paramMap.put(key,value);
}
public String getParam(String key){
String s= paramMap.get(key);
return s==null?"":s;
}
public int getParamInt(String key){
String v = paramMap.get(key);
if(StringUtils.isBlank(v)){
return 0;
}
try{
return Integer.parseInt(v);
}catch(Exception e){
return 0;
}
}
public Map<String, String> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
/**
* @return the priority
*/
public int getPriority() {
return priority;
}
/**
* @param priority the priority to set
*/
public void setPriority(int priority) {
this.priority = priority;
}
}
/**
* @author coolboyysy [coolboyysy @126.com]
* @since 15-3-6
*/
public class RandomAverageUtil {
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为4档,[1][2-3][4-6][7-11]随机抽取11内自然数,落在哪个区间内就只该权值的值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则比对,0+1,0+1+2,0+1+2+3,0+1+2+3+5四次,在第四次获取到的即为选中的channel
* @param channels
* @return
*/
public static MsgChannel getSpecialChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
for (MsgChannel channel:channels) {
totalrate = totalrate+channel.getPriority();
}
if(totalrate==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int hitnumber = new Random().nextInt(totalrate);
int tmp_total = 0;
for (MsgChannel channel:channels) {
tmp_total += channel.getPriority();
if (tmp_total >= hitnumber) {
luckChannel = channel;
break;
}
}
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,填入[0-11)内自然数,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得7对应的channel
* @param channels
* @return
*/
public static MsgChannel getLuckyChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
map.put(j, channel);
}
totalrate += channel.getPriority();
}
if(totalrate==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int hitnumber = new Random().nextInt(totalrate);
luckChannel = map.get(hitnumber);
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,在数组内填入[0-11)内自然数,map 中填入对应id的channel,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2+1)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得index为7对应的map中的channel
* @param channels
* @return
*/
public static MsgChannel getLuckChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
int sum = 0;
for (MsgChannel channel:channels) {
sum += channel.getPriority();
}
if(sum==0){
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
return luckChannel;
}
int[] temp = new int[sum];
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
temp[j]=j;
map.put(j, channel);
}
totalrate += channel.getPriority();
}
int index = (int) (Math.random() * temp.length);
luckChannel = map.get(index);
}
return luckChannel;
}
/**
* 通过权值来获取渠道id
* 算法: 例如权值为1,2,3,5 则总权值为11 ,分为11档,填入[0-11)内自然数为key的map中,随机取值,随机命中率为1/11,2/11,3/11,5/11。
* 即便是权值均等,此方法也一样适用。
* 特点:时间复杂度为T(n) = O(n^2)
* 使用累计方法获取对应的channel,例如:11的总权值,我随机抽到7,则直接取得index为7对应的map中的channel
* 但随机抽取方法是按时间来做,因此,随着系统时间的不同,会有不同的几率表现,在瞬时大规模时候,偏向其中几个。适合随着时间长度增加而进行的随机平均算法
* @param channels
* @return
*/
public static MsgChannel getTimeLuckChannel(List<MsgChannel> channels){
MsgChannel luckChannel = null;
if(null!=channels&&channels.size()>0){
int totalrate = 0;
Map<Integer,MsgChannel> map = new HashMap<>();
for (MsgChannel channel:channels) {
for (int j = totalrate; j < totalrate+channel.getPriority(); j++) {
map.put(j, channel);
}
totalrate += channel.getPriority();
}
if(totalrate>0){
int index = (int) (System.currentTimeMillis()%totalrate)+1;
luckChannel = map.get(index);
}else{
int index = (int) (Math.random() * channels.size());
luckChannel = channels.get(index);
}
}
return luckChannel;
}
/**
* 算术除法运算
* @param a
* @param b
* @return
*/
public static String devideNum(int a,int b){
if(b==0){
return String.valueOf(a);
}
BigDecimal bda = new BigDecimal(String.valueOf(a));
BigDecimal bdb = new BigDecimal(String.valueOf(b));
bda.setScale(5,BigDecimal.ROUND_HALF_UP);
bdb.setScale(5,BigDecimal.ROUND_HALF_UP);
BigDecimal res = bda.divide(bdb,5,BigDecimal.ROUND_HALF_UP);
double f =res.setScale(5,BigDecimal.ROUND_HALF_UP).doubleValue();
return String.format("%.2f", f);
}
/**
* Map排序算法
* @param map
*/
public static void sortMap(Map<MsgChannel, Integer> map){
List<Map.Entry<MsgChannel, Integer>> infoIds = new ArrayList<Map.Entry<MsgChannel, Integer>>(map.entrySet());
// 排序
Collections.sort(infoIds, new Comparator<Map.Entry<MsgChannel, Integer>>() {
public int compare(Map.Entry<MsgChannel, Integer> o1,Map.Entry<MsgChannel, Integer> o2) {
return (o1.getValue()-o2.getValue());
}
});
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int total = 0;
List<MsgChannel> channels = new ArrayList<MsgChannel>();
int channelNum = 5;
for (int i = 1; i <=channelNum; i++) {
MsgChannel channel = new MsgChannel();
channel.setId(String.valueOf(i));
channel.setName("第"+i+"个");
channel.setSignName("hello"+i);
channel.setParamMap(new HashMap<String,String>(i));
channel.setPriority( new Random().nextInt(200));
channels.add(channel);
}
for (MsgChannel channel: channels) {
total +=channel.getPriority();
System.out.println("channelid: "+channel.getId()+" 权值: "+channel.getPriority());
}
int loopnum = 1000;
Map<MsgChannel,Integer> hitmap1 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap2 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap3 = new HashMap<MsgChannel,Integer>();
Map<MsgChannel,Integer> hitmap4 = new HashMap<MsgChannel,Integer>();
long start1Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel1 = getSpecialChannel(channels);
if(null!=hitmap1.get(channel1)){
int num = hitmap1.get(channel1);
num =num+1;
hitmap1.put(channel1, num);
}else{
int num =1;
hitmap1.put(channel1, num);
}
}
System.out.println("第一个方法耗时:"+(System.currentTimeMillis()-start1Time)+"毫秒");
long start2Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel2 = getLuckyChannel(channels);
if(null!=hitmap2.get(channel2)){
int num = hitmap2.get(channel2);
num =num+1;
hitmap2.put(channel2, num);
}else{
int num =1;
hitmap2.put(channel2, num);
}
}
System.out.println("第二个方法耗时:"+(System.currentTimeMillis()-start2Time)+"毫秒");
long start3Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
MsgChannel channel3 = getLuckChannel(channels);
if(null!=hitmap3.get(channel3)){
int num = hitmap3.get(channel3);
num =num+1;
hitmap3.put(channel3, num);
}else{
int num =1;
hitmap3.put(channel3, num);
}
}
System.out.println("第三个方法耗时:"+(System.currentTimeMillis()-start3Time)+"毫秒");
long start4Time = System.currentTimeMillis();
for (int i = 0; i < loopnum; i++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
MsgChannel channel4 = getTimeLuckChannel(channels);
if(null!=hitmap4.get(channel4)){
int num = hitmap4.get(channel4);
num =num+1;
hitmap4.put(channel4, num);
}else{
int num =1;
hitmap4.put(channel4, num);
}
}
System.out.println("第四个方法耗时:"+(System.currentTimeMillis()-start4Time)+"毫秒");
sortMap(hitmap1);
sortMap(hitmap2);
sortMap(hitmap3);
sortMap(hitmap4);
long endTime = System.currentTimeMillis();
System.out.println("总循环次数"+loopnum+" 总共耗时:"+(endTime-startTime)+"毫秒");
System.out.println("总权值数目:"+total);
for (MsgChannel channel: channels) {
System.out.println("channelid: "+channel.getId()+" 算术平均几率: "+devideNum(channel.getPriority()*100,total)+"% 权值: "+channel.getPriority());
}
for (MsgChannel channel:hitmap1.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第一个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap1.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap2.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第二个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap2.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap3.keySet()) {
if(null!=channel&&null!=hitmap3.get(channel)){
System.out.println("第三个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap3.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
for (MsgChannel channel:hitmap4.keySet()) {
if(null!=channel&&null!=hitmap4.get(channel)){
System.out.println("第四个方法: channelid: "+channel.getId()+" 几率: "+devideNum(hitmap4.get(channel)*100,loopnum)+"% 权值: "+channel.getPriority());
}
}
}
}
发表评论
-
spring远程加载spring配置文件--问题
2016-06-14 10:17 3227现在正在做动态加载spring远程配置文件的事情。 具体是需要 ... -
Javascript跨域访问解决方案
2014-07-19 14:08 693由于安全方面的考虑,Javascript被限制了跨域访问的能力 ... -
ZOOKEEPER 学习笔记
2014-01-28 16:57 01:zookeeper 干嘛的? ... -
类淘宝橱窗web应用设计
2013-10-28 15:58 0类淘宝橱窗web应用设计2 项目终于在大家共同努力下完成了。 ... -
类淘宝橱窗web应用设计1
2013-08-27 10:42 505续http://coolboyysy.iteye.com/ad ... -
类淘宝橱窗web应用设计问题
2013-08-19 23:11 1127需求: 1 设计实现类淘宝橱窗web应用并涵盖流量控制系统。 ...
相关推荐
具体而言,PSO算法将网络的权重和中心点作为粒子的坐标,通过迭代更新粒子的速度和位置,来寻找使得网络预测误差最小化的权重配置。这一过程包括以下几个步骤: 1. 初始化:随机生成粒子群,每个粒子对应一组RBF...
为提高针对自适应隐写算法的检测率,通过改进SRM算法,利用不同区域的像素对隐写检测贡献的差异性,提出了一种基于权值分配的隐写分析算法。理论证明了权值分配能够提高隐写检测特征的分类能力,并设计了一种基于...
该算法的核心思想在于利用无线传感器网络节点的特性,结合先进的数据融合理论,通过双重权值优化技术,融合来自多个传感器节点的数据,以实现目标的精确定位。双重权值优化涉及两个阶段:首先是利用最小均方误差...
遗传算法模仿生物进化过程中的“适者生存”原则,通过生成初始种群,然后进行选择、交叉和变异操作来迭代改进解的质量。在神经网络权值优化中,每个个体代表一组权重值,适应度函数衡量了这组权重在特定任务上的表现...
总之,动态改变权值的简化粒子群算法是对传统PSO的一种有效改进,它通过动态调整惯性权重策略,成功地平衡了全局搜索和局部优化,对于解决复杂的优化问题具有很大的潜力和应用价值。这一方法对于未来优化算法的研究...
总的来说,这种改进的线性递减权值的粒子群优化算法通过增强粒子多样性,有效解决了早熟收敛问题,提升了全局优化能力,对于需要全局优化的复杂问题具有较好的适用性。未来的研究可以进一步探索更有效的早熟停滞判断...
总的来说,LMS算法通过迭代优化权值来适应输入信号,加权计算和对输入信号相关性的利用是其核心特点。在实际应用中,我们需要合理选择学习率μ,以达到快速且稳定的收敛性能。同时,理解并分析输入信号的统计特性,...
PSO是一种模拟鸟类群体捕食行为的全局优化算法,每个解(粒子)在搜索空间中移动,通过追踪自身和群体的最佳解(pbest和gbest)来更新速度和位置,从而逐步接近全局最优解。 在应用PSO于不完全判断矩阵权值计算的...
他们通过引入复合惯性权值和收缩因子,旨在优化粒子群的行为,使之在问题求解过程中能更有效地平衡全局搜索与局部搜索,同时提高算法的收敛速度和精度。 具体而言,他们设计了一种复合惯性权值,该权值结合了线性...
权值优化是指通过某种策略或算法来调整每个粒子的权重,以便更准确地逼近真实状态的概率分布,从而提高滤波精度。 描述部分提到:“通过引入赋值密度函数、边缘密度函数等概念给出了连续值命题逻辑系统中公式概率真...
为解决在Web集群负载均衡算法中预先指定权值来评估服务器节点综合负载不能体现各负载指标动态变化情况的问题,提出一种自适应负载指标权值的负载均衡算法。根据服务器节点各负载指标的实际观测值动态调整各负载指标的...
标题中的“LMS.rar_LMS权值_lms_自适应LMS算法_自适应权值_自适应算法”指的是一个关于线性预测编码(Linear Minimum Mean Square Error, 简称LMS)算法的资源包,其中包含了LMS算法的核心——自适应权值的计算。...
算法通过场境建模,设定三类基准点并平均分布在建模场景中;获取设定场境内不同定位标签的RSSI向量,根据判定规则确定基准点,再运用室内传播模型计算移动权值,估算待测终端的位置信息。通过真实场景实验对比分析,...
Matlab遗传算法优化RBF网络权值-遗传算法优化RBF.rar 遗传算法优化RBF网络权值,可以运行出结果。 本人刚刚学习优化算法这一类知识,希望能有人多交流。希望能有优化算法的创新 我的邮箱 zb078@163.com
总结来看,基于权值的引力搜索算法在电力系统最优潮流计算中的应用研究,不仅推动了电力系统优化领域理论的发展,也为电力系统实际工程提供了宝贵的实践经验。它的出现,为电力工程师和研究人员在面对复杂电力网络...
本文将深入探讨“RBF网络(Radial Basis Function Network)”的权值优化过程,以及如何利用“量子粒子群优化算法”来改进这一过程。 RBF网络是一种前馈神经网络,其主要特点是采用径向基函数作为隐藏层神经元的...
总结来说,基于动态改变惯性权值的粒子群算法是对传统PSO的一种改进,它通过更智能的权值调整策略增强了算法的全局搜索和局部搜索能力,提升了优化效率。这一研究对于理解群智能算法的行为以及优化问题的求解具有...