Index: kernel/src/Restraint.cpp
===================================================================
--- kernel/src/Restraint.cpp	(revision 587)
+++ kernel/src/Restraint.cpp	(working copy)
@@ -42,7 +42,8 @@
 void Restraint::set_model(Model* model)
 {
   IMP_assert(model==NULL || number_of_particles()==0
-             || model == get_particle(0)->get_model(),
+             || model == get_particle(0)->get_model()
+             || model_&& model_.get() == model,
              "Model* different from Particle Model*");
   model_=model;
 }
@@ -65,7 +66,6 @@
                || obj->get_model() == (*particles_begin())->get_model(),
                "All particles in Restraint must belong to the "
                "same Model.");
-    if (0) std::cout << index;
   },);
 
 }  // namespace IMP
Index: kernel/src/ScoreState.cpp
===================================================================
--- kernel/src/ScoreState.cpp	(revision 587)
+++ kernel/src/ScoreState.cpp	(working copy)
@@ -19,7 +19,6 @@
 {
   update_iteration_= std::numeric_limits<unsigned int>::max();
   after_iteration_= std::numeric_limits<unsigned int>::max();
-  model_ = NULL;
   IMP_LOG(VERBOSE, "ScoreState constructed " << name << std::endl);
 }
 
Index: kernel/src/restraints/ConstantRestraint.cpp
===================================================================
--- kernel/src/restraints/ConstantRestraint.cpp	(revision 0)
+++ kernel/src/restraints/ConstantRestraint.cpp	(revision 0)
@@ -0,0 +1,27 @@
+/**
+ *  \file ConstantRestraint.cpp \brief Don't restrain anything.
+ *
+ *  Copyright 2007-8 Sali Lab. All rights reserved.
+ *
+ */
+
+#include "IMP/restraints/ConstantRestraint.h"
+
+namespace IMP
+{
+
+ConstantRestraint::ConstantRestraint(Float v) : v_(v){}
+
+
+
+Float ConstantRestraint::evaluate(DerivativeAccumulator *accum)
+{
+  return v_;
+}
+
+void ConstantRestraint::show(std::ostream& out) const
+{
+  out << "Constant restraint :" << v_ << std::endl;
+}
+
+}  // namespace IMP
Index: kernel/src/restraints/DistanceRestraint.cpp
===================================================================
--- kernel/src/restraints/DistanceRestraint.cpp	(revision 587)
+++ kernel/src/restraints/DistanceRestraint.cpp	(working copy)
@@ -5,8 +5,6 @@
  *
  */
 
-#include <cmath>
-
 #include "IMP/Particle.h"
 #include "IMP/Model.h"
 #include "IMP/log.h"
Index: kernel/src/restraints/SConscript
===================================================================
--- kernel/src/restraints/SConscript	(revision 587)
+++ kernel/src/restraints/SConscript	(working copy)
@@ -4,7 +4,8 @@
          'DistanceRestraint.cpp', 'AngleRestraint.cpp', 'DihedralRestraint.cpp',
          'NonbondedRestraint.cpp', 'BondDecoratorRestraint.cpp',
          'SingletonListRestraint.cpp', 'PairListRestraint.cpp',
-         'TripletChainRestraint.cpp', 'PairChainRestraint.cpp']
+         'TripletChainRestraint.cpp', 'PairChainRestraint.cpp',
+         'ConstantRestraint.cpp']
 
 files = [File(x) for x in files]
 Return('files')
Index: kernel/include/IMP/SingletonScore.h
===================================================================
--- kernel/include/IMP/SingletonScore.h	(revision 587)
+++ kernel/include/IMP/SingletonScore.h	(working copy)
@@ -9,7 +9,7 @@
 
 #include "IMP_config.h"
 #include "base_types.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 #include "DerivativeAccumulator.h"
 
 namespace IMP
@@ -27,7 +27,7 @@
 /** SingletonScores should take a UnaryFunction as their first
     argument if such is needed.
 */
-class IMPDLLEXPORT SingletonScore : public internal::Object
+class IMPDLLEXPORT SingletonScore : public internal::RefCountedObject
 {
 public:
   SingletonScore() {}
@@ -38,6 +38,8 @@
   virtual void show(std::ostream &out=std::cout) const = 0;
 };
 
+IMP_OUTPUT_OPERATOR(SingletonScore);
+
 } // namespace IMP
 
 #endif  /* __IMP_SINGLETON_SCORE_H */
Index: kernel/include/IMP/Model.h
===================================================================
--- kernel/include/IMP/Model.h	(revision 587)
+++ kernel/include/IMP/Model.h	(working copy)
@@ -61,7 +61,9 @@
   }
 };
 
+IMP_OUTPUT_OPERATOR(Model);
 
+
 } // namespace IMP
 
 #endif  /* __IMP_MODEL_H */
Index: kernel/include/IMP/UnaryFunction.h
===================================================================
--- kernel/include/IMP/UnaryFunction.h	(revision 587)
+++ kernel/include/IMP/UnaryFunction.h	(working copy)
@@ -9,7 +9,7 @@
 
 #include "IMP_config.h"
 #include "base_types.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 
 namespace IMP
 {
@@ -18,7 +18,7 @@
 /** These functors take a single feature value, and return a corresponding
     score (and optionally also the first derivative).
  */
-class IMPDLLEXPORT UnaryFunction : public internal::Object
+class IMPDLLEXPORT UnaryFunction : public internal::RefCountedObject
 {
 public:
   UnaryFunction() {}
@@ -41,6 +41,8 @@
   virtual void show(std::ostream &out=std::cout) const = 0;
 };
 
+IMP_OUTPUT_OPERATOR(UnaryFunction);
+
 } // namespace IMP
 
 #endif  /* __IMP_UNARY_FUNCTION_H */
Index: kernel/include/IMP/score_states/BondDecoratorListScoreState.h
===================================================================
--- kernel/include/IMP/score_states/BondDecoratorListScoreState.h	(revision 587)
+++ kernel/include/IMP/score_states/BondDecoratorListScoreState.h	(working copy)
@@ -8,10 +8,11 @@
 #ifndef __IMP_BOND_DECORATOR_LIST_SCORE_STATE_H
 #define __IMP_BOND_DECORATOR_LIST_SCORE_STATE_H
 
-#include <vector>
 #include "BondedListScoreState.h"
 #include "../decorators/bond_decorators.h"
 
+#include <vector>
+
 namespace IMP
 {
 
Index: kernel/include/IMP/score_states/CoverBondsScoreState.h
===================================================================
--- kernel/include/IMP/score_states/CoverBondsScoreState.h	(revision 587)
+++ kernel/include/IMP/score_states/CoverBondsScoreState.h	(working copy)
@@ -11,6 +11,7 @@
 #include "../ScoreState.h"
 #include "BondDecoratorListScoreState.h"
 #include "../internal/kernel_version_info.h"
+#include "../internal/ObjectPointer.h"
 
 namespace IMP
 {
@@ -29,7 +30,7 @@
  */
 class IMPDLLEXPORT CoverBondsScoreState: public ScoreState
 {
-  internal::ObjectPointer<BondDecoratorListScoreState, false> bl_;
+  internal::ObjectPointer<BondDecoratorListScoreState, true> bl_;
   FloatKey rk_;
 public:
   /** Get the list of bonds from the BondDecoratorListScoreState. This list is
Index: kernel/include/IMP/restraints/ConnectivityRestraint.h
===================================================================
--- kernel/include/IMP/restraints/ConnectivityRestraint.h	(revision 587)
+++ kernel/include/IMP/restraints/ConnectivityRestraint.h	(working copy)
@@ -11,8 +11,6 @@
 #ifndef __IMP_CONNECTIVITY_RESTRAINT_H
 #define __IMP_CONNECTIVITY_RESTRAINT_H
 
-#include <list>
-
 #include "../IMP_config.h"
 #include "../Restraint.h"
 #include "../internal/kernel_version_info.h"
Index: kernel/include/IMP/restraints/SConscript
===================================================================
--- kernel/include/IMP/restraints/SConscript	(revision 587)
+++ kernel/include/IMP/restraints/SConscript	(working copy)
@@ -6,7 +6,8 @@
          'DihedralRestraint.h', 'RestraintSet.h',
          'NonbondedRestraint.h', 'BondDecoratorRestraint.h',
          'SingletonListRestraint.h', 'PairListRestraint.h',
-         'TripletChainRestraint.h', 'PairChainRestraint.h']
+         'TripletChainRestraint.h', 'PairChainRestraint.h',
+         'ConstantRestraint.h']
 
 # Install the include files:
 includedir = os.path.join(env['includedir'], 'IMP', 'restraints')
Index: kernel/include/IMP/restraints/NonbondedRestraint.h
===================================================================
--- kernel/include/IMP/restraints/NonbondedRestraint.h	(revision 587)
+++ kernel/include/IMP/restraints/NonbondedRestraint.h	(working copy)
@@ -15,11 +15,11 @@
 #include "../Restraint.h"
 #include "../internal/kernel_version_info.h"
 #include "../internal/ObjectPointer.h"
+#include "../score_states/NonbondedListScoreState.h"
 
 namespace IMP
 {
 
-class NonbondedListScoreState;
 class PairScore;
 
 //! Apply a PairScore to all nonbonded pairs of particles
@@ -41,7 +41,7 @@
   IMP_RESTRAINT(internal::kernel_version_info)
 
 protected:
-  NonbondedListScoreState *nbl_;
+  internal::ObjectPointer<NonbondedListScoreState, true> nbl_;
   internal::ObjectPointer<PairScore, true> sf_;
 };
 
Index: kernel/include/IMP/restraints/ConstantRestraint.h
===================================================================
--- kernel/include/IMP/restraints/ConstantRestraint.h	(revision 0)
+++ kernel/include/IMP/restraints/ConstantRestraint.h	(revision 0)
@@ -0,0 +1,38 @@
+/**
+ *  \file ConstantRestraint.h    \brief Constant restraint.
+ *
+ *  Just return a constant.
+ *
+ *  Copyright 2007-8 Sali Lab. All rights reserved.
+ *
+ */
+
+#ifndef __IMP_CONSTANT_RESTRAINT_H
+#define __IMP_CONSTANT_RESTRAINT_H
+
+#include "../IMP_config.h"
+#include "../Restraint.h"
+#include "../internal/kernel_version_info.h"
+
+namespace IMP
+{
+class PairScore;
+
+//! Return a constant value.
+/** This restraint is mostly for testing, but can also be used to make
+    the total score look nicer..
+
+    \ingroup restraint
+ */
+class IMPDLLEXPORT ConstantRestraint : public Restraint
+{
+  Float v_;
+public:
+  ConstantRestraint(Float v);
+
+  IMP_RESTRAINT(internal::kernel_version_info)
+};
+
+} // namespace IMP
+
+#endif /* __IMP_CONNECTIVITY_RESTRAINT_H */
Index: kernel/include/IMP/restraints/DistanceRestraint.h
===================================================================
--- kernel/include/IMP/restraints/DistanceRestraint.h	(revision 587)
+++ kernel/include/IMP/restraints/DistanceRestraint.h	(working copy)
@@ -8,14 +8,12 @@
 #ifndef __IMP_DISTANCE_RESTRAINT_H
 #define __IMP_DISTANCE_RESTRAINT_H
 
-#include <vector>
-#include <iostream>
-
 #include "../IMP_config.h"
 #include "../pair_scores/DistancePairScore.h"
 #include "../Restraint.h"
 #include "../internal/kernel_version_info.h"
 
+#include <iostream>
 
 namespace IMP
 {
Index: kernel/include/IMP/Restraint.h
===================================================================
--- kernel/include/IMP/Restraint.h	(revision 587)
+++ kernel/include/IMP/Restraint.h	(working copy)
@@ -49,7 +49,7 @@
     \note Physical restraints should use the units of kcal/mol for restraint
     values and kcal/mol/A for derivatives.
  */
-class IMPDLLEXPORT Restraint : public internal::Object
+class IMPDLLEXPORT Restraint : public internal::RefCountedObject
 {
 public:
   //! Initialize the Restraint
Index: kernel/include/IMP/OptimizerState.h
===================================================================
--- kernel/include/IMP/OptimizerState.h	(revision 587)
+++ kernel/include/IMP/OptimizerState.h	(working copy)
@@ -10,7 +10,7 @@
 
 #include "IMP_config.h"
 #include "VersionInfo.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 #include "internal/ObjectPointer.h"
 #include "Optimizer.h"
 
Index: kernel/include/IMP/internal/RefCountedObject.h
===================================================================
--- kernel/include/IMP/internal/RefCountedObject.h	(revision 587)
+++ kernel/include/IMP/internal/RefCountedObject.h	(working copy)
@@ -166,7 +166,7 @@
     // no checks
   } else {
     IMP_check(!o->get_has_ref(), "Trying to own already owned but "
-              << "non-reference-counted object.",
+              << "non-reference-counted object: " << *o,
               ValueException("Already owned object"));
   }
   o->ref();
Index: kernel/include/IMP/PairScore.h
===================================================================
--- kernel/include/IMP/PairScore.h	(revision 587)
+++ kernel/include/IMP/PairScore.h	(working copy)
@@ -9,7 +9,7 @@
 
 #include "IMP_config.h"
 #include "base_types.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 #include "Particle.h"
 #include "DerivativeAccumulator.h"
 
@@ -25,7 +25,7 @@
 /** PairScores should take a UnaryFunction as their first
     argument if such is needed.
 */
-class IMPDLLEXPORT PairScore : public internal::Object
+class IMPDLLEXPORT PairScore : public internal::RefCountedObject
 {
 public:
   PairScore() {}
@@ -36,6 +36,8 @@
   virtual void show(std::ostream &out=std::cout) const = 0;
 };
 
+IMP_OUTPUT_OPERATOR(PairScore);
+
 } // namespace IMP
 
 #endif  /* __IMP_PAIR_SCORE_H */
Index: kernel/include/IMP/ScoreState.h
===================================================================
--- kernel/include/IMP/ScoreState.h	(revision 587)
+++ kernel/include/IMP/ScoreState.h	(working copy)
@@ -9,7 +9,7 @@
 #define __IMP_SCORE_STATE_H
 
 #include "IMP_config.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 #include "internal/ObjectPointer.h"
 #include "Model.h"
 #include "DerivativeAccumulator.h"
@@ -37,7 +37,7 @@
     of lines per update call.
 
  */
-class IMPDLLEXPORT ScoreState : public internal::Object
+class IMPDLLEXPORT ScoreState : public internal::RefCountedObject
 {
   friend class Model;
   void set_model(Model* model);
Index: kernel/include/IMP/TripletScore.h
===================================================================
--- kernel/include/IMP/TripletScore.h	(revision 587)
+++ kernel/include/IMP/TripletScore.h	(working copy)
@@ -9,7 +9,7 @@
 
 #include "IMP_config.h"
 #include "base_types.h"
-#include "internal/Object.h"
+#include "internal/RefCountedObject.h"
 #include "Particle.h"
 #include "DerivativeAccumulator.h"
 
@@ -26,7 +26,7 @@
 /** TripletScores should take a UnaryFunction as their first
     argument if such is needed.
 */
-class IMPDLLEXPORT TripletScore : public internal::Object
+class IMPDLLEXPORT TripletScore : public internal::RefCountedObject
 {
 public:
   TripletScore() {}
@@ -37,6 +37,8 @@
   virtual void show(std::ostream &out=std::cout) const = 0;
 };
 
+IMP_OUTPUT_OPERATOR(TripletScore);
+
 } // namespace IMP
 
 #endif  /* __IMP_TRIPLET_SCORE_H */
Index: kernel/include/IMP/Optimizer.h
===================================================================
--- kernel/include/IMP/Optimizer.h	(revision 587)
+++ kernel/include/IMP/Optimizer.h	(working copy)
@@ -63,6 +63,10 @@
    */
   void set_model(Model *m) {model_=m;}
 
+  virtual void show(std::ostream &out= std::cout) const {
+    out << "Some optimizer" << std::endl;
+  }
+
   IMP_CONTAINER(OptimizerState, optimizer_state, OptimizerStateIndex);
 
 protected:
@@ -197,8 +201,8 @@
   internal::ObjectPointer<Model, false> model_;
 };
 
+IMP_OUTPUT_OPERATOR(Optimizer);
 
-
 } // namespace IMP
 
 #endif  /* __IMP_OPTIMIZER_H */
Index: kernel/include/IMP.h
===================================================================
--- kernel/include/IMP.h	(revision 587)
+++ kernel/include/IMP.h	(working copy)
@@ -56,6 +56,7 @@
 #include "IMP/singleton_scores/AttributeSingletonScore.h"
 #include "IMP/triplet_scores/AngleTripletScore.h"
 #include "IMP/restraints/RestraintSet.h"
+#include "IMP/restraints/ConstantRestraint.h"
 #include "IMP/restraints/DistanceRestraint.h"
 #include "IMP/restraints/AngleRestraint.h"
 #include "IMP/restraints/DihedralRestraint.h"
Index: kernel/pyext/IMP.i
===================================================================
--- kernel/pyext/IMP.i	(revision 587)
+++ kernel/pyext/IMP.i	(working copy)
@@ -195,17 +195,6 @@
 %include "IMP/singleton_scores/DistanceToSingletonScore.h"
 %include "IMP/singleton_scores/AttributeSingletonScore.h"
 %include "IMP/triplet_scores/AngleTripletScore.h"
-%include "IMP/restraints/DistanceRestraint.h"
-%include "IMP/restraints/AngleRestraint.h"
-%include "IMP/restraints/TripletChainRestraint.h"
-%include "IMP/restraints/PairChainRestraint.h"
-%include "IMP/restraints/DihedralRestraint.h"
-%include "IMP/restraints/ConnectivityRestraint.h"
-%include "IMP/restraints/NonbondedRestraint.h"
-%include "IMP/restraints/BondDecoratorRestraint.h"
-%include "IMP/restraints/SingletonListRestraint.h"
-%include "IMP/restraints/PairListRestraint.h"
-%include "IMP/restraints/RestraintSet.h"
 %include "IMP/score_states/BondedListScoreState.h"
 %include "IMP/score_states/MaxChangeScoreState.h"
 %include "IMP/score_states/NonbondedListScoreState.h"
@@ -214,6 +203,18 @@
 %include "IMP/score_states/BipartiteNonbondedListScoreState.h"
 %include "IMP/score_states/GravityCenterScoreState.h"
 %include "IMP/score_states/CoverBondsScoreState.h"
+%include "IMP/restraints/AngleRestraint.h"
+%include "IMP/restraints/BondDecoratorRestraint.h"
+%include "IMP/restraints/ConnectivityRestraint.h"
+%include "IMP/restraints/ConstantRestraint.h"
+%include "IMP/restraints/DihedralRestraint.h"
+%include "IMP/restraints/DistanceRestraint.h"
+%include "IMP/restraints/NonbondedRestraint.h"
+%include "IMP/restraints/PairChainRestraint.h"
+%include "IMP/restraints/PairListRestraint.h"
+%include "IMP/restraints/RestraintSet.h"
+%include "IMP/restraints/SingletonListRestraint.h"
+%include "IMP/restraints/TripletChainRestraint.h"
 
 namespace IMP {
   %template(ParticleIndex) Index<ParticleTag>;
Index: kernel/test/particles/test_refcount.py
===================================================================
--- kernel/test/particles/test_refcount.py	(revision 587)
+++ kernel/test/particles/test_refcount.py	(working copy)
@@ -122,14 +122,17 @@
         mc= IMP.MaxChangeScoreState(IMP.XYZDecorator.get_xyz_keys())
         print "Add particle to mc"
         mc.add_particle(p)
-        self._check_number(1)
+        # also have the score state now
+        self._check_number(2)
         print "Remove from model"
         m.remove_particle(pi)
-        self._check_number(1)
+        self._check_number(2)
         p=1
-        self._check_number(1)
+        self._check_number(2)
         print "Remove from mc"
         mc.clear_particles()
+        self._check_number(1)
+        mc=0
         self._check_number(0)
 
     def test_skip(self):
@@ -144,6 +147,20 @@
         ps= m.get_particles()
         self.assertEqual(len(ps), 0, "Should no particles particle")
 
+    def test_restraints(self):
+        m= IMP.Model()
+        r= IMP.ConstantRestraint(1)
+        s= IMP.RestraintSet()
+        m.add_restraint(s)
+        m.add_restraint(r)
+        s.add_restraint(r)
+        m.evaluate(False)
+        r=0
+        s=0
+        m=0
+        self._check_number(0)
+        
+        
 
 if __name__ == '__main__':
     unittest.main()