Syntax error in for loop

Hello there!

I’m trying to solve the multiobjective knapsack problem using the weighted sum method, but I have problem with the for bucle, I get the error “Syntax error”, but I can’t notice the reason.

I suspect that the error is related to the printf function and the variable “f”. I need the for bucle for getting the Pareto front.

Note: the model works without the for bucle, evaluating w[1] and w[2] manually.

Could someone help me, please? These are my mod and dat files:

Mod:

reset;
model;

#definir un conjunto de elementos, indices o articulos
param M >=0 integer; # Cantidad de funciones objetivo
param O >=0 integer; # Cantidad de elementos
param N >=0 integer; # Cantidad de puntos a generar de w

set Elementos:= 1..O;
set Funciones_Objetivo:= 1..M;

var x{Elementos} binary; #variables de decisión

#variables FO
var f{Funciones_Objetivo};

param w{Funciones_Objetivo};
param ws{1..N, Funciones_Objetivo};
param b{Funciones_Objetivo, Elementos};

param p{Funciones_Objetivo, Elementos};
param C{Funciones_Objetivo};

maximize F: #max el beneficio
	sum{i in Funciones_Objetivo} w[i] * f[i];

	
s.t. rf{i in Funciones_Objetivo}:
	f[i] = sum{j in Elementos} b[i,j] * x[j];
		
s.t. rC{i in Funciones_Objetivo}:
	sum{j in Elementos} (p[i,j] * x[j]) <= C[i];
	
data datosMochilaMulti.dat;
#let w[1]:=1;
#let w[2]:=0;
option solver highs;

#solve;
#display f;

for {p in 1..N}{#cantidad de vectores peso (w)
	let w[1]:=ws[p,1];
	let w[2]:=ws[p,2];
	solve F;
	
	printf "[%d, %d], \n", f >resultado.txt;
}

Dat:

param N:=100;
param M:= 2; # Cantidad de funciones objetivo
param O:= 20; # Cantidad de elementos


param C:=
1 563.5
2 662;


param b:=
1 1 63
1 2 28
1 3 24
1 4 85
1 5 53
1 6 94
1 7 44
1 8 67
1 9 59
1 10 64
1 11 56
1 12 100
1 13 35
1 14 26
1 15 63
1 16 19
1 17 83
1 18 31
1 19 30
1 20 31
2 1 78
2 2 70
2 3 97
2 4 67
2 5 67
2 6 65
2 7 53
2 8 100
2 9 36
2 10 11
2 11 38
2 12 60
2 13 66
2 14 85
2 15 97
2 16 20
2 17 60
2 18 75
2 19 47
2 20 61;


param p:=
1 1 67
1 2 32
1 3 92
1 4 85
1 5 50
1 6 70
1 7 36
1 8 85
1 9 67
1 10 13
1 11 84
1 12 22
1 13 60
1 14 52
1 15 95
1 16 12
1 17 18
1 18 59
1 19 35
1 20 93
2 1 89
2 2 97
2 3 16
2 4 20
2 5 68
2 6 56
2 7 91
2 8 78
2 9 67
2 10 88
2 11 53
2 12 70
2 13 90
2 14 55
2 15 95
2 16 45
2 17 64
2 18 78
2 19 39
2 20 65;

The outfile is expected to have an structure similar to this one, where the first value corresponds to the variable f[1] and the second value is the variable f[2] value:

[724.000000, 580.000000], 
[698.000000, 600.000000], 
[694.000000, 620.000000], 
[694.000000, 640.000000], 
[682.000000, 660.000000], 
[679.000000, 680.000000], 
[670.000000, 700.000000], 
[654.000000, 720.000000], 
[623.000000, 740.000000], 

Thank you!

In an AMPL printf statement, you cannot refer to f by itself; it needs to have an index in [] after it. Since f is indexed over 1..M in your model and M is set to 2 in your data, you could write

printf "[%d, %d], \n", f[1], f[2] >resultado.txt;

If M could be any positive value, then the output could be written more generally using 3 printf statements:

   printf "[" >resultado.txt;
   printf {i in 1..M-1}: "%d, ", f[i] >resultado.txt;
   printf "%d], \n", f[M] >resultado.txt;

Hello.

It didn’t work, so I used the AMPL API for python. Thank you