ITeye博客

凑数算法

AllenZhang · 2011-01-13 15:38 · 编程语言 · 算法 · Apache · JDK · Markdown

今天yy和我提了一个现实问题。 因为他们公司财务对账需要,需要实现一个凑数的工具。 具体的需求基本就是: 财务出总帐。供应商这里有若干材料的单价。需要确定这若干材料的数量,使得总价能和财务账面一致。部分材料有约束条件,比如数量不能超过某个数。单价因为也是经过计算的,所以是小数点后有4位。 凑数算法的实现,应该有很多种方法。 其中最简单的一个就是穷举。穷举排列组合。穷举本身的实现不难。 这篇也就主要说说穷举。应该还有更好的算法。 穷举的缺点就是慢。数量是几何级扩展。和材料种类以及个数成正比。 计算总量是n1*n2*....nn 测试了一下以下代码的实际的工作效率,在jre下,一台普通的开发机器可以每秒验证1000万个组合。速度并不算慢。在jdk server模式下,差不多可以快4倍。每秒在4000万条左右。不过在实际的应用面前还是杯水车薪。假设有7种材料,每种约束区间是0-100,实际的组合约为100的7次方,即100万亿。这个数量级别,我估计就算是银河也要跑好一阵子了。 [code="java"] package com.allensoft.math; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.io.FileUtils; public class CalNummber { private static final int MAX_NUM = 100; private static final int START_NUM = 1; private static long caltimes = 0l; private static Map resultMap = new HashMap(); public static void main(String[] args) { //String filename = CalNummber.class.getClassLoader().getResource("/").getPath() + "number"; String filename = "number"; if(null != args && args.length > 0) { filename = args[0]; } else { filename = CalNummber.class.getResource("/").getPath() + "number"; } //System.out.println(CalNummber.class.getResource("").getPath() + "number"); File paramFile = new File(filename); List lineList = null; try { lineList = (List)FileUtils.readLines(paramFile, "UTF-8"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } List priceList = new ArrayList(); List lowerList = new ArrayList(); List upperList = new ArrayList(); double result = 0d; if(null != lineList) { for(String line : lineList) { if(line.startsWith("result")) { String[] temp = line.split("="); result = Double.parseDouble(temp[1]); } else { String[] temp = line.split(","); if(temp.length == 3) { priceList.add(Double.valueOf(temp[0])); lowerList.add(Integer.valueOf(temp[1])); upperList.add(Integer.valueOf(temp[2])); } else { priceList.add(Double.valueOf(line)); lowerList.add(Integer.valueOf(START_NUM)); upperList.add(Integer.valueOf(MAX_NUM)); } } } } isCalable(priceList, lowerList, upperList, result); long current = System.currentTimeMillis(); try{ if(cal(0d,result,priceList,lowerList,upperList,0)) { System.out.println(caltimes); System.out.println("Cal take " + (System.currentTimeMillis() - current) + " ms"); verify(resultMap,priceList,result); } else { System.out.println("no result found"); } } catch(Exception e) { e.printStackTrace(); } } private static boolean cal(Double startPrice, Double targetPrice, List priceList, final List lowerList, final List upperList, int indexNum) { double currentPrice = 0; for(int i = lowerList.get(indexNum); i targetPrice) { if(indexNum == priceList.size() -1) { //System.out.println((indexNum + 1) + "---" + priceList.get(indexNum) + "---" + actualNumber); return false; } else { if(cal(startPrice, targetPrice, priceList,lowerList,upperList, indexNum + 1)){ System.out.println((indexNum + 1) + "---" + priceList.get(indexNum) + "---" + (i-1)); resultMap.put(indexNum, (i-1)); return true; } continue; } } else { if(indexNum == priceList.size() -1) { continue; } else { if(cal(currentPrice, targetPrice, priceList, lowerList,upperList, indexNum + 1)){ System.out.println((indexNum + 1) + "---" + priceList.get(indexNum) + "---" + i); resultMap.put(indexNum, i); return true; } continue; } } } return false; } private static void isCalable(List priceList, List lowerList,final List upperList, double targetValue) { System.out.println("If cal able ..."); StringBuffer formular = new StringBuffer("Lower = 0"); double calValue = 0d; for(int i=0; i " + targetValue); System.out.println(formular); System.out.println("Verify result = " + Boolean.valueOf(calValue > targetValue)); } private static void verify(Map resultMap, List priceList, double targetValue) { System.out.println("Verify ..."); StringBuffer formular = new StringBuffer("0"); double calValue = 0d; for(Entry entry : resultMap.entrySet()) { formular.append("+" + entry.getValue() + "*" + priceList.get(entry.getKey())); calValue = calValue + entry.getValue()*priceList.get(entry.getKey()); } formular.append(" = " + calValue); System.out.println(formular); System.out.println("Verify result = " + Boolean.valueOf(calValue == targetValue)); } } [/code] 有没有办法在穷举上做一些优化呢?事实上是有的。事实上,实际使用中也是这么做的。 其实可以看到,有一些计算结果会和实际偏离很大,要么太小,要么太大。 isCalable方法其实就是想要表明这个问题。 如果上限的组合总价和目标总价距离很近,则说明事实上,每种材料的取材数量就接近上限,压根没有必要以下限进行计算。反之,距离下限很近,则可能每种材料数量都很小。所以,实际的材料数量取值区间,可以参照上下限的总价,以及目标总价的比值,进行一个优化调整,这样实际计算的数量就会大大减少。 除了穷举法之外,还有没有更好的方法呢? 应该是有的。代码还没有去写。不过提供一个思路。其实上,这个有点类似于a1x1 + a2x2 + ...+anxn = y的线程方程求解。可以对方程做初等变换,得到x1的表达式。令x1等于一个确定的值。再做初等变换。可以得到x2的表达式。最后,如果xn是非负整数,则结果成立。反正,从新拟推x1的值。以此类推。这种算法的好处就是可以避免盲目的穷举。效率应该会更高。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。