IBM CPLEX在目标函数中使用XOR

发布于 2025-02-06 14:06:56 字数 404 浏览 2 评论 0原文

我想建模 max-cut 图形的问题。

当您需要选择顶点子集时,我的方法是用布尔决策变量编码每个顶点。

我的问题是目标函数:Edge {a,b}如果其一个顶点之一在子集中,而另一个位于另一个顶点,则确切地在剪切中,这是逻辑 xor

我看不到目标函数中的“ 1 if xor(a,b)else 0”的方法。方法是否应该完全不同?

I want to model the Max-Cut problem for graphs.

As you need to select a subset of the vertices, my approach is to encode each vertex with a boolean decision variable.

My problem is the objective function: And edge {a,b} is in the cut exactly if one of its vertices is in the subset while the other is not, which is the logical XOR.

I don't see a way how to include "1 if XOR(a,b) else 0" in the objective function. Should the approach be entirely different?

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

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

发布评论

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

评论(2

傾城如夢未必闌珊 2025-02-13 14:06:56

z = x xor y可以写为线性不等式系统:

  z ≤ x+y
  z ≥ x-y
  z ≥ y-x
  z ≤ 2-x-y 

z = x xor y can be written as a system of linear inequalities:

  z ≤ x+y
  z ≥ x-y
  z ≥ y-x
  z ≤ 2-x-y 
谈情不如逗狗 2025-02-13 14:06:56

在所有API中,您可以使用逻辑构造,然后写XOR。

例如,在OPL中:

dvar boolean x;
dvar boolean y;
dvar boolean xxory;

subject to
{
  xxory==(x+y==1);
  xxory==1;
}

要从MaxCut开始,您可以从 maxcut示例< /a> in 如何使用OPL

int n=400;
range r=1..n;

// Random graph  
float edge_prob=0.5;
int  weight_range=10;
int big=100000;
 
tuple t
{
  int i;
  int j;
}

{t} s={<i,j> | ordered i,j in r};

int w[i in r][j in r]=(i<=j)?((rand(big)<=big*edge_prob)?rand(weight_range):0):0;

// end of random graph

//int n=4;
//range r=1..n;
//float w[r][r]=
//
//[[ 0. , 8. ,-9. , 0.],
// [ 8. , 0. , 7. , 9.],
// [-9. , 7.  ,0., -8.],
// [ 0. , 9., -8. , 0.]];

assert card(s)==n*(n-1) div 2;

 // x is the unknown and 0 or 1 means in one or the other side of the fence
 dvar boolean x[r];
 
 dexpr float obj=2*sum(<i,j> in s) w[i][j]*x[i]*(1-x[j]);
 
 maximize obj;
 
 subject to
 {
   
 }
 
 {int} x1={i| i in r:x[i]==1};
 
 execute
 {
   writeln("objective = ",obj);
   writeln("x set to 1 : ",x1);
 }

In CPLEX with all APIs you can use logical constaints and then write xor.

For example in OPL:

dvar boolean x;
dvar boolean y;
dvar boolean xxory;

subject to
{
  xxory==(x+y==1);
  xxory==1;
}

To start with maxcut, you can start with the maxcut example in how to with OPL ?

int n=400;
range r=1..n;

// Random graph  
float edge_prob=0.5;
int  weight_range=10;
int big=100000;
 
tuple t
{
  int i;
  int j;
}

{t} s={<i,j> | ordered i,j in r};

int w[i in r][j in r]=(i<=j)?((rand(big)<=big*edge_prob)?rand(weight_range):0):0;

// end of random graph

//int n=4;
//range r=1..n;
//float w[r][r]=
//
//[[ 0. , 8. ,-9. , 0.],
// [ 8. , 0. , 7. , 9.],
// [-9. , 7.  ,0., -8.],
// [ 0. , 9., -8. , 0.]];

assert card(s)==n*(n-1) div 2;

 // x is the unknown and 0 or 1 means in one or the other side of the fence
 dvar boolean x[r];
 
 dexpr float obj=2*sum(<i,j> in s) w[i][j]*x[i]*(1-x[j]);
 
 maximize obj;
 
 subject to
 {
   
 }
 
 {int} x1={i| i in r:x[i]==1};
 
 execute
 {
   writeln("objective = ",obj);
   writeln("x set to 1 : ",x1);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文