1

I solved a problem with ampl model software. I need to convert the code to java or c# to print out the result. How can I represent the ampl model code in java. Is there any libraries? How can I represent objective function and maximize function in java and c#? Please help me on this.

My AMPL code:

param i;    #Supply
param j;    #Demand
param k;    #Time

var x{1..i,1..j,1..k} binary;

maximize z_flow : sum{a in 1..k} (x[1,1,a]+x[1,5,a] +  x[2,1,a]+x[2,3,a] + x[3,2,a]+x[3,3,a] + x[4,1,a]+ x[4,2,a] + x[4,4,a] + x[5,2,a]+x[5,3,a]);

subject to supply1cons{a in 1..k} : (x[1,1,a] + x[1,5,a]) <= 1;
subject to supply2cons{a in 1..k} : (x[2,1,a] + x[2,3,a]) <= 1;
subject to supply3cons{a in 1..k} : (x[3,2,a] + x[3,3,a]) <= 1;
subject to supply4cons{a in 1..k} : (x[4,1,a] + x[4,2,a] + x[4,4,a]) <= 1;
subject to supply5cons{a in 1..k} : (x[5,2,a] + x[5,3,a]) <= 1;

subject to demand1cons{a in 1..k} : (x[1,1,a] + x[2,1,a] + x[4,1,a]) <= 1;
subject to demand2cons{a in 1..k} : (x[3,2,a] + x[4,2,a] + x[5,2,a]) <= 1;
subject to demand3cons{a in 1..k} : (x[2,3,a] + x[3,3,a] + x[5,3,a]) <= 1;
subject to demand4cons{a in 1..k} : (x[4,4,a]) <= 1;
subject to demand5cons{a in 1..k} : (x[1,5,a]) <=1;

subject to cap1 : sum{a in 1..k}(x[1,1,a]) = 2;
subject to cap2 : sum{a in 1..k}(x[1,5,a]) = 8;
subject to cap3 : sum{a in 1..k}(x[2,1,a]) = 3;
subject to cap4 : sum{a in 1..k}(x[2,3,a]) = 4;
subject to cap5 : sum{a in 1..k}(x[3,2,a]) = 1;
subject to cap6 : sum{a in 1..k}(x[3,3,a]) = 7;
subject to cap7 : sum{a in 1..k}(x[4,1,a]) = 5;
subject to cap8 : sum{a in 1..k}(x[4,2,a]) = 2;
subject to cap9 : sum{a in 1..k}(x[4,4,a]) = 6;
subject to cap10 : sum{a in 1..k}(x[5,2,a]) = 4;
subject to cap11 : sum{a in 1..k}(x[5,3,a]) = 3;
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Googler
  • 525
  • 3
  • 12
  • 30
  • Unless your model is *very* simple, you cannot do it, it is a highly nontrivial task. – Ali Dec 11 '12 at 20:24
  • Ali..It's a simple model..I need to find the minimum time slot to send all packets in a netwrok with some contraints – Googler Dec 11 '12 at 20:31
  • Then there is some hope. Could you post your AMPL model? – Ali Dec 11 '12 at 20:36
  • Do you have any licencing restriction? Restricition on third parties? – Ali Dec 11 '12 at 20:37
  • @ali.. no...no restriction with third parties untill its free.. and please find my ampl code in the post..I redited. – Googler Dec 11 '12 at 21:15
  • Ooops, sorry, I missed it on the first read: you only want to print out the result? It is very simple in AMPL: `display x;`. See [printf](http://www.ampl.com/NEW/printf.html) if you need something more sophisticated. Is that enough? – Ali Dec 11 '12 at 21:31
  • @ali, NO.I am able to get the result..I need to do implement the above working ampl code in java or c# language. Representing the above objective functions and constraints in java. I could get the correct output in ampl. I am not sure how to implement in java or c#. Hope I am clear. – Googler Dec 12 '12 at 03:24
  • Essentianlly, you've defined your problem in AMPL and you'd like someone to write some code in c# or java that solves it for you, right? – Jodrell Dec 12 '12 at 10:34

3 Answers3

4

AMPL is a modeling language, you can create models with reasonable efforts.

Then, the AMPL envionment calls a solver (backend if you wish) that actually solves your problem. It isn't AMPL that solves your problem.

Since your problem is fortunately simple, you can give up the modeling language part. Nevertheless, you still need a solver that solves the problem for you.

Your problem is a binary integer programming problem. Any linear programming solver that can handle integer variables is a candidate. Solvers are typically written in C or C++, so you need one that has a Java or C# interface.

Finally, you will code / build your problem using that API the solver comes with.


Candidates:

See also

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • I'd add also [or-tools](http://code.google.com/p/or-tools/). It has a nice .net wrapper to GLPK, CBC and other solvers (CBC is one of the best free solvers IMHO). – digEmAll Dec 12 '12 at 10:33
0

There are two parts to your question: modeling and calling from a (C#) program. As others have noted, AMPL is a modeling language. If you like using a modeling language like AMPL, you will need to call it from your program. Currently, AMPL does not have an API, but you can call AMPL via scripting. AIMMS and MPL both have APIs that allow you to call them from other programs, but they will require you to rewrite your model in their respective modeling languages.

The other option is to call a solver API directly. A number of solvers, including CPLEX and Gurobi, have .NET APIs that allow you to build and solve a model directly from a C# program.

(Disclaimer: I currently work for Gurobi Optimization and formerly worked for ILOG, which provided CPLEX).

Greg Glockner
  • 5,433
  • 2
  • 20
  • 23
0

You might try

Optimization Framework

It's a free project where you can do modelling and also you can read AMPL Dat and Mod files into your model. Solving is possible via CPLEX, Gurobi, MOPS and others.

Optimization Framework does not use AMPL, it uses GNU Mathprog for building model instances. MathProg is a subset of AMPL, so 80%-90% of your AMPL models will run unchanged.

Have in mind, that it's an academic project, so don't expect it to be stable or ripe for commercial use.

Knasterbax
  • 7,895
  • 1
  • 29
  • 23