📄 zerooneknapsackproblem.cs
字号:
namespace Opus6
{
using System;
using System.Collections;
using System.Text;
[Version("$Id: ZeroOneKnapsackProblem.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $"), Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng.")]
public class ZeroOneKnapsackProblem
{
public ZeroOneKnapsackProblem(int[] weight, int[] profit, int capacity)
{
this.numberOfItems = weight.Length;
this.weight = weight;
this.profit = profit;
this.capacity = capacity;
}
public Solution Solve(Solver solver)
{
return solver.Solve(new Opus6.ZeroOneKnapsackProblem.Node(this));
}
protected int capacity;
protected int numberOfItems;
protected int[] profit;
protected int[] weight;
protected class Node : Solution
{
public Node(ZeroOneKnapsackProblem problem)
{
this.problem = problem;
this.totalWeight = 0;
this.totalProfit = 0;
this.unplacedProfit = 0;
this.numberPlaced = 0;
this.x = new int[problem.numberOfItems];
for (int num1 = 0; num1 < problem.numberOfItems; num1++)
{
this.unplacedProfit += problem.profit[num1];
}
}
public ZeroOneKnapsackProblem.Node Copy()
{
ZeroOneKnapsackProblem.Node node1 = new ZeroOneKnapsackProblem.Node(this.problem);
node1.totalWeight = this.totalWeight;
node1.totalProfit = this.totalProfit;
node1.numberPlaced = this.numberPlaced;
for (int num1 = 0; num1 < this.problem.numberOfItems; num1++)
{
node1.x[num1] = this.x[num1];
}
node1.unplacedProfit = this.unplacedProfit;
return node1;
}
public void PlaceNext(int value)
{
this.x[this.numberPlaced] = value;
if (value == 1)
{
this.totalWeight += this.problem.weight[this.numberPlaced];
this.totalProfit += this.problem.profit[this.numberPlaced];
this.unplacedProfit -= this.problem.profit[this.numberPlaced];
}
this.numberPlaced++;
}
public override string ToString()
{
bool flag1 = false;
StringBuilder builder1 = new StringBuilder();
for (int num1 = 0; num1 < this.numberPlaced; num1++)
{
if (flag1)
{
builder1.Append(", ");
}
builder1.Append(this.x[num1]);
flag1 = true;
}
builder1.Append(" total weight = " + this.totalWeight);
builder1.Append(" total profit = " + this.totalProfit);
return builder1.ToString();
}
public int Bound
{
get
{
return -(this.totalProfit + this.unplacedProfit);
}
}
public bool IsComplete
{
get
{
return (this.numberPlaced == this.problem.numberOfItems);
}
}
public bool IsFeasible
{
get
{
return (this.totalWeight <= this.problem.capacity);
}
}
public int Objective
{
get
{
return -this.totalProfit;
}
}
public IEnumerable Successors
{
get
{
return new Enumerable(new Opus6.ZeroOneKnapsackProblem.Node.SuccessorEnumerator(this));
}
}
private int numberPlaced;
private ZeroOneKnapsackProblem problem;
private int totalProfit;
private int totalWeight;
private int unplacedProfit;
private int[] x;
public class SuccessorEnumerator : IEnumerator
{
internal SuccessorEnumerator(ZeroOneKnapsackProblem.Node node)
{
this.x = -1;
this.node = node;
}
public bool MoveNext()
{
this.x++;
if (this.x == 2)
{
this.x = -1;
}
return (this.x >= 0);
}
public void Reset()
{
this.x = -1;
}
public object Current
{
get
{
if (this.x < 0)
{
throw new InvalidOperationException();
}
ZeroOneKnapsackProblem.Node node1 = this.node.Copy();
node1.PlaceNext(this.x);
return node1;
}
}
private ZeroOneKnapsackProblem.Node node;
protected int x;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -