~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/solver/chem.ml

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(***************************************************************************
 
2
 *   Copyright (C) 2004 by Thomas Nagy                                     *
 
3
 *   tnagy2^8@yahoo.fr                                                     *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 
19
 ***************************************************************************)
 
20
 
 
21
open Facile;;
 
22
open Easy;;
 
23
open Datastruct;;
 
24
 
 
25
let solve (eq : eqtable) =
 
26
    let nb_molecules = eq#getsize_j () and nb_elements = eq#getsize_i () in
 
27
    let dist = Fd.array nb_molecules 1 900 in
 
28
 
 
29
    (* trivial constraints on domains *)
 
30
    for j = 0 to nb_molecules -1 do
 
31
        let num = try int_of_string (eq#getvar j) with _ -> -1 in
 
32
        if num > -1 then dist.(j) <- Fd.int num
 
33
    done;
 
34
    
 
35
    (* raises an exception if the problem is not solvable *)
 
36
    for i = 0 to nb_elements - 1 do
 
37
        Cstr.post (Arith.scalprod_fd (eq#getline i) dist =~ i2e 0)
 
38
    done;
 
39
    
 
40
    let goal = Goals.GlArray.labeling dist in
 
41
    if (Goals.solve goal) then Array.iteri (fun cnt i -> eq#setsol cnt (Fd.min i)) dist
 
42
    else failwith "no solution found"
 
43
;;
 
44
 
 
45
(* workaround for (probably) a bug in the facile library 1.0 (fixed in 1.1?) : 
 
46
 * when the constraints make a problem
 
47
 * unsolvable, an exception is raised
 
48
 * 
 
49
 * unfortunately the next problem
 
50
 * solved afterwards is not handled properly *)
 
51
 
 
52
let cleanup (eq : eqtable) =
 
53
(*  Printf.printf "cleaning up"; *)
 
54
    let nb_molecules = eq#getsize_j () and nb_elements = eq#getsize_i () in
 
55
    let dist = Fd.array nb_molecules 0 2 in
 
56
    let goal = Goals.GlArray.labeling dist in
 
57
    if not (Goals.solve goal) then failwith "fatal error"
 
58
;;
 
59
 
 
60