在 CPLEX OPL Studio 中创建具有加性标准的数学聚类模型时出现的问题

问题描述 投票:0回答:1

我正在尝试在 CPLEX OPL Studio 中创建一个模型,以便使用附加标准进行聚类,但我有许多错误,我不知道如何正确修复,因为我对 OPL Studio 非常不熟悉 最初有这么一个损失函数来计算与聚类中心的偏差 接下来,我将这些值代入一般损失函数,结果得到以下公式 还有一个计算簇中心的公式

`   // Number of clients, number of features, and number of clusters
   int n = ...; // Number of clients
   int m = ...; // Number of features
   int k = ...; // Number of clusters

   // Client data: feature values for each client
   float data[i in 1..n][j in 1..m] = ...;

   // Binary variables: x[i][c] = 1 if client i is assigned to cluster c
   dvar boolean x[1..n][1..k];

   // Variables for the center of each cluster for each feature
   dvar float mu[1..k][1..m];

   // Model
   minimize
       sum(c in 1..k, i in 1..n, j in 1..m) x[i][c] * (data[i][j] - mu[c][j])^2;

   // Constraints
   subject to {
       // Each client belongs to exactly one cluster
        forall(i in 1..n)
           sum(c in 1..k) x[i][c] == 1;
    
       // Definition of cluster centers
       forall(c in 1..k, j in 1..m)
            mu[c][j] == sum(i in 1..n) x[i][c] * data[i][j] / sum(i in 1..n) x[i][c];
    }`

我尝试为以下公式编写代码,但遇到了语法问题。例如,如下所示: CPLEX(默认)无法解析表达式: forall(c in 1..3, j in 1..4) mu[c][j] == sum(i in 1..5) ( x[ i][c]*data[i][j]) / (sum(i in 1..5) x[i][c]) 可能值得添加更多限制,但我有点困惑

cluster-analysis k-means cplex opl
1个回答
0
投票

在 CPLEX 中我宁愿使用约束规划算法。

using CP;



 // Number of clients, number of features, and number of clusters
   int n = 3; // Number of clients
   int m = 4; // Number of features
   int k = 2; // Number of clusters

   // Client data: feature values for each client
   float data[i in 1..n][j in 1..m] = i*j;

   // Binary variables: x[i][c] = 1 if client i is assigned to cluster c
   dvar boolean x[1..n][1..k];

   // Variables for the center of each cluster for each feature
   
   dexpr float mu[c in 1..k][j in 1..m]=
   sum(i in 1..n) x[i][c] * data[i][j] / sum(i in 1..n) x[i][c];

   // Model
   minimize
       sum(c in 1..k, i in 1..n, j in 1..m) x[i][c] * (data[i][j] - mu[c][j])^2;

   // Constraints
   subject to {
       // Each client belongs to exactly one cluster
        forall(i in 1..n)
           sum(c in 1..k) x[i][c] == 1;
    
       // Definition of cluster centers
//       forall(c in 1..k, j in 1..m)
//            mu[c][j] == sum(i in 1..n) x[i][c] * data[i][j] / sum(i in 1..n) x[i][c];
    }

工作正常

© www.soinside.com 2019 - 2024. All rights reserved.