cplexampl

Can't use fractional variable in AMPL


In my AMPL code, I want to use a variable whose value will be between 0 and 1. It can be any fraction between 0 and 1(0 and 1 inclusive). How can I declare such variable in AMPL and use it? I would be grateful if anyone provide me an example AMPL code for fractional knapsack problem.

I have written this code in AMPL and and tried to run in Neos Server. It is showing me all my variables x[i,j]=0 in the result but it is not the optimal solution. When I am using var x{i in 1..consumer, j in 1..source} binary;, it is giving the optimal result. But in my optimization problem, a consumer can derive energy from multiple sources and can take any fraction from the available energy of the sources. So, my variable must be 0<=x<=1 Can anyone find out what's the problem here? Thanks.

My .mod file is:

param source;
param consumer;
param max_cost;
param max_unit;

param available{j in 1..source};
param demand{i in 1..consumer};
param unit_cost{j in 1..source};
param fi{i in 1..consumer,j in 1..source};


var x{i in 1..consumer, j in 1..source};

minimize total: sum{i in 1..consumer, j in 1..source} ((0.5 * ((unit_cost[j] * x[i,j] * available[j]) / (max_cost * max_unit))) + (0.5 * fi[i,j] * ceil(x[i,j])));
subject to sell{j in 1..source}: sum{i in 1..consumer} x[i,j] <= 1;
subject to limita{i in 1..consumer, j in 1..source}:x[i,j] >=0;
subject to limitb{i in 1..consumer, j in 1..source}: x[i,j] <=1;
subject to dem{i in 1..consumer}: sum{j in 1..source} ( x[i,j] * available[j]) >= demand[i];

My .dat file is:

param source:= 2;
param consumer:= 1;
param max_cost:= 60;
param max_unit:=100;


param available:= 1 10 
               2 50;
               
param unit_cost:= 1 0
                2 20;
          
param fi: 1 2  :=
          1 0.3 0.05;
          

param demand:= 1 30;

Solution

  • instead of

    var x{i in 1..consumer, j in 1..source};
    

    you should write

    var x{i in 1..consumer, j in 1..source}>=0,<=1;