【问题标题】:MiniZinc CSP into JSON - iteration in arrays workaround javascriptMiniZinc CSP 到 JSON - 数组中的迭代解决方法 javascript
【发布时间】:2018-10-22 15:18:32
【问题描述】:

我使用 node.js 模块“治理 CSP 工具”来解决 CSP 问题。 按照我应该如何从 CSP 模型模式 (https://www.npmjs.com/package/governify-csp-tools) 中定义数组的说明,我尝试了 JSON 中的多种表示形式,但仍然出现错误:

数组:Ingredient_1= [30 , 30 , 30, 15, 15,15, 5 ,
5, 5, 1]; ^ 错误:语法错误,意外 ':',期待 [

我的javascript代码是:

var Reasoner = require("governify-csp-tools").Reasoner;



var cspModel = {
    "parameters":[

      {
          "id":"x",
          "type":"int",
          "value":"0"
      },
      {
          "id":"y",
          "type":"int",
          "value":"7"
      },
      {
          "id":"z",
          "type":"int",
          "value":"0"
      },
      {
          "id":"k",
          "type":"int",
          "value":"4"
      },
      { 
          "id":"Ingredient_1",
          "type":"array",

          "value":"[30 ,    30 ,    30, 15, 15,15,  5 , 5 , 5 , 1]",
      },
      {
          "id":"Ingredient_2",
          "type":"array",
          "value":"[3 , 7 , 12, 3 , 7 , 12, 3 , 7 , 12, 3 ]"
      },
      {
          "id":"Ingredient_3",
          "type":"array",

          "value":"[0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ]"
      },
      {
          "id":"Ingredient_4",
          "type":"array",
          "value":"[0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ]"
      },
      ],
    "variables": [
      {
        "id": "occur",
        "type": "int",
        "range": {
          "min": "1",
          "max": "10"
        }

      }

    ],
    "constraints": [
      {
        "id": "C1",
        "expression": "x == 0 -> forall (i in occur) (Ingredient_4 [i]= 0)"
      },
      {
        "id": "C2",
        "expression": "y=7 \\/ y=6 -> forall (i in occur)(Ingredient_1 [i]=30 )"
      },
      {
          "id": "C3",
          "expression": "y==1 -> forall (i in occur)(Ingredient_1 [i]=0 )"
        },
      {
          "id": "C4",
          "expression": "z==5 \\/ z==6 \\/ z==7  ->forall (i in occur)(Ingredient_4[i] !=0) "
      },
      {
          "id": "C5",
          "expression": "k==7 \\/ k==6 -> forall (i in occur)(Ingredient_2 [i] =12)"
      },
      {
          "id": "C6",
          "expression": "k==5 -> forall (i in occur)(Ingredient_2 [i] =7)"
      },
      {
          "id": "C7",
          "expression": "k==4 \\/ k==3  -> forall (i in occur)(Ingredient_2 [i] !=0)"
      },
    ],
    "goal": "satisfy"
  };

  // Configure the CSP reasoner
  var reasoner = new Reasoner({
      type: 'local', // type value also can be 'api' or 'docker'
      folder: 'csp_files' // name of the folder which stores .mzn, .fzn and .ozn temporary files

  });


  console.log("solving model");
  // Solve CSP
  reasoner.solve(cspModel, (err, stdout, stderr, isSatisfiable) => {

      if (err) {
          // manage error
          console.log("model error");

      } else {

          console.log(stdout);
          console.log(isSatisfiable);

      }
      console.log("model solved");

  });

是否有任何解决方法我可以在 JSON 模式之外定义数组,然后在 JSON 模式中再次调用它以在需要的地方进行迭代,例如:

"id": "C2",
        "expression": "y=7 \\/ y=6 -> forall (i in occur)(Ingredient_1 [i]=30 )"

我尝试翻译成 JSON 的可执行 .mzn 表示是:

int: x = 0; % x-parameter
int: y = 7; % y-parameter
int: z = 0; % z-parameter
int: k = 4; % k-parameter
array[1..10] of int: Ingredient_1 = [30 ,   30 ,    30, 15, 15,15,  5 , 5 , 5 , 1]; % Ingredient_1-parameter
array [1..10] of int : Ingredient_2 = [3 ,  7 , 12, 3 , 7 , 12, 3 , 7 , 12, 3 ]; % Ingredient_2-parameter
array [1..10] of int: Ingredient_3 = [0   , 0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ]; % Ingredient_3-parameter
array[1..10] of int: Ingredient_4 = [0   ,  0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ]; % Ingredient_4-parameter
var set of  1..10: occur; % occur-variable
constraint x == 0 -> forall (i in occur) (Ingredient_4 [i]= 0); % C1-constraint
constraint y=7 \/ y=6 -> forall (i in occur)(Ingredient_1 [i]=30 ); % C2-constraint
constraint y==1 -> forall (i in occur)(Ingredient_1 [i]=0 ); % C3-constraint
constraint z==5 \/ z==6 \/ z==7  ->forall (i in occur)(Ingredient_4[i] !=0) ; % C4-constraint
constraint k==7 \/ k==6 -> forall (i in occur)(Ingredient_2 [i] =12); % C5-constraint
constraint k==5 -> forall (i in occur)(Ingredient_2 [i] =7); % C6-constraint
constraint k==4 \/ k==3  -> forall (i in occur)(Ingredient_2 [i] !=0); % C7-constraint
solve satisfy; % goal

问题是遵循在 YAML 上表达的 JSON 模式,特别是这个:

标题:“CSP 模型 JSON 架构”类型:“对象”属性:
参数: 类型:'数组' 项目: 类型:'对象' 特性: 编号:

来自https://www.npmjs.com/package/governify-csp-tools,我如何在 JSON 中表示:

array[1..10] of int: Ingredient_1 = [30 ,   30 ,    30, 15, 15,15,  5 , 5 , 5 , 1];

【问题讨论】:

    标签: javascript arrays node.js json minizinc


    【解决方案1】:

    虽然我对您使用的 javascript 库没有经验。似乎它在生成的 MiniZinc 模型中使用了字段type,没有进行任何转换。

    正如您的 MiniZinc 模型所建议的,声明数组的正确方法是:

    array[1..10] of int: Ingredient_1 = [30, 30, 30, 15, 15, 15, 5, 5, 5, 1];
    

    但是,生成的 MiniZinc 模型包含:

    array: Ingredient_1 = [30, 30, 30, 15, 15, 15, 5, 5, 5, 1];
    

    这表明type字段不应包含array,而应包含array[1..10] of int

    (JSON格式的其他数组可能会出现类似的问题)

    【讨论】:

    • 感谢您的评论!我相信这也是问题所在,但还没有找到将其表示为 JSON 以便获得正确输出的方法:array[1..10] of int: Ingredient_1 = [30, 30, 30, 15, 15 , 15, 5, 5, 5, 1];我想到的另一种解决方法是用 int 来表示问题,而不是用数组来消除这些错误,(似乎更容易)但是 Gecode 求解器似乎没有解决它,所以我必须找到一个解决方案用那个 JSON 表示。
    【解决方案2】:

    我换了这个

    "type":"array"
    

    有了这个

    "type":"array [1..10] of int"
    

    它生成了正确的.mzn

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2015-07-11
      • 2012-07-09
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多