Java:值在不应该更新的时候更新

发布于 2024-08-28 19:05:01 字数 2024 浏览 12 评论 0原文

基本上我正在尝试为多维背包问题创建模拟退火的实现。我在让系统决定是否接受较低值的状态时遇到问题。退火是用这个函数控制的:

while (this.temp > 0)
    {
        System.out.println("Temperature: "+this.temp);
        System.out.println("Current bag: "+bagString(currentBag)+" (Value "+problem.getValue(currentBag)+")");
        next = getNext();
        System.out.println("Next bag: "+bagString(next)+" (Value "+problem.getValue(next)+")");
        if (acceptNext(next))
        {
            System.out.println("Accepted");
            this.currentBag = next;
        } else {
            System.out.println("Not accepted");
        }
        this.temp -= this.delta;
    }

acceptNext()函数决定是否接受下一个状态,其定义如下:

public boolean acceptNext(ArrayList<Boolean> next)
{
    if (problem.getValue(next) > problem.getValue(this.currentBag))
    {
        return true;
    } else {
        int loss = (problem.getValue(this.currentBag) - problem.getValue(next));
        double prob = Math.exp(loss/this.temp);
        Random generator = new Random();
        double selection = generator.nextDouble();
        System.out.println("Prob: "+prob+", random number: "+selection);
        if (selection < prob) {
            return true;
        }
        return false;
    }
}

经过一些测试,我发现currentBag字段在acceptNext()之前被分配给下一个值函数被调用。我在任何代码中都找不到另一个“this.currentBag = next”。为了完整起见,这里是 getNext() 函数:

public ArrayList<Boolean> getNext()
{
    Random generator = new Random();
    boolean valid = false;
    ArrayList<Boolean> next = new ArrayList<Boolean>();
    int j;
    while (!valid)
    {
        next = this.currentBag;
        j = generator.nextInt(problem.getNumObjects());
        if (next.get(j) == true)
        {
            next.set(j, false);
        } else {
            next.set(j, true);
        }
        if (problem.isValid(next))
        {
            valid = true;
        }
    }
    return next;
}

我看不出是什么使这个值更新。有人看到代码中的任何内容吗?

谢谢

Basically I'm trying to create an implementation of simulated annealing for the multidimensional knapsack problem. I'm having a problem getting the system to decide whether or not to accept a state with a lower value. The annealing is controlled with this function:

while (this.temp > 0)
    {
        System.out.println("Temperature: "+this.temp);
        System.out.println("Current bag: "+bagString(currentBag)+" (Value "+problem.getValue(currentBag)+")");
        next = getNext();
        System.out.println("Next bag: "+bagString(next)+" (Value "+problem.getValue(next)+")");
        if (acceptNext(next))
        {
            System.out.println("Accepted");
            this.currentBag = next;
        } else {
            System.out.println("Not accepted");
        }
        this.temp -= this.delta;
    }

The acceptNext() function decides whether or not to accept the next state, and is defined thus:

public boolean acceptNext(ArrayList<Boolean> next)
{
    if (problem.getValue(next) > problem.getValue(this.currentBag))
    {
        return true;
    } else {
        int loss = (problem.getValue(this.currentBag) - problem.getValue(next));
        double prob = Math.exp(loss/this.temp);
        Random generator = new Random();
        double selection = generator.nextDouble();
        System.out.println("Prob: "+prob+", random number: "+selection);
        if (selection < prob) {
            return true;
        }
        return false;
    }
}

After doing some testing, I found that the currentBag field is assigned to the next value before the acceptNext() function is called. I can't find another "this.currentBag = next" in any of my code. For the sake of completeness, here is the getNext() function:

public ArrayList<Boolean> getNext()
{
    Random generator = new Random();
    boolean valid = false;
    ArrayList<Boolean> next = new ArrayList<Boolean>();
    int j;
    while (!valid)
    {
        next = this.currentBag;
        j = generator.nextInt(problem.getNumObjects());
        if (next.get(j) == true)
        {
            next.set(j, false);
        } else {
            next.set(j, true);
        }
        if (problem.isValid(next))
        {
            valid = true;
        }
    }
    return next;
}

I can't see what is making this value update. Does anyone see anything in the code?

Thanks

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-09-04 19:05:01

当您执行此操作时,next 指向与当前包相同的内容,因此对 next 的所有更改都会反映在 currentBag 中。
在你的 getNext() 方法中:

while (!valid)
{
    next = this.currentBag;
    ...
}

试试这个:

while (!valid)
{
    next = new ArrayList<Boolean>(this.currentBag);
    ...
}

When you do this, next points to the same thing as current bag, so all changes to next are reflected in currentBag.
In your getNext() method:

while (!valid)
{
    next = this.currentBag;
    ...
}

Try this instead:

while (!valid)
{
    next = new ArrayList<Boolean>(this.currentBag);
    ...
}
倚栏听风 2024-09-04 19:05:01

getNext() 设置 next 引用 currentBag 对象,然后对其执行设置操作。如果要修改 next 的值,则需要复制/克隆 currentBag。

getNext() sets next to reference the currentBag object and then performs the set operation on it. You need to copy/clone currentBag if you want to then modify the value of next.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文