File: Solver.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (212 lines) | stat: -rw-r--r-- 6,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* This file is part of the KDE project
   Copyright (C) 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Local
#include "Solver.h"

#include <kdebug.h>
#include <kpluginfactory.h>
#include <ktextedit.h>
#include <kaction.h>
#include <kstandarddirs.h>
#include <kactioncollection.h>
#include <Formula.h>
#include <Cell.h>
#include <part/Doc.h>
#include <Sheet.h>
#include <Value.h>
#include <part/View.h>
#include <Region.h>

#include "SolverDialog.h"

using namespace Calligra::Sheets::Plugins;

// make the plugin available
K_PLUGIN_FACTORY(SolverFactory, registerPlugin<Calligra::Sheets::Plugins::Solver>();)
K_EXPORT_PLUGIN(SolverFactory("sheetssolver"))

Calligra::Sheets::View* s_view = 0;
Calligra::Sheets::Formula* s_formula = 0;
double _function(const gsl_vector* vector, void *params);


class  Solver::Private
{
public:
    SolverDialog* dialog;
    View* view;
};

Solver::Solver(QObject* parent, const QVariantList& args)
    : d(new Private)
{
    Q_UNUSED(args)

    setXMLFile(KStandardDirs::locate("data", "sheets/viewplugins/solver.rc"), true);

    d->dialog = 0;
    d->view = qobject_cast<View*>(parent);
    if (!d->view) {
        kError() << "Solver: Parent object is not a Calligra::Sheets::View! Quitting." << endl;
        return;
    }

    QAction* solver = actionCollection()->addAction("sheetssolver");
    solver->setText(i18n("Function Optimizer..."));
    connect(solver, SIGNAL(triggered(bool)), this, SLOT(showDialog()));
}

Solver::~Solver()
{
    delete d;
}

void Solver::showDialog()
{
    d->dialog = new SolverDialog(d->view->selection(), d->view);
    connect(d->dialog, SIGNAL(okClicked()), this, SLOT(optimize()));
    d->dialog->show();
}

void Solver::optimize()
{
    register Sheet * const sheet = d->view->activeSheet();
    if (!sheet)
        return;

    if (d->dialog->function->textEdit()->toPlainText().isEmpty())
        return;

    if (d->dialog->parameters->textEdit()->toPlainText().isEmpty())
        return;

    Region region(d->dialog->function->textEdit()->toPlainText(), d->view->doc()->map(), d->view->activeSheet());
    if (!region.isValid())
        return;

    const QPoint point = (*region.constBegin())->rect().topLeft();
    const Cell formulaCell = Cell(sheet, point.x(), point.y());
    if (!formulaCell.isFormula())
        return;

    kDebug() << formulaCell.userInput();
    s_formula = new Formula(sheet);
    if (d->dialog->minimizeButton->isChecked()) {
        s_formula->setExpression(formulaCell.userInput());
    } else if (d->dialog->maximizeButton->isChecked()) {
        // invert the formula
        s_formula->setExpression("=-(" + formulaCell.userInput().mid(1) + ')');
    } else { // if (d->dialog->valueButton->isChecked())
        // TODO
        s_formula->setExpression("=ABS(" + formulaCell.userInput().mid(1) + '-'
                                 + d->dialog->value->text() + ')');
    }

    // Determine the parameters
    int dimension = 0;
    Parameters* parameters = new Parameters;
    region = Region(d->dialog->parameters->textEdit()->toPlainText(), d->view->doc()->map(), d->view->activeSheet());
    Region::ConstIterator end(region.constEnd());
    for (Region::ConstIterator it(region.constBegin()); it != end; ++it) {
        QRect range = (*it)->rect();
        for (int col = range.left(); col <= range.right(); ++col) {
            for (int row = range.top(); row <= range.bottom(); ++row) {
                parameters->cells.append(Cell(sheet, col, row));
                ++dimension;
            }
        }
    }

    /* Initial vertex size vector with a step size of 1 */
    gsl_vector* stepSizes = gsl_vector_alloc(dimension);
    gsl_vector_set_all(stepSizes, 1.0);

    /* Initialize starting point */
    int index = 0;
    gsl_vector* x = gsl_vector_alloc(dimension);
    foreach(const Cell &cell, parameters->cells) {
        gsl_vector_set(x, index++, numToDouble(cell.value().asFloat()));
    }

    /* Initialize method and iterate */
    gsl_multimin_function functionInfo;
    functionInfo.f = &_function;
    functionInfo.n = dimension;
    functionInfo.params = static_cast<void*>(parameters);

    // Use the simplex minimizer. The others depend on the first derivative.
    const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
    gsl_multimin_fminimizer* minimizer = gsl_multimin_fminimizer_alloc(T, dimension);
    gsl_multimin_fminimizer_set(minimizer, &functionInfo, x, stepSizes);

    int status = 0;
    int iteration = 0;
    const int maxIterations = d->dialog->iterations->value();
    double size = 1;
    const double epsilon = d->dialog->precision->value();
    do {
        iteration++;
        status = gsl_multimin_fminimizer_iterate(minimizer);

        if (status)
            break;

        size = gsl_multimin_fminimizer_size(minimizer);
        status = gsl_multimin_test_size(size, epsilon);

        if (status == GSL_SUCCESS) {
            kDebug() << "converged to minimum after" << iteration << " iteration(s) at";
        }

        for (int i = 0; i < dimension; ++i) {
            printf("%10.3e ", gsl_vector_get(minimizer->x, i));
        }
        printf("f() = %7.3f size = %.3f\n", minimizer->fval, size);
    } while (status == GSL_CONTINUE && iteration < maxIterations);


    // free allocated memory
    gsl_vector_free(x);
    gsl_vector_free(stepSizes);
    gsl_multimin_fminimizer_free(minimizer);
    delete parameters;
    delete s_formula;
}

double Solver::evaluate(const gsl_vector* vector, void *parameters)
{
    Q_UNUSED(vector)
    Q_UNUSED(parameters)
    return 0.0;
}

double _function(const gsl_vector* vector, void *params)
{
    Solver::Parameters* parameters = static_cast<Solver::Parameters*>(params);

    for (int i = 0; i < parameters->cells.count(); ++i) {
        parameters->cells[i].setValue(Calligra::Sheets::Value(gsl_vector_get(vector, i)));
    }

    // TODO check for errors/correct type
    return numToDouble(s_formula->eval().asFloat());
}

#include "Solver.moc"