凑数算法

今天yy和我提了一个现实问题。
因为他们公司财务对账需要,需要实现一个凑数的工具。
具体的需求基本就是:
财务出总帐。供应商这里有若干材料的单价。需要确定这若干材料的数量,使得总价能和财务账面一致。部分材料有约束条件,比如数量不能超过某个数。单价因为也是经过计算的,所以是小数点后有4位。

凑数算法的实现,应该有很多种方法。
其中最简单的一个就是穷举。穷举排列组合。穷举本身的实现不难。
这篇也就主要说说穷举。应该还有更好的算法。
穷举的缺点就是慢。数量是几何级扩展。和材料种类以及个数成正比。
计算总量是n1*n2*....nn
测试了一下以下代码的实际的工作效率,在jre下,一台普通的开发机器可以每秒验证1000万个组合。速度并不算慢。在jdk server模式下,差不多可以快4倍。每秒在4000万条左右。不过在实际的应用面前还是杯水车薪。假设有7种材料,每种约束区间是0-100,实际的组合约为100的7次方,即100万亿。这个数量级别,我估计就算是银河也要跑好一阵子了。


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<Integer,Integer> resultMap = new HashMap<Integer,Integer>();

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<String> lineList = null;
try {
lineList = (List<String>)FileUtils.readLines(paramFile, "UTF-8");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

List<Double> priceList = new ArrayList<Double>();
List<Integer> lowerList = new ArrayList<Integer>();
List<Integer> upperList = new ArrayList<Integer>();

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<Double> priceList, final List<Integer> lowerList, final List<Integer> upperList, int indexNum) {
double currentPrice = 0;
for(int i = lowerList.get(indexNum); i <upperList.get(indexNum); i++) {
caltimes++;
if(caltimes%1000000000 == 0) {
System.out.println(caltimes);
}
if(caltimes == Long.MAX_VALUE - 1) {
caltimes = 0;
}

currentPrice = startPrice + i*priceList.get(indexNum);
if(currentPrice == targetPrice) {
System.out.println(indexNum + 1 + "---" + priceList.get(indexNum) + "---" + i);
resultMap.put(indexNum, i);
return true;
}
if(currentPrice > 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<Double> priceList, List<Integer> lowerList,final List<Integer> upperList, double targetValue) {
System.out.println("If cal able ...");
StringBuffer formular = new StringBuffer("Lower = 0");
double calValue = 0d;
for(int i=0; i<priceList.size(); i++) {
formular.append("+" + lowerList.get(i) + "*" + priceList.get(i));
calValue = calValue + lowerList.get(i)*priceList.get(i);
}
formular.append(" = " + calValue);
formular.append(" < " + targetValue);

System.out.println(formular);
System.out.println("Verify result = " + Boolean.valueOf(calValue < targetValue));

calValue = 0d;
formular = new StringBuffer("Upper = 0");
for(int i=0; i<priceList.size(); i++) {
formular.append("+" + upperList.get(i) + "*" + priceList.get(i));
calValue = calValue + upperList.get(i)*priceList.get(i);
}
formular.append(" = " + calValue);
formular.append(" > " + targetValue);

System.out.println(formular);

System.out.println("Verify result = " + Boolean.valueOf(calValue > targetValue));
}

private static void verify(Map<Integer,Integer> resultMap, List<Double> priceList, double targetValue) {
System.out.println("Verify ...");
StringBuffer formular = new StringBuffer("0");
double calValue = 0d;
for(Entry<Integer,Integer> 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));
}

}



有没有办法在穷举上做一些优化呢?事实上是有的。事实上,实际使用中也是这么做的。
其实可以看到,有一些计算结果会和实际偏离很大,要么太小,要么太大。
isCalable方法其实就是想要表明这个问题。
如果上限的组合总价和目标总价距离很近,则说明事实上,每种材料的取材数量就接近上限,压根没有必要以下限进行计算。反之,距离下限很近,则可能每种材料数量都很小。所以,实际的材料数量取值区间,可以参照上下限的总价,以及目标总价的比值,进行一个优化调整,这样实际计算的数量就会大大减少。

除了穷举法之外,还有没有更好的方法呢? 应该是有的。代码还没有去写。不过提供一个思路。其实上,这个有点类似于a1x1 + a2x2 + ...+anxn = y的线程方程求解。可以对方程做初等变换,得到x1的表达式。令x1等于一个确定的值。再做初等变换。可以得到x2的表达式。最后,如果xn是非负整数,则结果成立。反正,从新拟推x1的值。以此类推。这种算法的好处就是可以避免盲目的穷举。效率应该会更高。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值