1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 1997-2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * END LICENSE BLOCK */
22
23/*
24 * ECLiPSe Application Example
25 *
26 * $Id: eg_c_mip.c,v 1.1 2008/06/30 17:43:53 jschimpf Exp $
27 *
28 * IDENTIFICATION:	eg_sendmore.c
29 *
30 * AUTHOR:		Joachim Schimpf
31 * AUTHOR:		Stefano Novello
32 *
33 * DESCRIPTION:
34 *	Example of solving a constraint problem from C
35 */
36
37#include	"eclipse.h"
38
39
40#define NCOLS	3
41#define NROWS	2
42
43double req[NROWS][NCOLS] = {
44    2.2, 1.8, 1.9,
45    2.4, 2.0, 2.1
46};
47
48double pc[NCOLS] = {
49    24.7, 22.4, 19.7
50};
51
52
53
54
55static void bounds(vars, lb, ub)
56pword vars;
57double lb, ub;
58{
59    ec_post_goal(ec_term(
60	ec_did("::",2),
61	    vars,
62	    ec_term(ec_did("..",2), ec_double(lb), ec_double(ub))));
63}
64
65static void eq(lhs, rhs)
66pword lhs, rhs;
67{
68    ec_post_goal(ec_term(ec_did("$=",2), lhs, rhs));
69}
70
71static void geq(lhs, rhs)
72pword lhs, rhs;
73{
74    ec_post_goal(ec_term(ec_did("$>=",2), lhs, rhs));
75}
76
77static void leq(lhs, rhs)
78pword lhs, rhs;
79{
80    ec_post_goal(ec_term(ec_did("$=<",2), lhs, rhs));
81}
82
83static void maximize(obj, objval)
84pword obj, objval;
85{
86    ec_post_goal( ec_term(ec_did("optimize",2),
87	ec_term(ec_did("max",1), obj), objval));
88}
89
90static pword plus(lhs, rhs)
91pword lhs, rhs;
92{
93    return ec_term(ec_did("+",2), lhs, rhs);
94}
95
96static pword times(lhs, rhs)
97pword lhs, rhs;
98{
99    return ec_term(ec_did("*",2), lhs, rhs);
100}
101
102
103main()
104{
105    ec_refs	Vars;
106    ec_ref	Profit;
107    pword	varlist;
108
109    ec_init();
110
111    ec_post_string("lib(eplex)");
112
113    Vars = ec_refs_create_newvars(NCOLS);
114    Profit = ec_ref_create_newvar();
115    varlist = ec_listofrefs(Vars);
116
117    bounds(varlist, 0.0, 1e20);
118
119    eq(  times(varlist, ec_listofdouble(NCOLS,pc)),	ec_ref_get(Profit));
120    leq( times(varlist, ec_listofdouble(NCOLS,req[0])),	ec_double(8.0));
121    leq( times(varlist, ec_listofdouble(NCOLS,req[1])),	ec_double(10.0));
122
123    maximize( times(varlist, ec_listofdouble(NCOLS,pc)), ec_ref_get(Profit));
124
125    if (ec_resume1(0) == PSUCCEED)		/* solve */
126    {
127	double d;
128	int i;
129
130	if (ec_get_double(ec_ref_get(Profit), &d) == PSUCCEED)
131	    printf("Profit is %f\n", d);
132	else
133	    printf("Profit is ?\n");
134
135	for (i=0; i<NCOLS; i++)
136	{
137	    if (ec_get_double(ec_refs_get(Vars,i), &d) == PSUCCEED)
138		printf("X%d = %f\n", i, d);
139	    else
140		printf("X%d = ?\n");
141	}
142    }
143    else printf("No solution\n");
144
145    ec_refs_destroy(Vars);
146    ec_ref_destroy(Profit);
147    ec_cleanup();
148}
149
150