Commit 759d9e82 authored by Jorn Baayen's avatar Jorn Baayen

Various Python3/casadi3 fixes

parent 4c4b629f
......@@ -106,7 +106,7 @@ class BSpline1D(BSpline):
ypt = SX.sym('ypt')
sq_diff = Function('sq_diff', [xpt, ypt], [
(ypt - bspline(c, xpt)[0])**2])
sq_diff = sq_diff.map('sq_diff', 'serial', N)
sq_diff = sq_diff.map(N, 'serial')
f = sum2(sq_diff(SX(x), SX(y))[0])
# Setup Curvature Constraints
......
......@@ -382,7 +382,7 @@ class CollocatedIntegratedOptimizationProblem(OptimizationProblem, metaclass = A
else:
input_syms = [self.variable(input_sym.name()) for input_sym in lookup_table.inputs]
[value] = lookup_table.function(input_syms)
value = lookup_table.function(*input_syms)
[dae_residual] = substitute(
[dae_residual], [sym], [value])
......
......@@ -6,7 +6,7 @@ import rtctools.data.csv as csv
from scipy.interpolate import splrep, bisplrep, splev, bisplev
from casadi import SX, Function
import numpy as np
import ConfigParser
import configparser
import logging
import pickle
import glob
......@@ -78,7 +78,7 @@ class CSVLookupTableMixin(OptimizationProblem):
ini_path = os.path.join(
self._lookup_table_folder, 'curvefit_options.ini')
try:
ini_config = ConfigParser.RawConfigParser()
ini_config = configparser.RawConfigParser()
ini_config.readfp(open(ini_path))
no_curvefit_options = False
except IOError:
......@@ -95,9 +95,9 @@ class CSVLookupTableMixin(OptimizationProblem):
def get_property(prop_name):
try:
prop = int(ini_config.get(curve_name, prop_name))
except ConfigParser.NoSectionError:
except configparser.NoSectionError:
prop = 0
except ConfigParser.NoOptionError:
except configparser.NoOptionError:
prop = 0
except ValueError:
raise Exception(
......
......@@ -81,7 +81,7 @@ class CSVMixin(OptimizationProblem):
self._initial_state = []
if self.csv_ensemble_mode:
self._ensemble = np.genfromtxt(os.path.join(
self._input_folder, 'ensemble.csv'), delimiter=",", deletechars='', dtype=None, names=True)
self._input_folder, 'ensemble.csv'), delimiter=",", deletechars='', dtype=None, names=True, converters={0: str})
logger.debug("CSVMixin: Read ensemble description")
for ensemble_member_name in self._ensemble['name']:
......@@ -182,7 +182,7 @@ class CSVMixin(OptimizationProblem):
# Load parameters from parameter config
for parameter in self.dae_variables['parameters']:
parameter = parameter.getName()
parameter = parameter.name()
try:
parameters[parameter] = self._parameters[ensemble_member][parameter]
except KeyError:
......@@ -200,7 +200,7 @@ class CSVMixin(OptimizationProblem):
# Load bounds from timeseries
for variable in self.dae_variables['constant_inputs']:
variable = variable.getName()
variable = variable.name()
try:
constant_inputs[variable] = Timeseries(
self._timeseries_times_sec, self._timeseries[ensemble_member][variable])
......@@ -219,7 +219,7 @@ class CSVMixin(OptimizationProblem):
# Load bounds from timeseries
for variable in self.dae_variables['free_variables']:
variable = variable.getName()
variable = variable.name()
m, M = None, None
......@@ -265,7 +265,7 @@ class CSVMixin(OptimizationProblem):
# Load parameters from parameter config
for variable in self.dae_variables['free_variables']:
variable = variable.getName()
variable = variable.name()
try:
initial_state[variable] = self._initial_state[ensemble_member][variable]
except (KeyError, ValueError):
......@@ -282,7 +282,7 @@ class CSVMixin(OptimizationProblem):
# Load seed values from CSV
for variable in self.dae_variables['free_variables']:
variable = variable.getName()
variable = variable.name()
try:
s = Timeseries(self._timeseries_times_sec, self._timeseries[ensemble_member][variable])
except (KeyError, ValueError):
......@@ -304,13 +304,13 @@ class CSVMixin(OptimizationProblem):
def write_output(ensemble_member, folder):
results = self.extract_results(ensemble_member)
names = ['time'] + sorted(set([sym.getName() for sym in self.output_variables]))
names = ['time'] + sorted(set([sym.name() for sym in self.output_variables]))
formats = ['O'] + (len(names) - 1) * ['f8']
dtype = dict(names=names, formats=formats)
data = np.zeros(len(self._timeseries_times), dtype=dtype)
data['time'] = self._timeseries_times
for i, output_variable in enumerate(self.output_variables):
output_variable = output_variable.getName()
output_variable = output_variable.name()
try:
values = results[output_variable]
if len(values) != len(times):
......
......@@ -208,8 +208,8 @@ class ModelicaMixin(OptimizationProblem):
constant_input_names = set(sym.name() for sym in self._mx['constant_inputs'])
for v in self._pymola_model.inputs:
if v.symbol.name() in constant_input_names:
constant_inputs[sym.name()] = Timeseries(
times, repmat(sym.value, len(times)))
constant_inputs[v.symbol.name()] = Timeseries(
times, np.full_like(times, v.value))
if logger.getEffectiveLevel() == logging.DEBUG:
logger.debug("Read constant input {} = {} from Modelica model".format(
sym.name(), sym.value))
......
......@@ -61,10 +61,10 @@ class LookupTable:
else:
if hasattr(args[0], '__iter__'):
evaluator = np.vectorize(
lambda v: float(self.function([v])[0]))
lambda v: float(self.function(v)))
return evaluator(args[0])
else:
return float(self.function(args)[0])
return float(self.function(*args))
class OptimizationProblem(metaclass = ABCMeta):
......
......@@ -92,7 +92,7 @@ class TestCSVMixin(TestCase):
def test_interpolate(self):
for v in ['x', 'y', 'u']:
for i in [0, len(self.problem.times()) / 2, -1]:
for i in [0, int(len(self.problem.times()) / 2), -1]:
a = self.problem.interpolate(
self.problem.times()[i], self.problem.times(), self.results[v], 0.0, 0.0)
b = self.results[v][i]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment