Skip to main content
Solved

Create a FUNCTION with multiple input and output.

  • August 25, 2024
  • 3 replies
  • 34 views

Forum|alt.badge.img+2

Hi! Does anyone know how to create a function with multiple inputs and outputs?

For example, there is a function edited with MATLAB that :“function [ dP,dQ,Pi,Qi ] = Unbalanced( n,m,P,Q,U,G,B,cita )”. How to model this function in AIMMS? Thank you very much!
 

Best answer by Chris Kuip

Yet another alternative is to return a vector instead of a scalar.

 

Let the set s_a be defined as { dp, dq, pi, qi } and i_a be an index in s_a, then you can define a function that returns a 1-dimensional parameter indexed over i_a as follows:

Function vecFunc {

    Arguments: (a,b);

    IndexDomain: i_a;

    Body: {

        ! data { dp, dq, pi, qi }

        temp('dp') := 1 + a ;

        temp('dq') := 1 + b ;

        temp('pi') := a + b ;

        temp('qi') := a * a ;

        

        vecFunc(i_a) := temp(i_a) ;

    }

    Parameter a {

        Property: Input;

    }

    Parameter b {

        Property: Input;

    }

    Parameter temp {

        IndexDomain: i_a;

    }

}

 

subsequently, you can use such a function like:


   p_d(i_a) := vecFunc( 3, 4)(i_a);
 

this would result in:

    p_d := data { dp : 4,  dq : 5,  pi : 7,  qi : 9 } ;

 

For your convenience, I attached the project that contains this example.

3 replies

gdiepen
AIMMS Champ
Forum|alt.badge.img+7
  • AIMMS Champ
  • August 27, 2024

A function can only have one return value.

 

However, if you want to have multiple output values, you can also do so by making some of the arguments InOut  or Out. This way, if the values of these are changed in the body of the function, the calling function will be able to access the new values in the parameters provided as arguments.


gabiservidone
Administrator
Forum|alt.badge.img+6

​ Hi @Jingfan Liu , how are you? 

You can use Procedures to achieve it, see more in this documentation (https://documentation.aimms.com/language-reference/procedural-language-components/procedures-and-functions/internal-procedures.html). 

To summarize it, you can either add them through the Arguments button, selecting the identifier type, the property and a unit. Or by simply adding between brackets the already existing identifiers directly on Arguments' Procedure setting. 

 


Chris Kuip
AIMMSian
Forum|alt.badge.img+7
  • AIMMSian
  • Answer
  • August 28, 2024

Yet another alternative is to return a vector instead of a scalar.

 

Let the set s_a be defined as { dp, dq, pi, qi } and i_a be an index in s_a, then you can define a function that returns a 1-dimensional parameter indexed over i_a as follows:

Function vecFunc {

    Arguments: (a,b);

    IndexDomain: i_a;

    Body: {

        ! data { dp, dq, pi, qi }

        temp('dp') := 1 + a ;

        temp('dq') := 1 + b ;

        temp('pi') := a + b ;

        temp('qi') := a * a ;

        

        vecFunc(i_a) := temp(i_a) ;

    }

    Parameter a {

        Property: Input;

    }

    Parameter b {

        Property: Input;

    }

    Parameter temp {

        IndexDomain: i_a;

    }

}

 

subsequently, you can use such a function like:


   p_d(i_a) := vecFunc( 3, 4)(i_a);
 

this would result in:

    p_d := data { dp : 4,  dq : 5,  pi : 7,  qi : 9 } ;

 

For your convenience, I attached the project that contains this example.


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator