Port in a storm

Here’s follow the solution (aimms) to the challenge Port in a Storm (puzzleor).

We need to create the sets docks and groups.

The distances, given in the spreadsheet:

Tip: in the definition, we can fill the matrix with the values.

Another tip: it is easy to exchange indexes. Make sure it does not happen.

The variable has range integer in dimensions (g,d)

Contraints: max docks and the minimum we have to distribute from the groups

Next, the mathematical programming.

We’re minimizing.

FO is the sum of variables times distances.

In the main execution, we write solve port (name of the mathematical programming);

Type F6 to run:

Result is FO = 31, the same as in the excel.

Note that the assignment is different from excel – in this case there’s more than one solution.

The zip file here contains the formulation. And here, the Excel solver version.

Below transcribed the full formulation.

MAIN MODEL Main_Port_in_a_storm

DECLARATION SECTION

SET:

   identifier   :  Docks

   subset of    :  Integers

   index        :  d

   definition   :  {1..3} ;



SET:

   identifier   :  Groups

   subset of    :  Integers

   index        :  g

   definition   :  {1..3} ;



PARAMETER:

   identifier   :  distances

   index domain :  (g,d)

   definition   :  data { ( 1, 1 ) : 1,  ( 1, 2 ) : 3,  ( 1, 3 ) : 2,  ( 2, 1 ) : 2,  ( 2, 2 ) : 2,  ( 2, 3 ) : 1,  ( 3, 1 ) : 3,  ( 3, 2 ) : 2,  ( 3, 3 ) : 1 } ;



PARAMETER:

   identifier   :  max_docks

   index domain :  d

   definition   :  data { 1 : 4,  2 : 7,  3 : 9 } ;



PARAMETER:

   identifier   :  min_groups

   index domain :  (g)

   definition   :  data { 1 : 8,  2 : 5,  3 : 7 } ;



VARIABLE:

   identifier   :  aloc

   index domain :  (g,d)

   range        :  integer ;



VARIABLE:

   identifier   :  fo

   range        :  free

   definition   :  sum((g,d),aloc(g, d)*distances(g, d)) ;



CONSTRAINT:

   identifier   :  c_docks

   index domain :  d

   definition   :  sum(g, aloc(g,d))<=max_docks(d) ;



CONSTRAINT:

   identifier   :  c_groups

   index domain :  (g)

   definition   :  sum(d, aloc(g,d))>=min_groups(g) ;



MATHEMATICAL PROGRAM:

   identifier   :  Port

   objective    :  fo

   direction    :  minimize

   constraints  :  AllConstraints

   variables    :  AllVariables

   type         :  Automatic ;

ENDSECTION ;

PROCEDURE

identifier :  MainInitialization

ENDPROCEDURE ;

PROCEDURE

identifier :  MainExecution

body       : 

  solve port;

ENDPROCEDURE ;

PROCEDURE

identifier :  MainTermination

body       : 

  return DataManagementExit();

ENDPROCEDURE ;

ENDMODEL Main_Port_in_a_storm ;

Ideias técnicas com uma pitada de filosofia:

https://ideiasesquecidas.com/

A simple exercise in AIMMS

I want to model the following Linear program (in the spreadsheet, using solver).

First of all, you’ll need to create a new Project, and one Set for each dimension.

Two dimensions = two sets. Each of this varying from 1 to 2.

We can write the data directly using {1,2} in definition area.

Now, you’ll have to create the arrays of data.

MatrizDados(i,j)

Rhs(i)  !Right hand side

c(j) !Coefficients in the FO

Next, create the variable. In this case, the name given was “var” and the dimension is j.

(You can name the dimensions as you wish, but do not make confusion with these).

Now, the constraint equations.

Note that the index domain is i. There is one equation like this for each line.

Another variable is the objective function:  sum(j,c(j)*var(j));

The mathematical programming is where we indicate the direction, the objective function and the type of problem (LP, MIP).

Last step. In the Main Execution, write down “solve mp;” – it is the command to solve the mathematical program we’ve called mp.

Click F6 to run.

The variable var(j) will contain the results: {12.5, 0} – the same as in the excel solver.

In this link, the aimms model. Perhaps it will not run in an older version of the software.

Regards.

Model Main_Teste01 {

    Set Set1 {

        SubsetOf: Integers;

        Index: i;

        Definition: {

            {1,2}

        }

    }

    Set Set2 {

        SubsetOf: Integers;

        Index: j;

        Definition: {

            {1,2}

        }

    }

   Parameter MatrizDados {

        IndexDomain: (i,j);

        Definition: data { ( 1, 1 ) : 10,  ( 1, 2 ) : 30,  ( 2, 1 ) : 20,  ( 2, 2 ) : 40 };

    }

    Parameter c {

        IndexDomain: j;

        Definition: data { 1 : 12,  2 : 23 };

    }

    Parameter rhs {

        IndexDomain: i;

        Definition: data { 1 : 150,  2 : 250 };

    }

    Variable var {

        IndexDomain: j;

        Range: nonnegative;

    }

    Constraint restr {

        IndexDomain: i;

        Definition: sum(j, var(j)*MatrizDados(i,j))<= rhs(i);

    }

    Variable fo {

        Range: free;

        Definition: sum(j,c(j)*var(j));

    }

    MathematicalProgram mp {

        Objective: fo;

        Direction: minimize;

        Constraints: AllConstraints;

        Variables: AllVariables;

        Type: LP;

    }

    Procedure MainInitialization;

    Procedure MainExecution {

        Body: {

            solve mp;

        }

    }

    Procedure MainTermination {

        Body: {

            return DataManagementExit();

        }

    }

}