How to define an objective function piecemeal

6 views
Skip to first unread message

gpb...@gmail.com

unread,
Nov 12, 2017, 12:16:11 PM11/12/17
to AMPL Modeling Language

I am working on a large problem with various soft constraints. I want to take a modular approach, where each soft constraint is handled in its own .mod or .run file, and this file includes a definition for part of the objective function. The final objective function would then be a sum of the OFs from all included soft-constraint files.

Is there a nice way to do this? Currently I have to define the objective function in one place, which hurts modularity. If I want to add a new soft constraint, I need to create a new .mod that defines relevant variables etc., but then edit the objective function in a separate place to include terms related to the new soft constraint; if I then decide to remove the new constraint, I also have to edit it out of the objective function.

Ideally, I would like a solution such that when I comment out the .mod or .run related to the soft constraint, the objective function is automatically modified to remove relevant terms.

Illustration: what I have is of the form:

# main.mod

var x{i}; # decisions to be made
# various params and constraints here

# option1.mod
var y{j}; # defines some related outcome
# constraints here that relate y to x

# option2.mod
var z{k};
# constraints here that relate z to x

# masterobjective.mod
minimize OF_Everything: [function of x] + [function of y] + [function of z];

# note that if I decide to leave out one of the optional .mods I need to edit masterobjective.mod, otherwise my OF refers to variables that don't exist
# note also that the OF related to y is in a separate file to the definition and constraints related to y, which makes things harder to follow

What I want is something that works more like:

# main.mod

var x{i}; # decisions to be made
# various params and constraints here
minimize OF_Everything: [function of x];

# option1.mod
var y{j}; # defines some related outcome
# constraints here that relate y to x
let OF_Everything := OF_Everything + [function of y];

# option2.mod
var z{k};
# constraints here that relate z to x
let OF_Everything := OF_Everything + [function of z];

# note that in this case, if I drop out one of the optional .mods the relevant terms would simply vanish from the OF
# note also that the OF components related to y are defined right next to everything else related to y, making things easier to follow

Any ideas here? I'm open to other solutions, basically I just want something that lets me slot individual soft constraints in and out without having to modify several parts of the program.

Thanks in advance - Geoffrey

Robert Fourer

unread,
Nov 16, 2017, 1:10:11 PM11/16/17
to am...@googlegroups.com
AMPL does not have a syntax for adding terms to an objective function that has already been defined. You might do something close to what you have mind by working at the input file level, writing the objective function statement piece by piece to a file and then reading the file at the end. Your main model would create the file and the optimal models would add to the same file:

#main.mod
printf "minimize OF_Everything: [function of x]" >obj.txt;

#option1.mod
printf " + [function of y]" >obj.txt;

#option2.mod
printf " + [function of z]" >obj.txt;

Following the reading of these files, you would need to actually define the objective by running the commands

close obj.txt;
include obj.txt ;

However it seems that the modularity that you are seeking is mainly preserved.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages