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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/STRUCTURE/kekulizer.h>
#include <BALL/KERNEL/forEach.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/atom.h>
#include <BALL/KERNEL/molecule.h>
#include <BALL/KERNEL/PTE.h>
//#include <BALL/DATATYPE/hashSet.h>
#include <BALL/STRUCTURE/smartsMatcher.h>
using namespace std;
// #define DEBUG_KEKULIZER
// #define BALL_MMFF94_TEST
namespace BALL
{
bool Kekuliser::AtomInfo::operator < (const Kekuliser::AtomInfo& info) const
{
return (*atom) < *info.atom;
}
Kekuliser::AtomInfo& Kekuliser::AtomInfo::operator = (const AtomInfo& ai)
{
atom = ai.atom;
double_bond = ai.double_bond;
abonds = ai.abonds;
partner_id = ai.partner_id;
curr_double = ai.curr_double;
min_double = ai.min_double;
max_double = ai.max_double;
uncharged_double = ai.uncharged_double;
return *this;
}
Kekuliser::Kekuliser()
: use_formal_charges_(true)
{
clear();
}
bool Kekuliser::setup(Molecule& mol)
{
molecule_ = &mol;
// collect aromatic bonds and atoms to speed up SMARTS matching:
collectAromaticAtoms_();
SmartsMatcher sm;
// dont recalculate the smallest set of smallest rings:
// no ring information needed for smarts matcher:
vector<vector<Atom*> > rings_vector;
sm.setSSSR(rings_vector);
Position nr_ca = 0;
vector<set<const Atom*> > result;
set<const Atom*>::iterator sit;
//////////////////////////////////////////////////////////////
// fix carboxlic acid:
sm.match(result, mol, "[#8;D1]~#6~[#8;D1]", aromatic_atoms_);
for (Position pos = 0; pos < result.size(); pos++)
{
set<const Atom*>& set = result[pos];
vector<Atom*> oxygen;
Atom* carbon = 0;
for (sit = set.begin(); sit != set.end(); ++sit)
{
if ((*sit)->getElement().getSymbol() == "C")
{
carbon = (Atom*)*sit;
}
else
{
oxygen.push_back((Atom*)*sit);
}
}
if (!hasAromaticBonds_(*carbon)) continue;
oxygen[0]->getBond(*carbon)->setOrder(Bond::ORDER__SINGLE);
oxygen[1]->getBond(*carbon)->setOrder(Bond::ORDER__DOUBLE);
aromatic_atoms_.erase(oxygen[0]);
aromatic_atoms_.erase(oxygen[1]);
aromatic_atoms_.erase(carbon);
nr_ca++;
}
//////////////////////////////////////////////////////////////
// fix amidene and guanidine
// sm.match(result, mol, "[#7;D1]#6([#7;D1])*");
Size nr_am_gu = 0;
result.clear();
sm.match(result, mol, "[#7;D1]~[#6R0]~[#7;D1]", aromatic_atoms_);
if (!result.empty())
{
for (Position pos = 0; pos < result.size(); pos++)
{
set<const Atom*>& set = result[pos];
vector<Atom*> nitrogen;
Atom* carbon = 0;
for (sit = set.begin(); sit != set.end(); ++sit)
{
if ((*sit)->getElement().getSymbol() == "C")
{
carbon = (Atom*)*sit;
}
else
{
nitrogen.push_back((Atom*)*sit);
}
}
if (!hasAromaticBonds_(*carbon)) continue;
nitrogen[0]->getBond(*carbon)->setOrder(Bond::ORDER__SINGLE);
nitrogen[1]->getBond(*carbon)->setOrder(Bond::ORDER__DOUBLE);
aromatic_atoms_.erase(carbon);
aromatic_atoms_.erase(nitrogen[0]);
aromatic_atoms_.erase(nitrogen[1]);
nr_am_gu++;
}
}
//////////////////////////////////////////////////////////////
// fix phosphonic acid
result.clear();
Size nr_phos = 0;
// sm.match(result, mol, "[p]([oD1])([oD1])([oD1])[#6,#8]");
sm.match(result, mol, "[P]([#8;D1])([#8;D1])([#8;D1])", aromatic_atoms_);
for (Position pos = 0; pos < result.size(); pos++)
{
set<const Atom*>& set = result[pos];
vector<Atom*> oxygen;
Atom* phosphor = 0;
for (sit = set.begin(); sit != set.end(); ++sit)
{
if ((*sit)->getElement().getSymbol() == "P")
{
phosphor = (Atom*)*sit;
}
else
{
oxygen.push_back((Atom*)*sit);
}
}
if (!hasAromaticBonds_(*phosphor)) continue;
oxygen[0]->getBond(*phosphor)->setOrder(Bond::ORDER__DOUBLE);
oxygen[1]->getBond(*phosphor)->setOrder(Bond::ORDER__SINGLE);
oxygen[2]->getBond(*phosphor)->setOrder(Bond::ORDER__SINGLE);
aromatic_atoms_.erase(phosphor);
aromatic_atoms_.erase(oxygen[0]);
aromatic_atoms_.erase(oxygen[1]);
aromatic_atoms_.erase(oxygen[2]);
nr_phos++;
}
//////////////////////////////////////////////////////////////
// fix O~N
Size on = 0;
sm.match(result, mol, "[#8;D1;R0;$(#8~[#6R0])]", aromatic_atoms_);
for (Position pos = 0; pos < result.size(); pos++)
{
Atom* oxygen = (Atom*)*result[pos].begin();
if (!hasAromaticBonds_(*oxygen)) continue;
oxygen->getBond(0)->setOrder(Bond::ORDER__DOUBLE);
on++;
}
//////////////////////////////////////////////////////////////
// fix aromatic rings
bool ok = fixAromaticRings_();
// recollect the remaining aromatic bonds:
unassigned_bonds_.clear();
AtomBondIterator bit;
AtomIterator ait;
BALL_FOREACH_BOND(mol, ait, bit)
{
if (bit->getOrder() == Bond::ORDER__AROMATIC)
{
unassigned_bonds_.push_back(&*bit);
}
}
if (ok)
{
// set formal charges
HashMap<Atom*,Index>::Iterator hit = max_valence_.begin();
for (; hit != max_valence_.end(); ++hit)
{
Atom* atom = hit->first;
Size nr = 0;
AtomBondIterator bit = atom->beginBond();
for(;+bit;++bit)
{
if (bit->getOrder() >= 1 && bit->getOrder() <= 5)
{
nr += (Size) bit->getOrder();
}
}
atom->setFormalCharge(nr - hit->second);
}
}
#ifdef BALL_MMFF94_TEST
Log.error() << "Kekulized bonds: "
<< "CA " << nr_ca << " "
<< "NH2 " << nr_am_gu << " "
<< "ON " << on << " "
<< "PHOS " << nr_phos << std::endl;
Log.error() << "Not kekulized: " << unassigned_bonds_.size() << std::endl;
#endif
return ok;
}
bool Kekuliser::hasAromaticBonds_(Atom& atom)
{
AtomBondIterator abit = atom.beginBond();
for (; +abit; ++abit)
{
if (abit->getOrder() == Bond::ORDER__AROMATIC)
{
return true;
}
}
return false;
}
void Kekuliser::dump()
{
for (Position p = 0; p < atom_infos_.size(); p++)
{
AtomInfo& ai = atom_infos_[p];
Log.error() << ai.atom->getFullName() << " a. Bonds: " << ai.abonds.size()
<< " Cur " << ai.curr_double
<< " min " << ai.min_double
<< " max " << ai.max_double
<< " unc " << ai.uncharged_double
<< " ";
for (Position b = 0; b < ai.abonds.size(); b++)
{
Atom* partner = ai.abonds[b]->getPartner(*ai.atom);
Log.error() << partner->getName() << " ";
}
Log.error() << " " << current_penalty_;
if (lowest_penalty_ != std::numeric_limits<int>::max()) Log.error() << " + " << lowest_penalty_;
Log.error() << std::endl;
}
}
bool Kekuliser::fixAromaticRings_()
{
bool ok = true;
calculateAromaticSystems_();
if (aromatic_systems_.empty()) return true;
getMaximumValence_();
AtomInfo temp_ai;
temp_ai.uncharged_double = 0;
temp_ai.curr_double = 0;
temp_ai.double_bond = 0;
// iterate over all aromatic systems:
vector<set<Atom*> >::iterator rit = aromatic_systems_.begin();
for (; rit != aromatic_systems_.end(); rit++)
{
set<Atom*>& atom_set = *rit;
// abort for strange rings:
if (atom_set.size() < 3)
{
#ifdef DEBUG_KEKULIZER
Log.error() << "Kekulizer: Could not assign ring with " << (*rit).size()
<< " atoms. " << std::endl;
if (atom_set.size())
{
Log.error() << (**atom_set.begin()).getFullName() << std::endl;
}
#endif
ok = false;
continue;
}
atom_infos_.clear();
bool abort_this_ring = false;
// for one aromatic system: collect all needed informations for the individual atoms:
set<Atom*>::iterator hit = atom_set.begin();
for (; hit != atom_set.end(); ++hit)
{
Atom& atom = *(Atom*)*hit;
// calculate the current valence:
Index curr_valence = 0;
AtomBondIterator bit = atom.beginBond();
for (; +bit; ++bit)
{
if (bit->getType() == Bond::TYPE__HYDROGEN) continue;
if (bit->getOrder() < 2 ||
bit->getOrder() > 4)
{
curr_valence++;
}
else
{
curr_valence += (Index) bit->getOrder();
}
}
// calculate the number of needed double bonds:
Index uncharged_double = max_valence_[&atom] - curr_valence;
if (uncharged_double < 0)
{
Log.error() << "Kekulizer: Could not calculate max number of needed double bonds for "
<< atom.getFullName() << std::endl;
Log.error() << "Max: " << max_valence_[&atom] << " Curr: " << curr_valence << std::endl;
abort_this_ring = true;
ok = false;
break;
}
atom_infos_.push_back(temp_ai);
AtomInfo& info = *atom_infos_.rbegin();
info.atom = &atom;
Index max_double = uncharged_double + 1;
max_double = std::min(1, max_double);
Index min_double = uncharged_double - 1;
min_double = std::max(0, min_double);
// allow charged atoms only for selected elements
Position atomic_number = atom.getElement().getAtomicNumber();
if (atomic_number != 6 &&
atomic_number != 7)
{
max_double = uncharged_double;
min_double = uncharged_double;
}
info.max_double = max_double;
info.min_double = min_double;
info.uncharged_double = uncharged_double;
// collect aromatic bonds for this atom:
bit = atom.beginBond();
for (; +bit; ++bit)
{
// add aromatic bonds only once:
if (bit->getOrder() == Bond::ORDER__AROMATIC &&
*bit->getPartner(atom) > atom)
{
// set bond order initialy to single
bit->setOrder(Bond::ORDER__SINGLE);
info.abonds.push_back(&*bit);
}
}
} // all aromatic atoms of this ring
if (abort_this_ring) continue;
std::sort(atom_infos_.begin(), atom_infos_.end());
// map the AtomInfos to the atoms
HashMap<Atom*, Position> atom_to_id;
for (Position p = 0; p < atom_infos_.size(); p++)
{
atom_to_id[atom_infos_[p].atom] = p;
}
for (Position p = 0; p < atom_infos_.size(); p++)
{
AtomInfo& ai = atom_infos_[p];
Atom* atom = ai.atom;
for (Position b = 0; b < ai.abonds.size(); b++)
{
Atom* partner = ai.abonds[b]->getPartner(*atom);
Position partnerp = atom_to_id[partner];
ai.partner_id.push_back(partnerp);
}
}
#ifdef DEBUG_KEKULIZER
Log.error() << "State before Kekulizer:" << std::endl;
dump();
#endif
solutions_.clear();
lowest_penalty_ = std::numeric_limits<int>::max();
current_penalty_ = 0;
fixAromaticSystem_(0);
// test could be changed to achieve at most a given max value:
if (lowest_penalty_ < std::numeric_limits<int>::max())
{
if (lowest_penalty_ == 0)
{
applySolution_(0);
}
else
{
applySolution_(calculateDistanceScores_());
}
continue;
}
// we were not successfull, so reset the bonds to aromatic:
ok = false;
} // all aromatic systems
return ok;
}
void Kekuliser::fixAromaticSystem_(Position it)
{
#ifdef DEBUG_KEKULIZER
if (it < atom_infos_.size())
{
Log.error() << "fixAromaticSystem_ " << it << " " << atom_infos_[it].atom->getFullName() << std::endl;
dump();
}
#endif
if (current_penalty_ > lowest_penalty_) return;
// no more atoms in this aromatic system?
if (it >= atom_infos_.size())
{
// is this solution maybe better than any we have tested so far?
//
// if we had already a solution with a penalty of 0, it can't get any better!
if (lowest_penalty_ == 0) return;
if (current_penalty_ <= lowest_penalty_)
{
// better than any we have seen so far?
// than throw away the old solutions!
if (current_penalty_ < lowest_penalty_)
{
solutions_.clear();
lowest_penalty_ = current_penalty_;
}
// store the new solution
solutions_.push_back(atom_infos_);
}
return;
}
AtomInfo& ai = atom_infos_[it];
// no aromatic bonds left?
if (ai.abonds.empty())
{
// penality for this atom if we have not enough double bonds for it
// to become uncharged:
Size tap = 0;
if (ai.curr_double < ai.uncharged_double)
{
tap = getPenalty_(*ai.atom, -1);
}
current_penalty_ += tap;
fixAromaticSystem_(it + 1);
current_penalty_ -= tap;
return;
// no aromatic bonds left to setup, so in case of no success:
// no need to reset the bonds to single order
}
// first try without any double bonds if this atom is than uncharged:
if (ai.min_double <= ai.curr_double &&
ai.uncharged_double == ai.curr_double)
{
fixAromaticSystem_(it + 1);
}
// this atom penalty:
Size tap = 0;
// can we try to add a double bond?
if (ai.curr_double < ai.max_double)
{
if (ai.uncharged_double != 1)
{
tap = getPenalty_(*ai.atom, 1);
}
for (Position b = 0; b < ai.abonds.size(); b++)
{
// get the bond and partner atom:
Bond* bond = ai.abonds[b];
Position p = ai.partner_id[b];
AtomInfo& pi = atom_infos_[p];
// if partner cant take any more double bonds, take next bond:
if (pi.curr_double == pi.max_double)
{
continue;
}
// partner atom penalty:
Size pap = 0;
if (pi.curr_double + 1 > pi.uncharged_double)
{
pap += getPenalty_(*pi.atom, 1);
}
pap += tap;
// try an early break
if (current_penalty_ + pap > lowest_penalty_)
{
continue;
}
ai.curr_double++;
pi.curr_double++;
ai.double_bond = bond;
pi.double_bond = bond;
current_penalty_ += pap;
// try this solution:
fixAromaticSystem_(it + 1);
// remove the bond and reset all values:
current_penalty_ -= pap;
ai.double_bond = 0;
pi.double_bond = 0;
ai.curr_double--;
pi.curr_double--;
}
}
// try without any extra double bonds,
// if this atom will than be charged,
// otherwise we have tested it above
if (ai.uncharged_double != ai.curr_double)
{
tap = 0;
if (ai.curr_double == 0) tap = getPenalty_(*ai.atom, -1);
current_penalty_ += tap;
fixAromaticSystem_(it + 1);
current_penalty_ -= tap;
}
}
void Kekuliser::collectAromaticAtoms_()
{
aromatic_systems_.clear();
aromatic_atoms_.clear();
AtomIterator ait;
BALL_FOREACH_ATOM(*molecule_, ait)
{
AtomBondIterator abit = ait->beginBond();
for (; +abit; ++abit)
{
if (abit->getOrder() == Bond::ORDER__AROMATIC)
{
aromatic_atoms_.insert(abit->getFirstAtom());
aromatic_atoms_.insert(abit->getSecondAtom());
break;
}
}
}
all_aromatic_atoms_ = aromatic_atoms_;
}
void Kekuliser::calculateAromaticSystems_()
{
collectAromaticAtoms_();
// iterate over all aromatic ring atoms:
while (!aromatic_atoms_.empty())
{
// first ring aromatic atom that is still left:
Atom* atom = (Atom*)*aromatic_atoms_.begin();
current_aromatic_system_.clear();
collectSystems_(*atom);
aromatic_systems_.push_back(current_aromatic_system_);
}
}
void Kekuliser::collectSystems_(Atom& atom)
{
current_aromatic_system_.insert(&atom);
aromatic_atoms_.erase(&atom);
// all bonds of this atom:
AtomBondIterator abit = atom.beginBond();
for (; +abit; ++abit)
{
// if aromatic bond:
if (abit->getOrder() != Bond::ORDER__AROMATIC) continue;
Atom* partner = abit->getPartner(atom);
// if not seen partner before:
if (aromatic_atoms_.find(partner) == aromatic_atoms_.end()) continue;
collectSystems_(*partner);
}
}
void Kekuliser::getMaximumValence_()
{
max_valence_.clear();
vector<set<Atom*> >::iterator rit = aromatic_systems_.begin();
for (; rit != aromatic_systems_.end(); rit++)
{
set<Atom*>::iterator hit = (*rit).begin();
for (; hit != rit->end(); ++hit)
{
Atom& atom = **hit;
Index max_valence = 0;
if (max_valence_.has(&atom)) continue;
Position atomic_number = atom.getElement().getAtomicNumber();
switch (atomic_number)
{
case 6:
max_valence = 4;
break;
case 8:
case 16:
case 34:
case 52:
max_valence = 2;
break;
case 7:
case 15:
case 33:
max_valence = 3;
break;
}
// Nitrogen and sulfur:
if (atomic_number == 7 ||
atomic_number == 16)
{
AtomBondIterator abit = atom.beginBond();
for (; +abit; ++abit)
{
if (abit->getOrder() != Bond::ORDER__DOUBLE) continue;
if (abit->getPartner(atom)->getElement().getAtomicNumber() == 8)
{
max_valence += 2;
}
}
}
max_valence_[&atom] = max_valence;
}
}
}
void Kekuliser::clear()
{
aromatic_systems_.clear();
aromatic_rings_.clear();
unassigned_bonds_.clear();
aromatic_atoms_.clear();
max_valence_.clear();
current_aromatic_system_.clear();
atom_infos_.clear();
}
Size Kekuliser::getPenalty_(Atom& atom, Index charge)
{
#define POSITIVE_NITROGEN 10
#define NEGATIVE_NITROGEN 11
#define NEGATIVE_CARBON 25
#define POSITIVE_CARBON 26
// if formal charge information available and we get an other charge:
#define UNEQUAL_CHARGE 100
if (use_formal_charges_ && atom.getFormalCharge() != 0)
{
if (atom.getFormalCharge() != charge) return UNEQUAL_CHARGE;
return 0;
}
Position p = atom.getElement().getAtomicNumber();
if (p == 6)
{
if (charge == 1) return POSITIVE_CARBON;
else if (charge == -1) return NEGATIVE_CARBON;
}
if (p == 7)
{
if (charge == -1) return NEGATIVE_NITROGEN;
else if (charge == 1)
{
AtomBondIterator abit = atom.beginBond();
for (; +abit; ++abit)
{
if (abit->getPartner(atom)->countBonds() == 1) return POSITIVE_NITROGEN - 1;
}
return POSITIVE_NITROGEN;
}
}
return 0;
}
void Kekuliser::applySolution_(Position pos)
{
vector<AtomInfo> vit = solutions_[pos];
for (Position i = 0; i < vit.size(); i++)
{
AtomInfo& ai = vit[i];
if (ai.double_bond != 0) ai.double_bond->setOrder(Bond::ORDER__DOUBLE);
}
}
// calculate a score for the distribution of the different
// charged atoms in this aromatic system:
// return the best solution
Position Kekuliser::calculateDistanceScores_()
{
float best_score = std::numeric_limits<int>::max();
Size best_solution = 0;
for (Position solp = 0; solp < solutions_.size(); solp++)
{
vector<AtomInfo> vit = solutions_[solp];
vector<Atom*> pos_atoms;
vector<Atom*> neg_atoms;
for (Position a = 0; a < vit.size(); a++)
{
AtomInfo& ai = vit[a];
if (ai.curr_double < ai.uncharged_double)
{
neg_atoms.push_back(ai.atom);
continue;
}
if (ai.curr_double > ai.uncharged_double)
{
pos_atoms.push_back(ai.atom);
}
}
float this_score = 0;
// atoms with same charge get a high penalty score if they are near each other
for (Position posp = 0; posp < pos_atoms.size(); posp++)
{
for (Position p = posp; p < pos_atoms.size(); p++)
{
Atom* a1 = pos_atoms[posp];
Atom* a2 = pos_atoms[p];
this_score += 100. / (a1->getPosition().getSquareDistance(a2->getPosition()) + 1.);
}
}
for (Position negp = 0; negp < neg_atoms.size(); negp++)
{
for (Position p = negp; p < neg_atoms.size(); p++)
{
Atom* a1 = neg_atoms[negp];
Atom* a2 = neg_atoms[p];
this_score += 100. / (a1->getPosition().getSquareDistance(a2->getPosition()) + 1.);
}
}
// atoms with different charge get a high penalty score if they are far away
for (Position p = 0; p < pos_atoms.size(); p++)
{
Atom* a1 = pos_atoms[p];
for (Position n = 0; n < neg_atoms.size(); n++)
{
Atom* a2 = neg_atoms[n];
this_score += (a1->getPosition().getSquareDistance(a2->getPosition())) / 100.;
}
}
if (this_score < best_score)
{
best_solution = solp;
best_score = this_score;
}
}
return best_solution;
}
} // namespace BALL
|